001package jmri.configurexml; 002 003import java.awt.event.ActionEvent; 004import java.io.File; 005import java.util.ResourceBundle; 006 007import javax.annotation.CheckForNull; 008import javax.swing.JFileChooser; 009 010import jmri.ConfigureManager; 011import jmri.InstanceManager; 012import jmri.util.swing.JmriJOptionPane; 013 014/** 015 * Store the JMRI configuration information as XML. 016 * <p> 017 * Note that this does not store preferences, tools or user information in the 018 * file. This is not a complete store! See {@link jmri.ConfigureManager} for 019 * information on the various types of information stored in configuration 020 * files. 021 * 022 * @author Bob Jacobsen Copyright (C) 2002 023 * @see jmri.jmrit.XmlFile 024 */ 025public class StoreXmlConfigAction extends LoadStoreBaseAction { 026 027 static final ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrit.display.DisplayBundle"); 028 029 public StoreXmlConfigAction() { 030 this("Store configuration..."); 031 } 032 033 public StoreXmlConfigAction(String s) { 034 super(s); 035 } 036 037 static public File getFileName(JFileChooser fileChooser) { 038 fileChooser.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG); 039 return getFileCustom(fileChooser); 040 } 041 042 /** 043 * Do the filename handling: 044 * <ol> 045 * <li>rescan directory to see any new files 046 * <li>Prompt user to select a file 047 * <li>adds .xml extension if needed 048 * <li>if that file exists, check with user 049 * </ol> 050 * 051 * @param fileChooser the file chooser to use 052 * @return the file to store or null if the user declined to store a file 053 */ 054 @CheckForNull 055 public static File getFileCustom(JFileChooser fileChooser) { 056 fileChooser.rescanCurrentDirectory(); 057 int retVal = fileChooser.showDialog(null, Bundle.getMessage("MenuItemStore")); 058 if (retVal != JFileChooser.APPROVE_OPTION) { 059 return null; // give up if no file selected 060 } 061 File file = fileChooser.getSelectedFile(); 062 if (fileChooser.getFileFilter() != fileChooser.getAcceptAllFileFilter()) { 063 // append .xml to file name if needed 064 String fileName = file.getAbsolutePath(); 065 String fileNameLC = fileName.toLowerCase(); 066 if (!fileNameLC.endsWith(".xml")) { 067 fileName = fileName + ".xml"; 068 file = new File(fileName); 069 } 070 } 071 log.debug("Save file: {}", file.getPath()); 072 // check for possible overwrite 073 if (file.exists()) { 074 int selectedValue = JmriJOptionPane.showConfirmDialog(null, 075 Bundle.getMessage("FileOverwriteWarning", file.getName()), 076 Bundle.getMessage("OverwriteFile"), 077 JmriJOptionPane.OK_CANCEL_OPTION); 078 if (selectedValue != JmriJOptionPane.OK_OPTION) { 079 return null; 080 } 081 } 082 return file; 083 } 084 085 @Override 086 public void actionPerformed(ActionEvent e) { 087 File file = getFileName(getConfigFileChooser()); 088 if (file == null) { 089 return; 090 } 091 092 // and finally store 093 ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class); 094 if (cm == null) { 095 log.error("Failed to get default configure manager"); 096 } else { 097 boolean results = cm.storeConfig(file); 098 log.debug("store {}", results ? "was successful" : "failed"); 099 if (!results) { 100 JmriJOptionPane.showMessageDialog(null, 101 rb.getString("StoreHasErrors") + "\n" 102 + rb.getString("StoreIncomplete") + "\n" 103 + rb.getString("ConsoleWindowHasInfo"), 104 rb.getString("StoreError"), JmriJOptionPane.ERROR_MESSAGE); 105 } 106 } 107 } 108 109 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StoreXmlConfigAction.class); 110 111}