/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw 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; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw 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


 */
package japlot.jaxodraw;

import java.io.File;

import java.util.Hashtable;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;


/** Opens a file chooser dialog that is used for opening, saving and exporting
 * files.
 */
public class JaxoChooseFile {
    /** Brings up a file chooser dialog.
     * @return A string with the absolute path of the chosen file, or an empty
     * string, if no file was selected.
     * @param selectedFile A file that should be marked as pre-selected.
     * @param fileFilter A filter that indicates which files should be shown.
     * @param approveText The text to be shown as a tool tip of the approve
     * button in the dialog.
     * @param description A description of this dialog.
     */
    public final String chooseFile(String[] fileFilter, String approveText,
        String description, String selectedFile) {
        String fileName = "";

        final JFileChooser chooser = new JFileChooser(JaxoDraw.getCurDir());
        chooser.setDialogTitle(approveText);
        chooser.setFileFilter(new JaxoFileFilter(fileFilter, description));
        chooser.setSelectedFile(new File(selectedFile));
        chooser.setApproveButtonText(approveText);
        chooser.setApproveButtonToolTipText(approveText
            + java.util.ResourceBundle.getBundle(JaxoPrefs.getPref(
                    JaxoPrefs.PREF_LANGUAGE)).getString("_chosen_file"));

        int returnVal = chooser.showDialog(null, null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            fileName = chooser.getSelectedFile().getAbsolutePath();
            JaxoDraw.setCurDir(chooser.getSelectedFile().getParent());
        }

        return fileName;
    }

    static class JaxoFileFilter extends FileFilter {
        private Hashtable filters = null;
        private String description = null;

        public JaxoFileFilter(String[] filterStrings, String descriptionString) {
            this.filters = new Hashtable();

            for (int i = 0; i < filterStrings.length; i++) {
                addExtension(filterStrings[i]);
            }

            if (descriptionString != null) {
                setDescription(descriptionString);
            }
        }

        public final boolean accept(File f) {
            if (f != null) {
                if (f.isDirectory()) {
                    return true;
                }

                String extension = getExtension(f);

                if ((extension != null)
                        && (filters.get(getExtension(f)) != null)) {
                    return true;
                }
            }

            return false;
        }

        public final String getExtension(File f) {
            if (f != null) {
                String filename = f.getName();
                int i = filename.lastIndexOf('.');

                if ((i > 0) && (i < (filename.length() - 1))) {
                    return filename.substring(i + 1).toLowerCase();
                }
            }

            return null;
        }

        public final void addExtension(String extension) {
            if (filters == null) {
                filters = new Hashtable(JaxoSaveGraph.LIST_INIT_SIZE);
            }

            filters.put(extension.toLowerCase(), this);
        }

        public final String getDescription() {
            return description;
        }

        public final void setDescription(String newDescription) {
            this.description = newDescription;
        }
    }
}

 

Ads help maintain this website.