001package jmri.configurexml;
002
003import java.awt.event.ActionEvent;
004import java.io.File;
005
006import javax.swing.JFileChooser;
007
008import jmri.*;
009import jmri.util.swing.JmriJOptionPane;
010
011/**
012 * Load configuration information from an XML file.
013 * <p>
014 * The file context for this is the "user" file chooser.
015 * <p>
016 * This will load whatever information types are present in the file. See
017 * {@link jmri.ConfigureManager} for information on the various types of
018 * information stored in configuration files.
019 *
020 * @author Bob Jacobsen Copyright (C) 2002
021 * @see jmri.jmrit.XmlFile
022 */
023public class LoadXmlUserAction extends LoadXmlConfigAction {
024
025    private static File currentFile = null;
026
027    public LoadXmlUserAction() {
028        this(Bundle.getMessage("MenuItemLoad"));
029    }
030
031    public LoadXmlUserAction(String s) {
032        super(s);
033    }
034
035    @Override
036    public void actionPerformed(ActionEvent e) {
037        if (! InstanceManager.getDefault(PermissionManager.class)
038                .ensureAtLeastPermission(LoadAndStorePermissionOwner.LOAD_XML_FILE_PERMISSION,
039                        BooleanPermission.BooleanValue.TRUE)) {
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}