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