001package jmri.jmrit.vsdecoder;
002
003import java.awt.GraphicsEnvironment;
004import java.awt.event.ActionEvent;
005import java.io.File;
006import javax.swing.AbstractAction;
007import javax.swing.JFileChooser;
008import javax.swing.JOptionPane;
009import javax.swing.filechooser.FileNameExtensionFilter;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Load VSDecoder Profiles from XML.
015 *
016 * <hr>
017 * This file is part of JMRI.
018 * <p>
019 * JMRI is free software; you can redistribute it and/or modify it under 
020 * the terms of version 2 of the GNU General Public License as published 
021 * by the Free Software Foundation. See the "COPYING" file for a copy
022 * of this license.
023 * <p>
024 * JMRI is distributed in the hope that it will be useful, but WITHOUT 
025 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
026 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
027 * for more details.
028 *
029 * @author Mark Underwood Copyright (C) 2011
030 */
031
032/**
033 * Load VSDecoder Profiles from XML
034 *
035 * Adapted from LoadXmlThrottleProfileAction by Glen Oberhauser (2004)
036 *
037 * @author Mark Underwood 2011
038 */
039public class LoadVSDFileAction extends AbstractAction {
040
041    /**
042     * Constructor
043     *
044     * @param s Name for the action.
045     */
046    public LoadVSDFileAction(String s) {
047        super(s);
048    }
049
050    public LoadVSDFileAction() {
051        this(Bundle.getMessage("VSDecoderFileMenuLoadVSDFile")); // File Chooser Title
052        // Shouldn't this be in the resource bundle?
053    }
054
055    JFileChooser fileChooser;
056    private String last_path = null;
057
058    /**
059     * The action is performed. Let the user choose the file to load from. Read
060     * XML for each VSDecoder Profile.
061     *
062     * @param e The event causing the action.
063     */
064    @Override
065    public void actionPerformed(ActionEvent e) {
066        if (fileChooser == null) {
067            // Need to somehow give the user a history...
068            // Must investigate JFileChooser...
069            String start_dir = VSDecoderManager.instance().getVSDecoderPreferences().getDefaultVSDFilePath();
070            if (last_path != null) {
071                start_dir = last_path;
072            }
073
074            log.debug("Using path: {}", start_dir);
075
076            fileChooser = new JFileChooser(start_dir);
077            fileChooser.setFileFilter(new FileNameExtensionFilter(Bundle.getMessage("LoadVSDFileChooserFilterLabel"), "vsd", "zip")); // NOI18N
078            fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
079            fileChooser.setCurrentDirectory(new File(start_dir));
080        }
081        int retVal = fileChooser.showOpenDialog(null);
082        if (retVal != JFileChooser.APPROVE_OPTION) {
083            return;
084            // give up if no file selected
085        }
086
087        loadVSDFile(fileChooser.getSelectedFile().toString());
088
089        // Store the last used directory
090        try {
091            last_path = fileChooser.getCurrentDirectory().getCanonicalPath();
092        } catch (java.io.IOException err) {
093            log.debug("Error getting current directory", err);
094            last_path = VSDecoderManager.instance().getVSDecoderPreferences().getDefaultVSDFilePath();
095        }
096    }
097
098    public static boolean loadVSDFile(String fp) {
099        // Check whether the file exists
100        File file = new File(fp);
101        if (!file.exists()) {
102            log.error("Cannot locate VSD File {}", fp);
103            if (!GraphicsEnvironment.isHeadless()) {
104                JOptionPane.showMessageDialog(null, "Cannot locate VSD File",
105                        Bundle.getMessage("VSDFileError"), JOptionPane.ERROR_MESSAGE);
106            }
107            return false;
108        }
109
110        // Check config.xml
111        VSDFile vsdfile;
112        try {
113            // Create a VSD (zip) file.
114            vsdfile = new VSDFile(fp);
115            log.debug("VSD File name: {}", vsdfile.getName());
116            if (vsdfile.isInitialized()) {
117                VSDecoderManager.instance().loadProfiles(vsdfile);
118            }
119            // Cleanup and close files.
120            vsdfile.close();
121
122            if (!vsdfile.isInitialized() && !GraphicsEnvironment.isHeadless()) {
123                JOptionPane.showMessageDialog(null, vsdfile.getStatusMessage(),
124                        Bundle.getMessage("VSDFileError"), JOptionPane.ERROR_MESSAGE);
125            }
126
127            return vsdfile.isInitialized();
128
129        } catch (java.util.zip.ZipException ze) {
130            log.error("ZipException opening file {}", fp, ze);
131            return false;
132        } catch (java.io.IOException ze) {
133            log.error("IOException opening file {}", fp, ze);
134            return false;
135        }
136
137        /*
138         File f = null;
139         try {
140         f = new File(fp);
141         return loadVSDFile(f);
142         } catch (java.io.IOException ioe) {
143         log.warn("IO Error auto-loading VSD File: " + (f==null?"(null)":f.getAbsolutePath()) + " ", ioe);
144         return false;
145         } catch (NullPointerException npe) {
146         log.warn("NP Error auto-loading VSD File: FP = " + fp, npe);
147         return false;
148         }
149         */
150    }
151
152    private static final Logger log = LoggerFactory.getLogger(LoadVSDFileAction.class);
153
154}