001package jmri.jmrit.display.panelEditor.configurexml;
002
003import java.awt.Color;
004import java.awt.Dimension;
005import java.awt.Point;
006import java.util.List;
007
008import javax.swing.JFrame;
009
010import org.jdom2.Attribute;
011import org.jdom2.Element;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015import jmri.ConfigureManager;
016import jmri.InstanceManager;
017import jmri.configurexml.AbstractXmlAdapter;
018import jmri.configurexml.XmlAdapter;
019import jmri.jmrit.display.EditorManager;
020import jmri.jmrit.display.Positionable;
021import jmri.jmrit.display.panelEditor.PanelEditor;
022
023/**
024 * Handle configuration for {@link PanelEditor} panes.
025 *
026 * @author Bob Jacobsen Copyright: Copyright (c) 2002
027 */
028public class PanelEditorXml extends AbstractXmlAdapter {
029
030    public PanelEditorXml() {
031    }
032
033    /**
034     * Default implementation for storing the contents of a PanelEditor
035     *
036     * @param o Object to store, of type PanelEditor
037     * @return Element containing the complete info
038     */
039    @Override
040    public Element store(Object o) {
041        PanelEditor p = (PanelEditor) o;
042        Element panel = new Element("paneleditor");
043
044        JFrame frame = p.getTargetFrame();
045        Dimension size = frame.getSize();
046        Point posn = frame.getLocation();
047
048        panel.setAttribute("class", "jmri.jmrit.display.panelEditor.configurexml.PanelEditorXml");
049        panel.setAttribute("name", "" + frame.getTitle());
050        panel.setAttribute("x", "" + posn.x);
051        panel.setAttribute("y", "" + posn.y);
052        panel.setAttribute("height", "" + size.height);
053        panel.setAttribute("width", "" + size.width);
054        panel.setAttribute("editable", "" + (p.isEditable() ? "yes" : "no"));
055        panel.setAttribute("positionable", "" + (p.allPositionable() ? "yes" : "no"));
056        //panel.setAttribute("showcoordinates", ""+(p.showCoordinates()?"yes":"no"));
057        panel.setAttribute("showtooltips", "" + (p.showToolTip() ? "yes" : "no"));
058        panel.setAttribute("controlling", "" + (p.allControlling() ? "yes" : "no"));
059        panel.setAttribute("hide", p.showHidden() ? "no" : "yes");  // hide=no means showHidden enabled
060        panel.setAttribute("panelmenu", p.isPanelMenuVisible() ? "yes" : "no");
061        panel.setAttribute("scrollable", p.getScrollable());
062        if (p.getBackgroundColor() != null) {
063            panel.setAttribute("redBackground", "" + p.getBackgroundColor().getRed());
064            panel.setAttribute("greenBackground", "" + p.getBackgroundColor().getGreen());
065            panel.setAttribute("blueBackground", "" + p.getBackgroundColor().getBlue());
066        }
067
068        // include contents
069        List<Positionable> contents = p.getContents();
070        if (log.isDebugEnabled()) {
071            log.debug("N elements: {}", contents.size());
072        }
073        for (Positionable sub : contents) {
074            if (sub != null && sub.storeItem()) {
075                try {
076                    Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
077                    if (e != null) {
078                        panel.addContent(e);
079                    }
080                } catch (RuntimeException e) {
081                    log.error("Error storing panel element", e);
082                }
083            }
084        }
085
086        return panel;
087    }
088
089    /**
090     * Create a PanelEditor object, then register and fill it, then pop it in a
091     * JFrame.
092     *
093     * @param shared Top level Element to unpack.
094     * @return true if successful
095     */
096    @Override
097    public boolean load(Element shared, Element perNode) {
098        if (java.awt.GraphicsEnvironment.isHeadless()) {
099            return true;
100        }
101
102        boolean result = true;
103        Attribute a;
104        // find coordinates
105        int x = 0;
106        int y = 0;
107        int height = 400;
108        int width = 300;
109        try {
110            if ((a = shared.getAttribute("x")) != null) {
111                x = a.getIntValue();
112            }
113            if ((a = shared.getAttribute("y")) != null) {
114                y = a.getIntValue();
115            }
116            if ((a = shared.getAttribute("height")) != null) {
117                height = a.getIntValue();
118            }
119            if ((a = shared.getAttribute("width")) != null) {
120                width = a.getIntValue();
121            }
122        } catch (org.jdom2.DataConversionException e) {
123            log.error("failed to convert PanelEditor's attribute");
124            result = false;
125        }
126        // find the name
127        String name = "Panel";
128        if (shared.getAttribute("name") != null) {
129            name = shared.getAttribute("name").getValue();
130        }
131        // confirm that panel hasn't already been loaded
132        if (InstanceManager.getDefault(EditorManager.class).contains(name)) {
133            log.warn("File contains a panel with the same name ({}) as an existing panel", name);
134            result = false;
135        }
136
137        // If available, override location and size with machine dependent values
138        if (!InstanceManager.getDefault(jmri.util.gui.GuiLafPreferencesManager.class).isEditorUseOldLocSize()) {
139            jmri.UserPreferencesManager prefsMgr = InstanceManager.getNullableDefault(jmri.UserPreferencesManager.class);
140            if (prefsMgr != null) {
141
142                java.awt.Point prefsWindowLocation = prefsMgr.getWindowLocation(name);
143                if (prefsWindowLocation != null) {
144                    x = (int) prefsWindowLocation.getX();
145                    y = (int) prefsWindowLocation.getY();
146                }
147
148                java.awt.Dimension prefsWindowSize = prefsMgr.getWindowSize(name);
149                if (prefsWindowSize != null && prefsWindowSize.getHeight() != 0 && prefsWindowSize.getWidth() != 0) {
150                    height = (int) prefsWindowSize.getHeight();
151                    width = (int) prefsWindowSize.getWidth();
152                }
153            }
154        }
155
156        PanelEditor panel = new PanelEditor(name);
157        panel.setTitle();
158        panel.getTargetFrame().setLocation(x, y);
159        panel.getTargetFrame().setSize(width, height);
160        //Panel already added to EditorManager with new PanelEditor(name)
161//        InstanceManager.getDefault(EditorManager.class).add(panel);
162
163        // Load editor option flags. This has to be done before the content
164        // items are loaded, to preserve the individual item settings
165        panel.setAllEditable(!shared.getAttributeValue("editable","yes").equals("no"));
166        panel.setAllPositionable(!shared.getAttributeValue("positionable","yes").equals("no"));
167        //panel.setShowCoordinates(shared.getAttributeValue("showcoordinates","no").equals("yes"));
168        panel.setAllShowToolTip(!shared.getAttributeValue("showtooltips","yes").equals("no"));
169        panel.setAllControlling(!shared.getAttributeValue("controlling", "yes").equals("no"));
170        panel.setShowHidden(!shared.getAttributeValue("hide","no").equals("yes"));
171        panel.setPanelMenuVisible(!shared.getAttributeValue("panelmenu","yes").equals("no"));
172        panel.setScroll(shared.getAttributeValue("scrollable","both"));
173
174        // set color if needed
175        try {
176            int red = shared.getAttribute("redBackground").getIntValue();
177            int blue = shared.getAttribute("blueBackground").getIntValue();
178            int green = shared.getAttribute("greenBackground").getIntValue();
179            panel.setBackgroundColor(new Color(red, green, blue));
180        } catch (org.jdom2.DataConversionException e) {
181            log.warn("Could not parse color attributes!");
182        } catch (NullPointerException e) {  // considered normal if the attributes are not present
183        }
184        //set the (global) editor display widgets to their flag settings
185        panel.initView();
186
187        // load the contents with their individual option settings
188        List<Element> panelItems = shared.getChildren();
189        for (Element item : panelItems) {
190            // get the class, hence the adapter object to do loading
191            String adapterName = item.getAttribute("class").getValue();
192            log.debug("load via {}", adapterName);
193            try {
194                XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).getDeclaredConstructor().newInstance();
195                // and do it
196                adapter.load(item, panel);
197                if (!panel.loadOK()) {
198                    result = false;
199                }
200            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException
201                    | jmri.configurexml.JmriConfigureXmlException | java.lang.reflect.InvocationTargetException e) {
202                log.error("Exception while loading {}", item.getName(), e);
203                result = false;
204            }
205        }
206        panel.disposeLoadData();     // dispose of url correction data
207
208        // display the results, with the editor in back
209        panel.pack();
210        panel.setAllEditable(panel.isEditable());
211
212        // we don't pack the target frame here, because size was specified
213        // TODO: Work out why, when calling this method, panel size is increased
214        // vertically (at least on MS Windows)
215        panel.getTargetFrame().setVisible(true);    // always show the panel
216
217        // register the resulting panel for later configuration
218        ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
219        if (cm != null) {
220            cm.registerUser(panel);
221        }
222
223        // reset the size and position, in case the display caused it to change
224        panel.getTargetFrame().setLocation(x, y);
225        panel.getTargetFrame().setSize(width, height);
226        return result;
227    }
228
229    @Override
230    public int loadOrder() {
231        return jmri.Manager.PANELFILES;
232    }
233
234    private final static Logger log = LoggerFactory.getLogger(PanelEditorXml.class);
235
236}