001package jmri.jmrit.decoderdefn;
002
003import java.io.File;
004import java.net.URI;
005import java.net.URL;
006import javax.swing.Icon;
007import javax.swing.JFileChooser;
008import javax.swing.JPanel;
009import jmri.util.swing.WindowInterface;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Install decoder definition from local file.
015 *
016 * @author Bob Jacobsen Copyright (C) 2008
017 * @see jmri.jmrit.XmlFile
018 */
019public class InstallDecoderFileAction extends InstallDecoderURLAction {
020
021    public InstallDecoderFileAction(String s, WindowInterface wi) {
022        super(s, wi);
023    }
024
025    public InstallDecoderFileAction(String s, Icon i, WindowInterface wi) {
026        super(s, i, wi);
027    }
028
029    public InstallDecoderFileAction(String s) {
030        super(s);
031    }
032
033    public InstallDecoderFileAction(String s, JPanel who) {
034        super(s);
035    }
036
037    JFileChooser fci;
038
039    @Override
040    URL pickURL(JPanel who) {
041        if (fci == null) {
042            fci = jmri.jmrit.XmlFile.userFileChooser("XML files", "xml");
043        }
044        // request the filename from an open dialog
045        fci.rescanCurrentDirectory();
046        int retVal = fci.showOpenDialog(who);
047        // handle selection or cancel
048        if (retVal == JFileChooser.APPROVE_OPTION) {
049            File file = fci.getSelectedFile();
050            if (log.isDebugEnabled()) {
051                log.debug("located file {} for XML processing", file);
052            }
053            try {
054                return new URI("file:" + file.getCanonicalPath()).toURL();
055            } catch (Exception e) {
056                log.error("Unexpected exception in new URL", e);
057                return null;
058            }
059        } else {
060            log.debug("cancelled in open dialog");
061            return null;
062        }
063    }
064
065    // never invoked, because we overrode actionPerformed above
066    @Override
067    public jmri.util.swing.JmriPanel makePanel() {
068        throw new IllegalArgumentException("Should not be invoked");
069    }
070
071    // initialize logging
072    private final static Logger log = LoggerFactory.getLogger(InstallDecoderFileAction.class);
073
074}