Source for org.jfree.ui.action.AbstractFileSelectionAction

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ----------------------------
  28:  * AbstractFileSelectionAction.java
  29:  * ----------------------------
  30:  * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: AbstractFileSelectionAction.java,v 1.4 2005/10/18 13:22:13 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 21-Nov-2004 : Initial version
  40:  *
  41:  */
  42: package org.jfree.ui.action;
  43: 
  44: import java.awt.Component;
  45: import java.io.File;
  46: import javax.swing.JFileChooser;
  47: 
  48: import org.jfree.ui.ExtensionFileFilter;
  49: import org.jfree.util.StringUtils;
  50: 
  51: /**
  52:  * A base class for all file operations. This implementation provides all methods
  53:  * to let the user select a file.
  54:  *
  55:  * @author Thomas Morgner
  56:  */
  57: public abstract class AbstractFileSelectionAction extends AbstractActionDowngrade {
  58:     /**
  59:      * The FileChooser that is used to perform the selection.
  60:      */
  61:     private JFileChooser fileChooser;
  62:     /**
  63:      * The (optional) parent component.
  64:      */
  65:     private Component parent;
  66: 
  67:     /**
  68:      * Creates a new FileSelectionAction with the given optional parent component
  69:      * as parent for the file chooser dialog.
  70:      *
  71:      * @param parent the parent
  72:      */
  73:     public AbstractFileSelectionAction(final Component parent) {
  74:         this.parent = parent;
  75:     }
  76: 
  77:     /**
  78:      * Returns the file extension that should be used for the operation.
  79:      *
  80:      * @return the file extension.
  81:      */
  82:     protected abstract String getFileExtension();
  83: 
  84:     /**
  85:      * Returns a descriptive text describing the file extension.
  86:      *
  87:      * @return the file description.
  88:      */
  89:     protected abstract String getFileDescription();
  90: 
  91:     /**
  92:      * Returns the working directory that should be used when initializing
  93:      * the FileChooser.
  94:      *
  95:      * @return the working directory.
  96:      */
  97:     protected File getCurrentDirectory() {
  98:         return new File(".");
  99:     }
 100: 
 101:     /**
 102:      * Selects a file to use as target for the operation.
 103:      *
 104:      * @param selectedFile    the selected file.
 105:      * @param dialogType  the dialog type.
 106:      * @param appendExtension true, if the file extension should be added if
 107:      *                        necessary, false if the unmodified filename should be used.
 108:      * 
 109:      * @return the selected and approved file or null, if the user canceled
 110:      *         the operation
 111:      */
 112:     protected File performSelectFile(final File selectedFile,
 113:                                      final int dialogType,
 114:                                      final boolean appendExtension) {
 115:         if (this.fileChooser == null) {
 116:             this.fileChooser = createFileChooser();
 117:         }
 118: 
 119:         this.fileChooser.setSelectedFile(selectedFile);
 120:         this.fileChooser.setDialogType(dialogType);
 121:         final int option = this.fileChooser.showDialog(this.parent, null);
 122:         if (option == JFileChooser.APPROVE_OPTION) {
 123:             final File selFile = this.fileChooser.getSelectedFile();
 124:             String selFileName = selFile.getAbsolutePath();
 125:             if (StringUtils.endsWithIgnoreCase(selFileName, getFileExtension()) == false) {
 126:                 selFileName = selFileName + getFileExtension();
 127:             }
 128:             return new File(selFileName);
 129:         }
 130:         return null;
 131:     }
 132: 
 133:     /**
 134:      * Creates the file chooser.
 135:      *
 136:      * @return the initialized file chooser.
 137:      */
 138:     protected JFileChooser createFileChooser() {
 139:         final JFileChooser fc = new JFileChooser();
 140:         fc.addChoosableFileFilter(
 141:             new ExtensionFileFilter(getFileDescription(), getFileExtension())
 142:         );
 143:         fc.setMultiSelectionEnabled(false);
 144:         fc.setCurrentDirectory(getCurrentDirectory());
 145:         return fc;
 146:     }
 147:     
 148: }