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; 015 016import org.jdom2.Document; 017import org.jdom2.Element; 018import org.jdom2.output.Format; 019import org.jdom2.output.XMLOutputter; 020import org.slf4j.Logger; 021import org.slf4j.LoggerFactory; 022 023/** 024 * Save throttles to XML 025 * 026 * @author Glen Oberhauser 027 * @author Daniel Boudreau (C) Copyright 2008 028 */ 029public class StoreXmlThrottlesLayoutAction extends AbstractAction { 030 031 /** 032 * Constructor 033 * 034 * @param s Name for the action. 035 */ 036 public StoreXmlThrottlesLayoutAction(String s) { 037 super(s); 038 // disable this ourselves if there is no throttle Manager 039 if (jmri.InstanceManager.getNullableDefault(jmri.ThrottleManager.class) == null) { 040 setEnabled(false); 041 } 042 } 043 044 public StoreXmlThrottlesLayoutAction() { 045 this("Save default throttle layout..."); 046 } 047 048 /** 049 * The action is performed. Let the user choose the file to save to. Write 050 * XML for each ThrottleFrame. 051 * 052 * @param e The event causing the action. 053 */ 054 @Override 055 public void actionPerformed(ActionEvent e) { 056 JFileChooser fileChooser = jmri.jmrit.XmlFile.userFileChooser(Bundle.getMessage("PromptXmlFileTypes"), "xml"); 057 fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); 058 fileChooser.setCurrentDirectory(new File(ThrottleFrame.getDefaultThrottleFolder())); 059 java.io.File file = StoreXmlConfigAction.getFileName(fileChooser); 060 if (file == null) { 061 return; 062 } 063 saveThrottlesLayout(file); 064 } 065 066 public void saveThrottlesLayout(java.io.File f) { 067 068 try { 069 Element root = new Element("throttle-layout-config"); 070 root.setAttribute("noNamespaceSchemaLocation", // NOI18N 071 "http://jmri.org/xml/schema/throttle-layout-config.xsd", // NOI18N 072 org.jdom2.Namespace.getNamespace("xsi", 073 "http://www.w3.org/2001/XMLSchema-instance")); // NOI18N 074 Document doc = new Document(root); 075 076 // add XSLT processing instruction 077 // <?xml-stylesheet type="text/xsl" href="XSLT/throttle-layout-config.xsl"?> 078 java.util.Map<String,String> m = new java.util.HashMap<String,String>(); 079 m.put("type", "text/xsl"); 080 m.put("href", jmri.jmrit.XmlFile.xsltLocation + "throttle-layout-config.xsl"); 081 org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m); 082 doc.addContent(0, p); 083 084 java.util.ArrayList<Element> children = new java.util.ArrayList<Element>(5); 085 086 // throttle list window 087 children.add(InstanceManager.getDefault(ThrottleFrameManager.class).getThrottlesListPanel().getXml()); 088 089 // throttle windows 090 for (Iterator<ThrottleWindow> i = InstanceManager.getDefault(ThrottleFrameManager.class).getThrottleWindows(); i.hasNext();) { 091 ThrottleWindow tw = i.next(); 092 Element throttleElement = tw.getXml(); 093 children.add(throttleElement); 094 } 095 root.setContent(children); 096 097 FileOutputStream o = new java.io.FileOutputStream(f); 098 try { 099 XMLOutputter fmt = new XMLOutputter(); 100 fmt.setFormat(Format.getPrettyFormat() 101 .setLineSeparator(System.getProperty("line.separator")) 102 .setTextMode(Format.TextMode.TRIM_FULL_WHITE)); 103 fmt.output(doc, o); 104 } catch (IOException ex) { 105 log.warn("Exception in storing throttle xml", ex); 106 } finally { 107 o.close(); 108 } 109 } catch (FileNotFoundException ex) { 110 log.warn("Exception in storing throttle xml", ex); 111 } catch (IOException ex) { 112 log.warn("Exception in storing throttle xml", ex); 113 } 114 } 115 116 // initialize logging 117 private final static Logger log = LoggerFactory.getLogger(StoreXmlThrottlesLayoutAction.class); 118 119}