001package jmri.jmrit.throttle;
002
003import java.awt.event.ActionEvent;
004import java.io.File;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.util.Iterator;
009
010import javax.swing.AbstractAction;
011import javax.swing.JFileChooser;
012
013import jmri.InstanceManager;
014import jmri.configurexml.StoreXmlConfigAction;
015import jmri.jmrit.throttle.implementation.ThrottleUICore;
016import jmri.jmrit.throttle.interfaces.ThrottleControllersUIContainer;
017
018import org.jdom2.Document;
019import org.jdom2.Element;
020import org.jdom2.output.Format;
021import org.jdom2.output.XMLOutputter;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025/**
026 * Save JMRI throttle window layout to XML
027 * 
028 * <hr>
029 * This file is part of JMRI.
030 * <p>
031 * JMRI is free software; you can redistribute it and/or modify it under the
032 * terms of version 2 of the GNU General Public License as published by the Free
033 * Software Foundation. See the "COPYING" file for a copy of this license.
034 * <p>
035 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
036 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
037 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
038 *
039 * @author Glen Oberhauser
040 * @author Daniel Boudreau (C) Copyright 2008
041 */
042public class StoreXmlThrottlesLayoutAction extends AbstractAction {
043
044    /**
045     * Constructor
046     *
047     * @param s Name for the action.
048     */
049    public StoreXmlThrottlesLayoutAction(String s) {
050        super(s);
051        // disable this ourselves if there is no throttle Manager
052        if (jmri.InstanceManager.getNullableDefault(jmri.ThrottleManager.class) == null) {
053            setEnabled(false);
054        }
055    }
056
057    public StoreXmlThrottlesLayoutAction() {
058        this("Save default throttle layout...");
059    }
060
061    /**
062     * The action is performed. Let the user choose the file to save to. Write
063     * XML for each ThrottleFrame.
064     *
065     * @param e The event causing the action.
066     */
067    @Override
068    public void actionPerformed(ActionEvent e) {
069        JFileChooser fileChooser = jmri.jmrit.XmlFile.userFileChooser(Bundle.getMessage("PromptXmlFileTypes"), "xml");
070        fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
071        fileChooser.setCurrentDirectory(new File(ThrottleUICore.getDefaultThrottleFolder()));
072        java.io.File file = StoreXmlConfigAction.getFileName(fileChooser);
073        if (file == null) {
074            return;
075        }
076        saveThrottlesLayout(file);
077    }
078
079    public void saveThrottlesLayout(java.io.File f) {
080
081        try {
082            Element root = new Element("throttle-layout-config");
083            root.setAttribute("noNamespaceSchemaLocation",  // NOI18N
084                    "http://jmri.org/xml/schema/throttle-layout-config.xsd",  // NOI18N
085                    org.jdom2.Namespace.getNamespace("xsi",
086                            "http://www.w3.org/2001/XMLSchema-instance"));  // NOI18N
087            Document doc = new Document(root);
088
089            // add XSLT processing instruction
090            // <?xml-stylesheet type="text/xsl" href="XSLT/throttle-layout-config.xsl"?>
091            java.util.Map<String,String> m = new java.util.HashMap<String,String>();
092            m.put("type", "text/xsl");
093            m.put("href", jmri.jmrit.XmlFile.xsltLocation + "throttle-layout-config.xsl");
094            org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
095            doc.addContent(0, p);
096
097            java.util.ArrayList<Element> children = new java.util.ArrayList<Element>(5);
098
099            // throttle list window
100            children.add(InstanceManager.getDefault(ThrottleFrameManager.class).getThrottlesListPanel().getXml());
101
102            // throttle windows
103            for (Iterator<ThrottleControllersUIContainer> i = InstanceManager.getDefault(ThrottleFrameManager.class).iterator(); i.hasNext();) {
104                ThrottleControllersUIContainer tw = i.next();
105                if (tw instanceof ThrottleWindow) { // only save ThrottleWindows
106                    Element throttleElement = ((ThrottleWindow)tw).getXml();
107                    children.add(throttleElement);
108                }
109            }
110            root.setContent(children);
111
112            FileOutputStream o = new java.io.FileOutputStream(f);
113            try {
114                XMLOutputter fmt = new XMLOutputter();
115                fmt.setFormat(Format.getPrettyFormat()
116                        .setLineSeparator(System.getProperty("line.separator"))
117                        .setTextMode(Format.TextMode.TRIM_FULL_WHITE));
118                fmt.output(doc, o);
119            } catch (IOException ex) {
120                log.warn("Exception in storing throttle xml", ex);
121            } finally {
122                o.close();
123            }
124        } catch (FileNotFoundException ex) {
125            log.warn("Exception in storing throttle xml", ex);
126        } catch (IOException ex) {
127            log.warn("Exception in storing throttle xml", ex);
128        }
129    }
130
131    // initialize logging
132    private static final Logger log = LoggerFactory.getLogger(StoreXmlThrottlesLayoutAction.class);
133
134}