001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ----------------------------
028: * AbstractFileSelectionAction.java
029: * ----------------------------
030: * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: AbstractFileSelectionAction.java,v 1.4 2005/10/18 13:22:13 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 21-Nov-2004 : Initial version
040: *
041: */
042: package org.jfree.ui.action;
043:
044: import java.awt.Component;
045: import java.io.File;
046: import javax.swing.JFileChooser;
047:
048: import org.jfree.ui.ExtensionFileFilter;
049: import org.jfree.util.StringUtils;
050:
051: /**
052: * A base class for all file operations. This implementation provides all methods
053: * to let the user select a file.
054: *
055: * @author Thomas Morgner
056: */
057: public abstract class AbstractFileSelectionAction extends
058: AbstractActionDowngrade {
059: /**
060: * The FileChooser that is used to perform the selection.
061: */
062: private JFileChooser fileChooser;
063: /**
064: * The (optional) parent component.
065: */
066: private Component parent;
067:
068: /**
069: * Creates a new FileSelectionAction with the given optional parent component
070: * as parent for the file chooser dialog.
071: *
072: * @param parent the parent
073: */
074: public AbstractFileSelectionAction(final Component parent) {
075: this .parent = parent;
076: }
077:
078: /**
079: * Returns the file extension that should be used for the operation.
080: *
081: * @return the file extension.
082: */
083: protected abstract String getFileExtension();
084:
085: /**
086: * Returns a descriptive text describing the file extension.
087: *
088: * @return the file description.
089: */
090: protected abstract String getFileDescription();
091:
092: /**
093: * Returns the working directory that should be used when initializing
094: * the FileChooser.
095: *
096: * @return the working directory.
097: */
098: protected File getCurrentDirectory() {
099: return new File(".");
100: }
101:
102: /**
103: * Selects a file to use as target for the operation.
104: *
105: * @param selectedFile the selected file.
106: * @param dialogType the dialog type.
107: * @param appendExtension true, if the file extension should be added if
108: * necessary, false if the unmodified filename should be used.
109: *
110: * @return the selected and approved file or null, if the user canceled
111: * the operation
112: */
113: protected File performSelectFile(final File selectedFile,
114: final int dialogType, 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,
122: null);
123: if (option == JFileChooser.APPROVE_OPTION) {
124: final File selFile = this .fileChooser.getSelectedFile();
125: String selFileName = selFile.getAbsolutePath();
126: if (StringUtils.endsWithIgnoreCase(selFileName,
127: getFileExtension()) == false) {
128: selFileName = selFileName + getFileExtension();
129: }
130: return new File(selFileName);
131: }
132: return null;
133: }
134:
135: /**
136: * Creates the file chooser.
137: *
138: * @return the initialized file chooser.
139: */
140: protected JFileChooser createFileChooser() {
141: final JFileChooser fc = new JFileChooser();
142: fc.addChoosableFileFilter(new ExtensionFileFilter(
143: getFileDescription(), getFileExtension()));
144: fc.setMultiSelectionEnabled(false);
145: fc.setCurrentDirectory(getCurrentDirectory());
146: return fc;
147: }
148:
149: }
|