001package jmri.jmrit.display.logixng.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalActionManager;
006import jmri.jmrit.logixng.NamedBeanAddressing;
007import jmri.jmrit.display.logixng.WindowManagement;
008import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
009import jmri.jmrit.logixng.util.parser.ParserException;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for WindowManagement objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2024
018 */
019public class WindowManagementXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public WindowManagementXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a WindowManagement
026     *
027     * @param o Object to store, of type TripleTurnoutSignalHead
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        WindowManagement p = (WindowManagement) o;
033
034        var selectEnumHideOrShowXml = new LogixNG_SelectEnumXml<WindowManagement.HideOrShow>();
035        var selectEnumMaximizeMinimizeNormalizeXml = new LogixNG_SelectEnumXml<WindowManagement.MaximizeMinimizeNormalize>();
036        var selectEnumBringToFrontOrBackXml = new LogixNG_SelectEnumXml<WindowManagement.BringToFrontOrBack>();
037
038        Element element = new Element("DisplayActionWindowManagement");
039        element.setAttribute("class", this.getClass().getName());
040        element.addContent(new Element("systemName").addContent(p.getSystemName()));
041
042        storeCommon(p, element);
043
044        String jmriJFrameTitle = p.getJmriJFrameTitle();
045        if (jmriJFrameTitle != null) {
046            element.addContent(new Element("jmriJFrameTitle").addContent(jmriJFrameTitle));
047        }
048
049        element.addContent(new Element("addressing").addContent(p.getAddressing().name()));
050        element.addContent(new Element("reference").addContent(p.getReference()));
051        element.addContent(new Element("localVariable").addContent(p.getLocalVariable()));
052        element.addContent(new Element("formula").addContent(p.getFormula()));
053
054        element.addContent(new Element("ignoreWindowNotFound")
055                .addContent(p.isIgnoreWindowNotFound()? "yes" : "no"));  // NOI18N
056
057        element.addContent(selectEnumHideOrShowXml.store(
058                p.getSelectEnumHideOrShow(), "hideOrShow"));
059
060        element.addContent(selectEnumMaximizeMinimizeNormalizeXml.store(
061                p.getSelectEnumMaximizeMinimizeNormalize(), "maximizeMinimizeNormalize"));
062
063        element.addContent(selectEnumBringToFrontOrBackXml.store(
064                p.getSelectEnumBringToFrontOrBack(), "bringToFrontOrBack"));
065
066        return element;
067    }
068
069    @Override
070    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
071        String sys = getSystemName(shared);
072        String uname = getUserName(shared);
073        WindowManagement h = new WindowManagement(sys, uname);
074
075        var selectEnumHideOrShowXml = new LogixNG_SelectEnumXml<WindowManagement.HideOrShow>();
076        var selectEnumMaximizeMinimizeNormalizeXml = new LogixNG_SelectEnumXml<WindowManagement.MaximizeMinimizeNormalize>();
077        var selectEnumBringToFrontOrBackXml = new LogixNG_SelectEnumXml<WindowManagement.BringToFrontOrBack>();
078
079        loadCommon(h, shared);
080
081        Element elem = shared.getChild("jmriJFrameTitle");
082        if (elem != null) {
083            h.setJmriJFrame(elem.getTextTrim());
084        }
085
086        try {
087            elem = shared.getChild("addressing");
088            if (elem != null) {
089                h.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
090            }
091
092            elem = shared.getChild("reference");
093            if (elem != null) h.setReference(elem.getTextTrim());
094
095            elem = shared.getChild("localVariable");
096            if (elem != null) h.setLocalVariable(elem.getTextTrim());
097
098            elem = shared.getChild("formula");
099            if (elem != null) h.setFormula(elem.getTextTrim());
100
101        } catch (ParserException e) {
102            throw new JmriConfigureXmlException(e);
103        }
104
105        String ignoreWindowNotFound = "no";
106        Element element = shared.getChild("ignoreWindowNotFound");
107        if (element != null) {
108            ignoreWindowNotFound = element.getTextTrim();  // NOI18N
109        }
110        h.setIgnoreWindowNotFound("yes".equals(ignoreWindowNotFound));
111
112        selectEnumHideOrShowXml.load(shared.getChild("hideOrShow"),
113                h.getSelectEnumHideOrShow());
114
115        selectEnumMaximizeMinimizeNormalizeXml.load(
116                shared.getChild("maximizeMinimizeNormalize"),
117                h.getSelectEnumMaximizeMinimizeNormalize());
118
119        Element bringToFrontOrBack = shared.getChild("bringToFrontOrBack");
120        if (bringToFrontOrBack != null) {
121            selectEnumBringToFrontOrBackXml.load(bringToFrontOrBack,
122                    h.getSelectEnumBringToFrontOrBack());
123        } else {
124            // Handle pre JMRI 5.7.6
125            h.getSelectEnumBringToFrontOrBack().setEnum(
126                    WindowManagement.BringToFrontOrBack.Front);
127        }
128
129        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
130        return true;
131    }
132
133//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WindowToFrontXml.class);
134}