001package jmri.configurexml;
002
003import javax.swing.AbstractAction;
004import javax.swing.JFileChooser;
005import javax.swing.filechooser.FileNameExtensionFilter;
006import jmri.ConfigureManager;
007import jmri.InstanceManager;
008import jmri.implementation.JmriConfigurationManager;
009import jmri.util.FileUtil;
010
011/**
012 * Base implementation for the load and store actions.
013 * 
014 * Primarily provides file checking services to the specific subclasses that
015 * load/store particular types of data.
016 * <p>
017 * Also used to hold common information, specifically common instances of the
018 * JFileChooser. These bring the user back to the same place in the file system
019 * each time an action is invoked.
020 *
021 * @author Bob Jacobsen Copyright (C) 2004
022 * @see jmri.jmrit.XmlFile
023 */
024abstract public class LoadStoreBaseAction extends AbstractAction {
025
026    public LoadStoreBaseAction(String s) {
027        super(s);
028        // ensure that an XML config manager exists
029        if (!InstanceManager.getOptionalDefault(ConfigureManager.class).isPresent()) {
030            InstanceManager.setDefault(ConfigureManager.class, new JmriConfigurationManager());
031        }
032    }
033
034    /*
035     * These JFileChoosers are retained so that multiple actions can all open
036     * the JFileChoosers at the last used location for the context that the
037     * action supports.
038     */
039    static private JFileChooser allFileChooser = null;
040    static private JFileChooser configFileChooser = null;
041    static private JFileChooser userFileChooser = null;
042
043    static private JFileChooser getXmlFileChooser(String path) {
044        JFileChooser chooser = new jmri.util.swing.JmriJFileChooser(path);
045        chooser.setFileFilter(new FileNameExtensionFilter("XML files", "xml")); // NOI18N
046        return chooser;
047    }
048
049    static protected JFileChooser getAllFileChooser() {
050        if (allFileChooser == null) {
051            allFileChooser = getXmlFileChooser(FileUtil.getUserFilesPath());
052        }
053        return allFileChooser;
054    }
055
056    static protected JFileChooser getConfigFileChooser() {
057        if (configFileChooser == null) {
058            configFileChooser = getXmlFileChooser(FileUtil.getUserFilesPath());
059        }
060        return configFileChooser;
061    }
062
063    // Made public so JmriConfigurationManager.java can set the
064    // "Store Panels..." default file (to the panel file being loaded)
065    static public JFileChooser getUserFileChooser() {
066        if (userFileChooser == null) {
067            userFileChooser = getXmlFileChooser(FileUtil.getUserFilesPath());
068        }
069        return userFileChooser;
070    }
071}