Search in sources :

Example 1 with SignalSystemManager

use of jmri.SignalSystemManager in project JMRI by JMRI.

the class AddSignalMastPanel method loadMastDefinitions.

void loadMastDefinitions() {
    // need to remove itemListener before addItem() or item event will occur
    if (mastBox.getItemListeners().length > 0) {
        // should this be a while loop?
        mastBox.removeItemListener(mastBox.getItemListeners()[0]);
    }
    mastBox.removeAllItems();
    try {
        mastNames = new ArrayList<File>();
        SignalSystemManager man = InstanceManager.getDefault(jmri.SignalSystemManager.class);
        // get the signals system name from the user name in combo box
        String u = (String) sigSysBox.getSelectedItem();
        sigsysname = man.getByUserName(u).getSystemName();
        map = new HashMap<String, Integer>();
        // do file IO to get all the appearances
        // gather all the appearance files
        //Look for the default system defined ones first
        URL path = FileUtil.findURL("xml/signals/" + sigsysname, FileUtil.Location.INSTALLED);
        if (path != null) {
            File[] apps = new File(path.toURI()).listFiles();
            for (File app : apps) {
                if (app.getName().startsWith("appearance") && app.getName().endsWith(".xml")) {
                    log.debug("   found file: " + app.getName());
                    // load it and get name
                    mastNames.add(app);
                    jmri.jmrit.XmlFile xf = new jmri.jmrit.XmlFile() {
                    };
                    Element root = xf.rootFromFile(app);
                    String name = root.getChild("name").getText();
                    mastBox.addItem(name);
                    map.put(name, root.getChild("appearances").getChild("appearance").getChildren("show").size());
                }
            }
        }
    } catch (org.jdom2.JDOMException e) {
        mastBox.addItem("Failed to create definition, did you select a system?");
        log.warn("in loadMastDefinitions", e);
    } catch (java.io.IOException | URISyntaxException e) {
        mastBox.addItem("Failed to read definition, did you select a system?");
        log.warn("in loadMastDefinitions", e);
    }
    try {
        URL path = FileUtil.findURL("signals/" + sigsysname, FileUtil.Location.USER, "xml", "resources");
        if (path != null) {
            File[] apps = new File(path.toURI()).listFiles();
            for (File app : apps) {
                if (app.getName().startsWith("appearance") && app.getName().endsWith(".xml")) {
                    log.debug("   found file: " + app.getName());
                    // If the mast file name already exists no point in re-adding it
                    if (!mastNames.contains(app)) {
                        mastNames.add(app);
                        jmri.jmrit.XmlFile xf = new jmri.jmrit.XmlFile() {
                        };
                        Element root = xf.rootFromFile(app);
                        String name = root.getChild("name").getText();
                        //if the mast name already exist no point in readding it.
                        if (!map.containsKey(name)) {
                            mastBox.addItem(name);
                            map.put(name, root.getChild("appearances").getChild("appearance").getChildren("show").size());
                        }
                    }
                }
            }
        }
    } catch (org.jdom2.JDOMException | java.io.IOException | URISyntaxException e) {
        log.warn("in loadMastDefinitions", e);
    }
    mastBox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            updateSelectedDriver();
        }
    });
    updateSelectedDriver();
    if (prefs.getComboBoxLastSelection(mastSelectionCombo + ":" + ((String) sigSysBox.getSelectedItem())) != null) {
        mastBox.setSelectedItem(prefs.getComboBoxLastSelection(mastSelectionCombo + ":" + ((String) sigSysBox.getSelectedItem())));
    }
}
Also used : ItemEvent(java.awt.event.ItemEvent) Element(org.jdom2.Element) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) SignalSystemManager(jmri.SignalSystemManager) ItemListener(java.awt.event.ItemListener) File(java.io.File)

Aggregations

ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 SignalSystemManager (jmri.SignalSystemManager)1 Element (org.jdom2.Element)1