Search in sources :

Example 6 with XmlAdapter

use of jmri.configurexml.XmlAdapter in project JMRI by JMRI.

the class DefaultSignalMastManagerXml method load.

/**
     * Create a DefaultSignalMastManager
     *
     * @param shared Top level Element to unpack.
     * @param perNode Top level Element that is per-node.
     * @return true if successful
     */
@Override
public boolean load(Element shared, Element perNode) {
    // loop over contained signalmast elements
    List<Element> list = shared.getChildren("signalmast");
    for (int i = 0; i < list.size(); i++) {
        Element e = list.get(i);
        if (e.getAttribute("class") == null) {
            SignalMast m;
            String sys = getSystemName(e);
            try {
                m = InstanceManager.getDefault(jmri.SignalMastManager.class).provideSignalMast(sys);
                if (getUserName(e) != null) {
                    m.setUserName(getUserName(e));
                }
                loadCommon(m, e);
            } catch (IllegalArgumentException ex) {
                log.warn("Failed to provide SignalMast \"{}\" in load", sys);
            }
        } else {
            String adapterName = e.getAttribute("class").getValue();
            log.debug("load via " + adapterName);
            try {
                XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
                // and do it
                adapter.load(e, null);
            } catch (Exception ex) {
                log.error("Exception while loading {}: {}", e.getName(), ex, ex);
            }
        }
    }
    list = shared.getChildren("turnoutsignalmast");
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            Element e = list.get(i);
            String adapterName = e.getAttribute("class").getValue();
            log.debug("load via " + adapterName);
            try {
                XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
                // and do it
                adapter.load(e, null);
            } catch (Exception ex) {
                log.error("Exception while loading {}: {}", e.getName(), ex, ex);
            }
        }
    }
    list = shared.getChildren("virtualsignalmast");
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            Element e = list.get(i);
            String adapterName = e.getAttribute("class").getValue();
            log.debug("load via " + adapterName);
            try {
                XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
                // and do it
                adapter.load(e, null);
            } catch (Exception ex) {
                log.error("Exception while loading {}: {}", e.getName(), ex, ex);
            }
        }
    }
    list = shared.getChildren("matrixsignalmast");
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            Element e = list.get(i);
            String adapterName = e.getAttribute("class").getValue();
            log.debug("load via " + adapterName);
            try {
                XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
                // and do it
                adapter.load(e, null);
            } catch (Exception ex) {
                log.error("Exception while loading {}: {}", e.getName(), ex, ex);
            }
        }
    }
    list = shared.getChildren("dccsignalmast");
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            Element e = list.get(i);
            String adapterName = e.getAttribute("class").getValue();
            log.debug("load via " + adapterName);
            try {
                XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
                // and do it
                adapter.load(e, null);
            } catch (Exception ex) {
                log.error("Exception while loading {}: {}", e.getName(), ex, ex);
            }
        }
    }
    list = shared.getChildren("signalmastrepeater");
    if (list != null) {
        DefaultSignalMastManager m = (DefaultSignalMastManager) InstanceManager.getDefault(jmri.SignalMastManager.class);
        for (int i = 0; i < list.size(); i++) {
            Element e = list.get(i);
            String masterName = e.getChild("masterMast").getText();
            String slaveName = e.getChild("slaveMast").getText();
            SignalMastRepeater smr = new SignalMastRepeater(masterName, slaveName);
            if (e.getChild("enabled") != null && e.getChild("enabled").getText().equals("false")) {
                smr.setEnabled(false);
            }
            if (e.getChild("update") != null) {
                if (e.getChild("update").getText().equals("MasterToSlave")) {
                    smr.setDirection(SignalMastRepeater.MASTERTOSLAVE);
                } else if (e.getChild("update").getText().equals("SlaveToMaster")) {
                    smr.setDirection(SignalMastRepeater.SLAVETOMASTER);
                }
            }
            try {
                m.addRepeater(smr);
            } catch (jmri.JmriException ex) {
                log.error("Unable to add mast repeater " + masterName + " : " + slaveName);
            }
        }
        m.initialiseRepeaters();
    }
    return true;
}
Also used : Element(org.jdom2.Element) DefaultSignalMastManager(jmri.managers.DefaultSignalMastManager) DefaultSignalMastManager(jmri.managers.DefaultSignalMastManager) SignalMastRepeater(jmri.implementation.SignalMastRepeater) SignalMast(jmri.SignalMast) XmlAdapter(jmri.configurexml.XmlAdapter)

