001package jmri.jmrit.vsdecoder;
002
003import java.awt.GraphicsEnvironment;
004import java.awt.event.ActionEvent;
005import java.io.File;
006
007import javax.swing.AbstractAction;
008import javax.swing.JFileChooser;
009import javax.swing.filechooser.FileNameExtensionFilter;
010
011import jmri.util.swing.JmriJOptionPane;
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    /**
056     * The action is performed. Let the user choose the file to load from. Read
057     * XML for each VSDecoder Profile.
058     *
059     * @param e The event causing the action.
060     */
061    @Override
062    public void actionPerformed(ActionEvent e) {
063        JFileChooser fileChooser;
064        String dir_external =
065                jmri.util.FileUtil.getExternalFilename(VSDecoderManager.instance().getVSDecoderPreferences().getDefaultVSDFilePath());
066
067        fileChooser = new jmri.util.swing.JmriJFileChooser(dir_external);
068        fileChooser.setFileFilter(new FileNameExtensionFilter(Bundle.getMessage("LoadVSDFileChooserFilterLabel"), "vsd", "zip")); // NOI18N
069        fileChooser.setCurrentDirectory(new File(dir_external));
070        fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
071
072        int retVal = fileChooser.showOpenDialog(null);
073        if (retVal == JFileChooser.APPROVE_OPTION) {
074            loadVSDFile(fileChooser.getSelectedFile().toString());
075        }
076    }
077
078    public static boolean loadVSDFile(String fp) {
079        // Check whether the file exists
080        String fp_external = jmri.util.FileUtil.getExternalFilename(fp);
081        File file = new File(fp_external);
082        if (!file.exists()) {
083            log.error("Cannot locate VSD File {}", fp_external);
084            if (!GraphicsEnvironment.isHeadless()) {
085                JmriJOptionPane.showMessageDialog(null, "Cannot locate VSD File",
086                        Bundle.getMessage("VSDFileError"), JmriJOptionPane.ERROR_MESSAGE);
087            }
088            return false;
089        }
090
091        // Check config.xml
092        VSDFile vsdfile;
093        try {
094            // Create a VSD (zip) file.
095            vsdfile = new VSDFile(fp_external);
096            log.debug("VSD File name: {}", vsdfile.getName());
097            if (vsdfile.isInitialized()) {
098                VSDecoderManager.instance().loadProfiles(vsdfile);
099            }
100            // Cleanup and close files.
101            vsdfile.close();
102
103            if (!vsdfile.isInitialized() && !GraphicsEnvironment.isHeadless()) {
104                JmriJOptionPane.showMessageDialog(null, vsdfile.getStatusMessage(),
105                        Bundle.getMessage("VSDFileError"), JmriJOptionPane.ERROR_MESSAGE);
106            }
107
108            return vsdfile.isInitialized();
109
110        } catch (java.util.zip.ZipException ze) {
111            log.error("ZipException opening file {}", fp, ze);
112            return false;
113        } catch (java.io.IOException ze) {
114            log.error("IOException opening file {}", fp, ze);
115            return false;
116        }
117    }
118
119    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LoadVSDFileAction.class);
120
121}