001package jmri.configurexml;
002
003import java.awt.event.ActionEvent;
004import java.io.File;
005import javax.swing.JFileChooser;
006import javax.swing.JOptionPane;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Load configuration information from an XML file.
012 * <p>
013 * The file context for this is the "user" file chooser.
014 * <p>
015 * This will load whatever information types are present in the file. See
016 * {@link jmri.ConfigureManager} for information on the various types of
017 * information stored in configuration files.
018 *
019 * @author Bob Jacobsen Copyright (C) 2002
020 * @see jmri.jmrit.XmlFile
021 */
022public class LoadXmlUserAction extends LoadXmlConfigAction {
023
024    static private File currentFile = null;
025
026    public LoadXmlUserAction() {
027        this(Bundle.getMessage("MenuItemLoad"));
028    }
029
030    public LoadXmlUserAction(String s) {
031        super(s);
032    }
033
034    @Override
035    public void actionPerformed(ActionEvent e) {
036        JFileChooser userFileChooser = getUserFileChooser();
037        userFileChooser.setDialogType(javax.swing.JFileChooser.OPEN_DIALOG);
038        userFileChooser.setApproveButtonText(Bundle.getMessage("ButtonOpen"));
039        // Cancel button can't be localized like userFileChooser.setCancelButtonText() TODO
040        userFileChooser.setDialogTitle(Bundle.getMessage("LoadTitle"));
041
042        boolean results = loadFile(userFileChooser);
043        if (results) {
044            log.debug("load was successful");
045            setCurrentFile(userFileChooser.getSelectedFile());
046        } else {
047            log.debug("load failed");
048            JOptionPane.showMessageDialog(null,
049                    Bundle.getMessage("LoadHasErrors") + "\n"
050                    + Bundle.getMessage("CheckPreferences") + "\n"
051                    + Bundle.getMessage("ConsoleWindowHasInfo"),
052                    Bundle.getMessage("LoadError"), JOptionPane.ERROR_MESSAGE);
053            setCurrentFile(null);
054        }
055    }
056
057    /**
058     * Used by e.g. jmri.jmrit.mailreport.ReportPanel et al to know last load
059     *
060     * @return the last file loaded using this action; returns null if this
061     *         action was not called or if the last time this action was called,
062     *         no file was loaded
063     */
064    public static File getCurrentFile() {
065        return currentFile;
066    }
067
068    private static void setCurrentFile(File arg) {
069        currentFile = arg;
070    }
071
072    // initialize logging
073    private final static Logger log = LoggerFactory.getLogger(LoadXmlUserAction.class);
074
075}