    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 */
    042package org.jfree.ui.action;
    043
    044import java.awt.Component;
    045import java.io.File;
    046import javax.swing.JFileChooser;
    047
    048import org.jfree.ui.ExtensionFileFilter;
    049import 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 */
    057public abstract class AbstractFileSelectionAction extends AbstractActionDowngrade {
    058    /**
    059     * The FileChooser that is used to perform the selection.
    060     */
    061    private JFileChooser fileChooser;
    062    /**
    063     * The (optional) parent component.
    064     */
    065    private Component parent;
    066
    067    /**
    068     * Creates a new FileSelectionAction with the given optional parent component
    069     * as parent for the file chooser dialog.
    070     *
    071     * @param parent the parent
    072     */
    073    public AbstractFileSelectionAction(final Component parent) {
    074        this.parent = parent;
    075    }
    076
    077    /**
    078     * Returns the file extension that should be used for the operation.
    079     *
    080     * @return the file extension.
    081     */
    082    protected abstract String getFileExtension();
    083
    084    /**
    085     * Returns a descriptive text describing the file extension.
    086     *
    087     * @return the file description.
    088     */
    089    protected abstract String getFileDescription();
    090
    091    /**
    092     * Returns the working directory that should be used when initializing
    093     * the FileChooser.
    094     *
    095     * @return the working directory.
    096     */
    097    protected File getCurrentDirectory() {
    098        return new File(".");
    099    }
    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}