/*============================================================================= | | NAME | | FileAction.java | | DESCRIPTION | | Action class for File item on the menu bar. | | AUTHOR | | Sean E. O'Connor | | 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 java.io.File ; // File class. import javax.swing.* ; // Swing main GUI package. import javax.swing.filechooser.FileFilter ; // File filter. import Model.* ; import Prentice.* ; // Prentice stuff. // Class which retains the state and handles events of menu items and buttons. class FileAction extends AbstractAction implements PrenticeConstants { // Make the file chooser a class variable and initialize it. // That way it remembers the last opened directory from one // invocation to the next and remembers our other preferences. static private JFileChooser fileOpenDialog ; static { fileOpenDialog = new JFileChooser() ; // Title and tooltip. fileOpenDialog.setDialogTitle( "Open Model File" ) ; fileOpenDialog.setApproveButtonToolTipText( "The model file contains the wireframe graphics." ) ; // File filter for model .mod files. FileFilter modelFileFilter = new ModelFileFilter( ".mod", "(.mod) Model File" ) ; fileOpenDialog.setFileFilter( modelFileFilter ) ; // Don't allow (all files) to be displayed in dialog. fileOpenDialog.setAcceptAllFileFilterUsed( false ) ; } // File action. FileAction( String name ) { super( name ) ; // Name of action. } // File action with accelerator key. FileAction( String name, char shortcut ) { this( name ) ; KeyStroke keystroke = KeyStroke.getKeyStroke( shortcut, Event.CTRL_MASK ) ; // Shortcut key. if (keystroke != null) { putValue( ACCELERATOR_KEY, keystroke ) ; } } // File action with accelerator key and tooltip. FileAction( String name, char shortcut, String tooltip ) { this( name, shortcut ) ; if (tooltip != null) { putValue( SHORT_DESCRIPTION, tooltip ) ; } } // Event handler. public void actionPerformed( ActionEvent e ) { // Get unique name for submenu item. String name = (String) getValue( NAME ) ; // Take action. if (name.equals( NEW )) { System.err.println( "New model" ) ; } else if (name.equals( OPEN )) { System.err.println( "Open model" ) ; // Refresh the list of files in the directory in case // of user creations and deletions. fileOpenDialog.rescanCurrentDirectory() ; // Show list of model files. int status = fileOpenDialog.showOpenDialog( null ) ; // User chose a file. if (status == fileOpenDialog.APPROVE_OPTION) { System.err.println( "Approved" ) ; // Parse the selected model file. ModelFileParser p = new ModelFileParser( fileOpenDialog.getSelectedFile() ) ; p = null ; } } } } class ModelFileFilter extends FileFilter { private String description_ ; private String extension_ ; public ModelFileFilter( String extension, String description ) { extension_ = extension ; description_ = description ; } public boolean accept( File file ) { return (file.isDirectory() || file.getName().toLowerCase().endsWith( extension_ )) ; } public String getDescription() { return description_ ; } }