/*============================================================================= | | NAME | | PrenticeView.java | | DESCRIPTION | | View showing buttons, scrollable image and status label. | We use a gridbag layout mangaer. | | +---------------------------------------------------+ | | Load Workspace | Save Workspace | | | +---------------------------------------------------+ | | | | | | | | Scroll pane | | | | | | -contains- | | | | | | a scrollable image | | | | | | -which paints- | | | | | | a PrenticeImage | | | | | | | | +---------------------------------------------------+ | | Status messages. | | +---------------------------------------------------+ | | | | LEGAL | | Prentice Version 1.1 - An Artist's Software Apprentice. | Copyright (C) 2003-2008 by Sean Erik O'Connor. All Rights Reserved. | | This program is free software; you can redistribute it and/or | modify it under the terms of the GNU General Public License | as published by the Free Software Foundation; version 2 | of the License. | | This program is distributed in the hope that it will be useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General Public License for more details. | | You should have received a copy of the GNU General Public License | along with this program; if not, write to the Free Software | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | | The author's address is artifex@seanerikoconnor.freeservers.com. | +============================================================================*/ package View ; // Package name for this project. import java.awt.* ; // AWT basic window handling. import java.awt.event.* ; // AWT basic event handling. import java.awt.geom.* ; // 2D graphics. import java.awt.image.* ; // Images and operations. import javax.swing.* ; // Swing main GUI package. import javax.swing.event.* ; // Swing GUI event handling. import java.awt.print.* ; // Printing. import Prentice.* ; // Prentice stuff. import Model.* ; // Within the frame, this view has the displays and controls for // our application. public class PrenticeView extends JPanel implements PrenticeConstants { // Handle to the application. private Prentice app ; // List of buttons. private JButton loadWorkspaceButton ; private JButton saveWorkspaceButton ; private JButton drawButton ; private JLabel cameraPositionLabel ; private JTextField cameraPosition ; private JSlider cameraAzumuth ; private JButton renderButton ; private JButton printButton ; // Scrollable image. private JScrollPane imageScrollPane ; private ScrollableImage scrollImage ; // Status label. private JLabel statusLabel ; // Add action listener which handles button push. class RenderButtonListener implements ActionListener { private Prentice app ; RenderButtonListener( Prentice app ) { super(); this.app = app ; } public void actionPerformed( ActionEvent e ) { DebugLog.println( "Render button pushed. Calling app.getModel().update()" ) ; app.getModel().update() ; } } /** * * Add action listener which handles camera position data entry. */ class CameraPositionListener implements ActionListener { private Prentice app ; private PrenticeView view ; CameraPositionListener( Prentice app, PrenticeView view ) { super(); this.app = app ; this.view = view ; } public void actionPerformed( ActionEvent e ) { String text = view.cameraPosition.getText(); view.cameraPosition.selectAll(); // Make sure the new text is visible, even if there // was a selection in the text area. cameraPosition.setCaretPosition(cameraPosition.getDocument().getLength()); float cameraLocation = Float.valueOf( text ).floatValue() ; System.out.println( "Camera position = " + cameraLocation ) ; app.getModel().eyeToPicturePlane( cameraLocation ) ; app.getModel().update() ; } } public ScrollableImage getScrollableImage() { return scrollImage ; } public PrenticeView( Prentice app ) { this.app = app ; DebugLog.println( "Entering PrenticeView" ) ; DebugLog.println( "app = " + app ) ; // Buttons. loadWorkspaceButton = new JButton( LOAD_WORKSPACE ) ; saveWorkspaceButton = new JButton( SAVE_WORKSPACE ) ; drawButton = new JButton( DRAW_BUTTON ) ; cameraPositionLabel = new JLabel( "Camera position:") ; cameraPosition = new JTextField( CAMERA_POSITION_FIELD_SIZE ) ; renderButton = new JButton( RENDER_BUTTON ) ; printButton = new JButton( PRINT_BUTTON ) ; // Scroll-savy image which goes into a scrollable container. scrollImage = new ScrollableImage( app ) ; // Set initial image size. scrollImage.setPreferredSize( new Dimension( 1024, 1024 )) ; // Put our client image into a scroll pane. imageScrollPane = new JScrollPane( scrollImage ) ; // Let scroll bars appear only if needed. imageScrollPane.setPreferredSize( new Dimension( 320, 240 ) ) ; // Status bar label. statusLabel = new JLabel( "Status Label" ) ; // Set initial size for the view. setPreferredSize( new Dimension( 640, 480 ) ) ; // Use a GridBag type layout and constraints. GridBagLayout gridbagLayout = new GridBagLayout() ; GridBagConstraints gridbagContraints = new GridBagConstraints() ; setLayout( gridbagLayout ) ; // Attach layout to view. //----------- Fill row 0 with buttons. gridbagContraints.gridx = 0 ; // Cell position in the spreadsheet-type grid. gridbagContraints.gridy = 0 ; gridbagContraints.gridwidth = 1 ; // Cell width and height in cell units. gridbagContraints.gridheight = 1 ; gridbagContraints.weightx = 0.5 ; // Buttons don't get extra space when window is stretched. gridbagContraints.weighty = 0.0 ; gridbagContraints.fill = GridBagConstraints.HORIZONTAL ; // Stretch button out horizontally to fill the cell. gridbagContraints.anchor = GridBagConstraints.CENTER ; // Center the button in the grid cell. gridbagLayout.setConstraints( loadWorkspaceButton, gridbagContraints ) ; add( loadWorkspaceButton ) ; gridbagContraints.gridx = 1 ; gridbagContraints.gridy = 0 ; gridbagContraints.gridwidth = 1 ; gridbagContraints.gridheight = 1 ; gridbagContraints.weightx = 0.5 ; gridbagContraints.weighty = 0.0 ; gridbagContraints.fill = GridBagConstraints.HORIZONTAL ; gridbagContraints.anchor = GridBagConstraints.CENTER ; gridbagLayout.setConstraints( saveWorkspaceButton, gridbagContraints ) ; add( saveWorkspaceButton ) ; gridbagContraints.gridx = 2 ; gridbagContraints.gridy = 0 ; gridbagContraints.gridwidth = 1 ; gridbagContraints.gridheight = 1 ; gridbagContraints.weightx = 0.5 ; gridbagContraints.weighty = 0.0 ; gridbagContraints.fill = GridBagConstraints.HORIZONTAL ; gridbagContraints.anchor = GridBagConstraints.CENTER ; gridbagLayout.setConstraints( drawButton, gridbagContraints ) ; add( drawButton ) ; gridbagContraints.gridx = 3 ; gridbagContraints.gridy = 0 ; gridbagContraints.gridwidth = 1 ; gridbagContraints.gridheight = 1 ; gridbagContraints.weightx = 0.5 ; gridbagContraints.weighty = 0.0 ; gridbagContraints.fill = GridBagConstraints.HORIZONTAL ; gridbagContraints.anchor = GridBagConstraints.CENTER ; gridbagLayout.setConstraints( renderButton, gridbagContraints ) ; add( renderButton ) ; gridbagContraints.gridx = 4 ; // Span the remainder of the row. gridbagContraints.gridy = 0 ; gridbagContraints.gridwidth = GridBagConstraints.REMAINDER ; gridbagContraints.gridheight = 1 ; gridbagContraints.weightx = 0.5 ; gridbagContraints.weighty = 0.0 ; gridbagContraints.fill = GridBagConstraints.HORIZONTAL ; gridbagContraints.anchor = GridBagConstraints.CENTER ; gridbagLayout.setConstraints( printButton, gridbagContraints ) ; add( printButton ) ; //----------- Fill row 1 with the scroll image pane. gridbagContraints.gridx = 0 ; gridbagContraints.gridy = 1 ; gridbagContraints.gridwidth = GridBagConstraints.REMAINDER ; // Span all cells horizontally. gridbagContraints.gridheight = 1 ; // One cell high. gridbagContraints.weightx = 0.5 ; gridbagContraints.weighty = 1.5 ; // Image gets more share of vertical space. gridbagContraints.fill = GridBagConstraints.BOTH ; // Stretch image out to fill the cell. gridbagContraints.anchor = GridBagConstraints.CENTER ; // Center the image in the grid cell. gridbagLayout.setConstraints( imageScrollPane, gridbagContraints ) ; add( imageScrollPane ) ; //----------- Fill row 2 with view parameters. gridbagContraints.gridx = 0 ; gridbagContraints.gridy = 2 ; gridbagContraints.gridwidth = 1 ; gridbagContraints.gridheight = 1 ; gridbagContraints.weightx = 0.5 ; gridbagContraints.weighty = 0.0 ; gridbagContraints.fill = GridBagConstraints.HORIZONTAL ; gridbagContraints.anchor = GridBagConstraints.CENTER ; gridbagLayout.setConstraints( cameraPositionLabel, gridbagContraints ) ; add( cameraPositionLabel ) ; gridbagContraints.gridx = 1 ; gridbagContraints.gridy = 2 ; gridbagContraints.gridwidth = 1 ; gridbagContraints.gridheight = 1 ; gridbagContraints.weightx = 0.5 ; gridbagContraints.weighty = 0.0 ; gridbagContraints.fill = GridBagConstraints.HORIZONTAL ; gridbagContraints.anchor = GridBagConstraints.CENTER ; gridbagLayout.setConstraints( cameraPosition, gridbagContraints ) ; add( cameraPosition ) ; //----------- Fill row 3 with the status label. gridbagContraints.gridx = 0 ; gridbagContraints.gridy = 3 ; gridbagContraints.gridwidth = 1 ; gridbagContraints.gridheight = GridBagConstraints.REMAINDER ; gridbagContraints.weightx = 0.5 ; gridbagContraints.weighty = 0.1 ; gridbagContraints.fill = GridBagConstraints.HORIZONTAL ; // Stretch button out horizontally to fill the cell. gridbagContraints.anchor = GridBagConstraints.CENTER ; gridbagLayout.setConstraints( statusLabel, gridbagContraints ) ; add( statusLabel ) ; // Add action listener to Render button. renderButton.addActionListener( new RenderButtonListener( app ) ) ; cameraPosition.addActionListener( new CameraPositionListener( app, this )) ; // Add action listener which handles print button push. printButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { // Do sample printing. PrenticePrint printer = new PrenticePrint() ; printer.print() ; printer = null ; } // end actionPerf } // end new ) ; // end addAction } // end PrenticeView() } // end class PrenticeView