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