001package jmri.configurexml; 002 003import java.util.Set; 004import java.awt.event.ActionEvent; 005 006import javax.swing.JFileChooser; 007 008import jmri.ConfigureManager; 009import jmri.InstanceManager; 010import jmri.jmrit.logixng.LogixNGPreferences; 011import jmri.JmriException; 012import jmri.jmrit.display.Editor; 013import jmri.jmrit.display.EditorManager; 014import jmri.jmrit.logixng.LogixNG_Manager; 015 016import org.slf4j.Logger; 017import org.slf4j.LoggerFactory; 018 019/** 020 * Load configuration information from an XML file. 021 * <p> 022 * The file context for this is the "config" file chooser. 023 * <p> 024 * This will load whatever information types are present in the file. See 025 * {@link jmri.ConfigureManager} for information on the various types of 026 * information stored in configuration files. 027 * 028 * @author Bob Jacobsen Copyright (C) 2002 029 * @see jmri.jmrit.XmlFile 030 */ 031public class LoadXmlConfigAction extends LoadStoreBaseAction { 032 033 public LoadXmlConfigAction() { 034 this("Open Data File ..."); // NOI18N 035 } 036 037 public LoadXmlConfigAction(String s) { 038 super(s); 039 } 040 041 @Override 042 public void actionPerformed(ActionEvent e) { 043 loadFile(getConfigFileChooser()); 044 } 045 046 /** 047 * 048 * @param fileChooser {@link JFileChooser} to use for file selection 049 * @return true if successful 050 */ 051 protected boolean loadFile(JFileChooser fileChooser) { 052 Set<Editor> editors = InstanceManager.getDefault(EditorManager.class).getAll(); 053 if (!editors.isEmpty()) { 054 InstanceManager.getDefault(jmri.UserPreferencesManager.class).showWarningMessage( 055 Bundle.getMessage("DuplicateLoadTitle"), Bundle.getMessage("DuplicateLoadMessage"), // NOI18N 056 "jmri.jmrit.display.EditorManager", "skipDupLoadDialog", false, true); //NOI18N 057 InstanceManager.getDefault(jmri.UserPreferencesManager.class).setPreferenceItemDetails( 058 "jmri.jmrit.display.EditorManager", "skipDupLoadDialog", Bundle.getMessage("DuplicateLoadSkip")); // NOI18N 059 } 060 061 boolean results = false; 062 java.io.File file = getFile(fileChooser); 063 if (file != null) { 064 log.info("Loading selected file: {}", file); // NOI18N 065 try { 066 ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class); 067 if (cm == null) { 068 log.error("Failed to get default configure manager"); // NOI18N 069 } else { 070 results = cm.load(file); 071 072 // If LogixNGs aren't setup, the actions and expressions will not 073 // be stored if the user stores the tables and panels. So we need 074 // to try to setup LogixNGs even if the loading failed. 075 LogixNG_Manager logixNG_Manager = InstanceManager.getDefault(LogixNG_Manager.class); 076 logixNG_Manager.setupAllLogixNGs(); 077 078 if (results) { 079 // insure logix etc fire up 080 InstanceManager.getDefault(jmri.LogixManager.class).activateAllLogixs(); 081 InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).initializeLayoutBlockPaths(); 082 083 if (InstanceManager.getDefault(LogixNGPreferences.class).getStartLogixNGOnStartup() 084 && logixNG_Manager.isStartLogixNGsOnLoad()) { 085 logixNG_Manager.activateAllLogixNGs(); 086 } 087 } 088 } 089 } catch (JmriException e) { 090 log.error("Unhandled problem in loadFile", e); // NOI18N 091 } 092 } else { 093 results = true; // We assume that as the file is null then the user has clicked cancel. 094 } 095 return results; 096 } 097 098 static public java.io.File getFile(JFileChooser fileChooser) { 099 fileChooser.setDialogType(javax.swing.JFileChooser.OPEN_DIALOG); 100 return getFileCustom(fileChooser); 101 } 102 103 static public java.io.File getFileCustom(JFileChooser fileChooser) { 104 fileChooser.rescanCurrentDirectory(); 105 int retVal = fileChooser.showDialog(null, Bundle.getMessage("MenuItemLoad")); // NOI18N 106 if (retVal != JFileChooser.APPROVE_OPTION) { 107 return null; // give up if no file selected 108 } 109 if (log.isDebugEnabled()) { 110 log.debug("Open file: {}", fileChooser.getSelectedFile().getPath()); // NOI18N 111 } 112 return fileChooser.getSelectedFile(); 113 } 114 115 // initialize logging 116 private final static Logger log = LoggerFactory.getLogger(LoadXmlConfigAction.class); 117 118}