Example 7 with XmlAdapter

use of jmri.configurexml.XmlAdapter in project JMRI by JMRI.

the class StartupActionsManager method initialize.

/**
     * {@inheritDoc}
     *
     * Loads the startup action preferences and, if all required managers have
     * initialized without exceptions, performs those actions. Startup actions
     * are only performed if {@link apps.startup.StartupModel#isValid()} is true
     * for the action. It is assumed that the action has retained an Exception
     * that can be used to explain why isValid() is false.
     */
@Override
public void initialize(Profile profile) throws InitializationException {
    if (!this.isInitialized(profile)) {
        boolean perform = true;
        try {
            this.requiresNoInitializedWithExceptions(profile, Bundle.getMessage("StartupActionsManager.RefusalToInitialize"));
        } catch (InitializationException ex) {
            perform = false;
        }
        try {
            Element startup;
            try {
                startup = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(STARTUP, NAMESPACE, true));
            } catch (NullPointerException ex) {
                log.debug("Reading element from version 2.9.6 namespace...");
                startup = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(STARTUP, NAMESPACE_OLD, true));
            }
            for (Element action : startup.getChildren()) {
                // NOI18N
                String adapter = action.getAttributeValue("class");
                // NOI18N
                String name = action.getAttributeValue("name");
                String override = StartupActionModelUtil.getDefault().getOverride(name);
                if (override != null) {
                    action.setAttribute("name", override);
                    log.info("Overridding statup action class {} with {}", name, override);
                    this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsOverriddenClasses", name, override), Bundle.getMessage(Locale.ENGLISH, "StartupActionsOverriddenClasses", name, override)));
                    // after logging difference and creating error message
                    name = override;
                }
                // NOI18N
                String type = action.getAttributeValue("type");
                log.debug("Read {} {} adapter {}", type, name, adapter);
                try {
                    log.debug("Creating {} {} adapter {}...", type, name, adapter);
                    // no perNode preferences
                    ((XmlAdapter) Class.forName(adapter).newInstance()).load(action, null);
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
                    log.error("Unable to create {} for {}", adapter, action, ex);
                    this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsCreationError", adapter, name), // NOI18N
                    Bundle.getMessage("StartupActionsCreationError", adapter, name)));
                } catch (Exception ex) {
                    log.error("Unable to load {} into {}", action, adapter, ex);
                    this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsLoadError", adapter, name), // NOI18N
                    Bundle.getMessage("StartupActionsLoadError", adapter, name)));
                }
            }
        } catch (NullPointerException ex) {
            // ignore - this indicates migration has not occured
            log.debug("No element to read");
        }
        if (perform) {
            this.actions.stream().filter((action) -> (action.isValid())).forEachOrdered((action) -> {
                try {
                    action.performAction();
                } catch (JmriException ex) {
                    this.addInitializationException(profile, ex);
                }
            });
        }
        this.isDirty = false;
        this.restartRequired = false;
        this.setInitialized(profile, true);
        List<Exception> exceptions = this.getInitializationExceptions(profile);
        if (exceptions.size() == 1) {
            throw new InitializationException(exceptions.get(0));
        } else if (exceptions.size() > 1) {
            throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsMultipleErrors"), // NOI18N
            Bundle.getMessage("StartupActionsMultipleErrors"));
        }
    }
}
Also used : ConfigXmlManager(jmri.configurexml.ConfigXmlManager) StartupModel(apps.startup.StartupModel) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) InitializationException(jmri.util.prefs.InitializationException) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) RosterConfigManager(jmri.jmrit.roster.RosterConfigManager) JDOMUtil(jmri.util.jdom.JDOMUtil) ProgrammerConfigManager(jmri.jmrit.symbolicprog.ProgrammerConfigManager) ProfileUtils(jmri.profile.ProfileUtils) Locale(java.util.Locale) Profile(jmri.profile.Profile) XmlAdapter(jmri.configurexml.XmlAdapter) Logger(org.slf4j.Logger) FileLocationsPreferences(jmri.implementation.FileLocationsPreferences) GuiLafPreferencesManager(apps.gui.GuiLafPreferencesManager) JmriException(jmri.JmriException) WarrantPreferences(jmri.jmrit.logix.WarrantPreferences) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) StartupActionModelUtil(apps.startup.StartupActionModelUtil) List(java.util.List) ManagerDefaultSelector(jmri.managers.ManagerDefaultSelector) PreferencesManager(jmri.spi.PreferencesManager) StartupModelFactory(apps.startup.StartupModelFactory) Element(org.jdom2.Element) Element(org.jdom2.Element) JmriException(jmri.JmriException) InitializationException(jmri.util.prefs.InitializationException) InitializationException(jmri.util.prefs.InitializationException) JDOMException(org.jdom2.JDOMException) JmriException(jmri.JmriException) XmlAdapter(jmri.configurexml.XmlAdapter)

Example 8 with XmlAdapter

use of jmri.configurexml.XmlAdapter in project JMRI by JMRI.

the class PanelEditorXml method load.

/**
     * Create a PanelEditor object, then register and fill it, then pop it in a
     * JFrame
     *
     * @param shared Top level Element to unpack.
     * @return true if successful
     */
@Override
public boolean load(Element shared, Element perNode) {
    boolean result = true;
    // find coordinates
    int x = 0;
    int y = 0;
    int height = 400;
    int width = 300;
    try {
        x = shared.getAttribute("x").getIntValue();
        y = shared.getAttribute("y").getIntValue();
        height = shared.getAttribute("height").getIntValue();
        width = shared.getAttribute("width").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert PanelEditor's attribute");
        result = false;
    }
    // find the name
    String name = "Panel";
    if (shared.getAttribute("name") != null) {
        name = shared.getAttribute("name").getValue();
    }
    // confirm that panel hasn't already been loaded
    if (jmri.jmrit.display.PanelMenu.instance().isPanelNameUsed(name)) {
        log.warn("File contains a panel with the same name (" + name + ") as an existing panel");
        result = false;
    }
    PanelEditor panel = new PanelEditor(name);
    //panel.makeFrame(name);
    jmri.jmrit.display.PanelMenu.instance().addEditorPanel(panel);
    panel.getTargetFrame().setLocation(x, y);
    panel.getTargetFrame().setSize(width, height);
    panel.setTitle();
    // Load editor option flags. This has to be done before the content 
    // items are loaded, to preserve the individual item settings
    Attribute a;
    boolean value = true;
    if ((a = shared.getAttribute("editable")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllEditable(value);
    value = true;
    if ((a = shared.getAttribute("positionable")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllPositionable(value);
    /*
         value = false;
         if ((a = element.getAttribute("showcoordinates"))!=null && a.getValue().equals("yes"))
         value = true;
         panel.setShowCoordinates(value);
         */
    value = true;
    if ((a = shared.getAttribute("showtooltips")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllShowTooltip(value);
    value = true;
    if ((a = shared.getAttribute("controlling")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setAllControlling(value);
    value = false;
    if ((a = shared.getAttribute("hide")) != null && a.getValue().equals("yes")) {
        value = true;
    }
    panel.setShowHidden(value);
    value = true;
    if ((a = shared.getAttribute("panelmenu")) != null && a.getValue().equals("no")) {
        value = false;
    }
    panel.setPanelMenuVisible(value);
    String state = "both";
    if ((a = shared.getAttribute("scrollable")) != null) {
        state = a.getValue();
    }
    panel.setScroll(state);
    // set color if needed
    try {
        int red = shared.getAttribute("redBackground").getIntValue();
        int blue = shared.getAttribute("blueBackground").getIntValue();
        int green = shared.getAttribute("greenBackground").getIntValue();
        panel.setBackgroundColor(new Color(red, green, blue));
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse color attributes!");
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    //set the (global) editor display widgets to their flag settings
    panel.initView();
    // load the contents with their individual option settings
    List<Element> items = shared.getChildren();
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        String adapterName = item.getAttribute("class").getValue();
        log.debug("load via " + adapterName);
        try {
            XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
            // and do it
            adapter.load(item, panel);
            if (!panel.loadOK()) {
                result = false;
            }
        } catch (Exception e) {
            log.error("Exception while loading " + item.getName() + ":" + e);
            result = false;
            e.printStackTrace();
        }
    }
    // dispose of url correction data
    panel.disposeLoadData();
    // display the results, with the editor in back
    panel.pack();
    panel.setAllEditable(panel.isEditable());
    // we don't pack the target frame here, because size was specified
    // TODO: Work out why, when calling this method, panel size is increased
    // vertically (at least on MS Windows)
    // always show the panel
    panel.getTargetFrame().setVisible(true);
    // register the resulting panel for later configuration
    ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
    if (cm != null) {
        cm.registerUser(panel);
    }
    // reset the size and position, in case the display caused it to change
    panel.getTargetFrame().setLocation(x, y);
    panel.getTargetFrame().setSize(width, height);
    return result;
}
Also used : Attribute(org.jdom2.Attribute) Color(java.awt.Color) Element(org.jdom2.Element) PanelEditor(jmri.jmrit.display.panelEditor.PanelEditor) Point(java.awt.Point) ConfigureManager(jmri.ConfigureManager) AbstractXmlAdapter(jmri.configurexml.AbstractXmlAdapter) XmlAdapter(jmri.configurexml.XmlAdapter)

Example 9 with XmlAdapter

use of jmri.configurexml.XmlAdapter in project JMRI by JMRI.

the class JmrixConfigPaneXml method store.

/**
     * Forward to the configurexml class for the specific object type.
     */
@Override
public Element store(Object o) {
    ConnectionConfig oprime = ((JmrixConfigPane) o).getCurrentObject();
    if (oprime == null) {
        return null;
    }
    String adapter = ConfigXmlManager.adapterName(oprime);
    log.debug("forward to " + adapter);
    try {
        XmlAdapter x = (XmlAdapter) Class.forName(adapter).newInstance();
        return x.store(oprime);
    } catch (Exception e) {
        log.error("Exception: " + e);
        e.printStackTrace();
        return null;
    }
}
Also used : JmrixConfigPane(jmri.jmrix.JmrixConfigPane) ConnectionConfig(jmri.jmrix.ConnectionConfig) AbstractXmlAdapter(jmri.configurexml.AbstractXmlAdapter) XmlAdapter(jmri.configurexml.XmlAdapter) UnsupportedLookAndFeelException(javax.swing.UnsupportedLookAndFeelException)

Example 10 with XmlAdapter

use of jmri.configurexml.XmlAdapter in project JMRI by JMRI.

the class AbstractSignalHeadManagerXml method loadSignalHeads.

/**
     * Utility method to load the individual SignalHead objects. If there's no
     * additional info needed for a specific signal head type, invoke this with
     * the parent of the set of SignalHead elements.
     *
     * @param shared  Element containing the SignalHead elements to load.
     * @param perNode Element containing any per-node information associated
     *                with the shared Element.
     */
public void loadSignalHeads(Element shared, Element perNode) {
    InstanceManager.getDefault(jmri.SignalHeadManager.class);
    // load the contents
    List<Element> items = shared.getChildren();
    if (log.isDebugEnabled()) {
        log.debug("Found " + items.size() + " signal heads");
    }
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        String adapterName = item.getAttribute("class").getValue();
        log.debug("load via " + adapterName);
        try {
            XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
            // and do it
            adapter.load(item, null);
        } catch (Exception e) {
            log.error("Exception while loading {}: {}", item.getName(), e, e);
        }
    }
}
Also used : Element(org.jdom2.Element) XmlAdapter(jmri.configurexml.XmlAdapter)

Aggregations

XmlAdapter (jmri.configurexml.XmlAdapter)10 Element (org.jdom2.Element)9 AbstractXmlAdapter (jmri.configurexml.AbstractXmlAdapter)5 Color (java.awt.Color)4 ConfigureManager (jmri.ConfigureManager)4 Attribute (org.jdom2.Attribute)4 Point (java.awt.Point)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 InitializationException (jmri.util.prefs.InitializationException)2 JDOMException (org.jdom2.JDOMException)2 GuiLafPreferencesManager (apps.gui.GuiLafPreferencesManager)1 StartupActionModelUtil (apps.startup.StartupActionModelUtil)1 StartupModel (apps.startup.StartupModel)1 StartupModelFactory (apps.startup.StartupModelFactory)1 List (java.util.List)1 Locale (java.util.Locale)1 ServiceLoader (java.util.ServiceLoader)1 Set (java.util.Set)1 JComponent (javax.swing.JComponent)1