Search in sources :

Example 1 with PanelEditor

use of jmri.jmrit.display.panelEditor.PanelEditor in project JMRI by JMRI.

the class JsonUtilHttpService method getPanels.

public JsonNode getPanels(Locale locale, String format) {
    ArrayNode root = mapper.createArrayNode();
    // list loaded Panels (ControlPanelEditor, PanelEditor, LayoutEditor, SwitchboardEditor)
    // list ControlPanelEditors
    Editor.getEditors(ControlPanelEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
        root.add(panel);
    });
    // list LayoutEditors and PanelEditors
    Editor.getEditors(PanelEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
        root.add(panel);
    });
    // list SwitchboardEditors
    Editor.getEditors(SwitchboardEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
        root.add(panel);
    });
    return root;
}
Also used : ControlPanelEditor(jmri.jmrit.display.controlPanelEditor.ControlPanelEditor) Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) ConnectionConfig(jmri.jmrix.ConnectionConfig) URL(jmri.server.json.JSON.URL) ProfileManager(jmri.profile.ProfileManager) DccLocoAddress(jmri.DccLocoAddress) JmriJFrame(jmri.util.JmriJFrame) PANEL(jmri.server.json.JSON.PANEL) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Editor(jmri.jmrit.display.Editor) JsonException(jmri.server.json.JsonException) SWITCHBOARD_PANEL(jmri.server.json.JSON.SWITCHBOARD_PANEL) ArrayList(java.util.ArrayList) TYPE(jmri.server.json.JSON.TYPE) JsonServerPreferences(jmri.jmris.json.JsonServerPreferences) ConnectionConfigManager(jmri.jmrix.ConnectionConfigManager) Locale(java.util.Locale) Profile(jmri.profile.Profile) SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) PanelEditor(jmri.jmrit.display.panelEditor.PanelEditor) JsonNode(com.fasterxml.jackson.databind.JsonNode) NAME(jmri.server.json.JSON.NAME) InstanceManager(jmri.InstanceManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonHttpService(jmri.server.json.JsonHttpService) Metadata(jmri.Metadata) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) WebServerPreferences(jmri.web.server.WebServerPreferences) SystemConnectionMemo(jmri.jmrix.SystemConnectionMemo) CONTROL_PANEL(jmri.server.json.JSON.CONTROL_PANEL) ConnectionNameFromSystemName(jmri.util.ConnectionNameFromSystemName) DATA(jmri.server.json.JSON.DATA) USERNAME(jmri.server.json.JSON.USERNAME) JSON(jmri.server.json.JSON) NodeIdentity(jmri.util.node.NodeIdentity) ZeroConfService(jmri.util.zeroconf.ZeroConfService) LAYOUT_PANEL(jmri.server.json.JSON.LAYOUT_PANEL) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with PanelEditor

use of jmri.jmrit.display.panelEditor.PanelEditor in project JMRI by JMRI.

the class IconEditorWindowTest method setUp.

// The minimal setup for log4J
@Override
protected void setUp() throws Exception {
    super.setUp();
    apps.tests.Log4JFixture.setUp();
    // log existing windows in setup
    JUnitUtil.resetWindows(true);
    JUnitUtil.resetInstanceManager();
    JUnitUtil.initInternalTurnoutManager();
    JUnitUtil.initInternalSensorManager();
    JUnitUtil.initInternalLightManager();
    JUnitUtil.initMemoryManager();
    JUnitUtil.initInternalSignalHeadManager();
    JUnitUtil.initShutDownManager();
    if (!GraphicsEnvironment.isHeadless()) {
        _editor = new PanelEditor("IconEditorTestPanel");
        Assert.assertNotNull(JFrameOperator.waitJFrame("IconEditorTestPanel", true, true));
        _panel = _editor.getTargetPanel();
        Assert.assertNotNull(_panel);
    }
}
Also used : PanelEditor(jmri.jmrit.display.panelEditor.PanelEditor)

Example 3 with PanelEditor

use of jmri.jmrit.display.panelEditor.PanelEditor in project JMRI by JMRI.

the class PanelServlet method getXmlPanel.

@Override
protected String getXmlPanel(String name) {
    log.debug("Getting {} for {}", getPanelType(), name);
    try {
        PanelEditor editor = (PanelEditor) getEditor(name);
        Element panel = new Element("panel");
        JFrame frame = editor.getTargetFrame();
        panel.setAttribute("name", name);
        panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight()));
        panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth()));
        panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight()));
        panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth()));
        panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
        panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
        if (editor.getBackgroundColor() != null) {
            Element color = new Element("backgroundColor");
            color.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed()));
            color.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen()));
            color.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue()));
            panel.addContent(color);
        }
        // include contents
        List<Positionable> contents = editor.getContents();
        log.debug("Panel has {} elements", contents.size());
        for (Positionable sub : contents) {
            if (sub != null) {
                try {
                    Element e = ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        if ("signalmasticon".equals(e.getName())) {
                            //insert icon details into signalmast
                            e.addContent(getSignalMastIconsElement(e.getAttributeValue("signalmast")));
                        }
                        try {
                            e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
                        } catch (NullPointerException ex) {
                            if (sub.getNamedBean() == null) {
                                log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
                            } else {
                                log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
                            }
                        }
                        parsePortableURIs(e);
                        panel.addContent(e);
                    }
                } catch (Exception ex) {
                    log.error("Error storing panel element: {}", ex.getMessage(), ex);
                }
            }
        }
        Document doc = new Document(panel);
        XMLOutputter out = new XMLOutputter();
        out.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
        return out.outputString(doc);
    } catch (NullPointerException ex) {
        log.warn("Requested Panel [" + name + "] does not exist.");
        return "ERROR Requested panel [" + name + "] does not exist.";
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) JFrame(javax.swing.JFrame) Element(org.jdom2.Element) Positionable(jmri.jmrit.display.Positionable) Document(org.jdom2.Document) PanelEditor(jmri.jmrit.display.panelEditor.PanelEditor) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 4 with PanelEditor

use of jmri.jmrit.display.panelEditor.PanelEditor in project JMRI by JMRI.

the class PanelServlet method getJsonPanel.

@Override
protected String getJsonPanel(String name) {
    log.debug("Getting {} for {}", getPanelType(), name);
    try {
        PanelEditor editor = (PanelEditor) getEditor(name);
        ObjectNode root = this.mapper.createObjectNode();
        ObjectNode panel = root.putObject("panel");
        JFrame frame = editor.getTargetFrame();
        panel.put("name", name);
        panel.put("height", frame.getContentPane().getHeight());
        panel.put("width", frame.getContentPane().getWidth());
        panel.put("panelheight", frame.getContentPane().getHeight());
        panel.put("panelwidth", frame.getContentPane().getWidth());
        panel.put("showtooltips", editor.showTooltip());
        panel.put("controlling", editor.allControlling());
        if (editor.getBackgroundColor() != null) {
            ObjectNode color = panel.putObject("backgroundColor");
            color.put("red", editor.getBackgroundColor().getRed());
            color.put("green", editor.getBackgroundColor().getGreen());
            color.put("blue", editor.getBackgroundColor().getBlue());
        }
        // include contents
        log.debug("N elements: {}", editor.getContents().size());
        for (Positionable sub : editor.getContents()) {
            try {
            // TODO: get all panel contents as JSON
            // I tried using JavaBean Introspection to simply build the contents using Jackson Databindings,
            // but when a panel element has a reference to the panel or to itself as a property, this leads
            // to infinite recursion
            } catch (Exception ex) {
                log.error("Error storing panel element: " + ex, ex);
            }
        }
        return this.mapper.writeValueAsString(root);
    } catch (NullPointerException ex) {
        log.warn("Requested Panel [" + name + "] does not exist.");
        return "ERROR Requested panel [" + name + "] does not exist.";
    } catch (JsonGenerationException e) {
        log.error("Error generating JSON", e);
        return "ERROR " + e.getLocalizedMessage();
    } catch (JsonMappingException e) {
        log.error("Error mapping JSON", e);
        return "ERROR " + e.getLocalizedMessage();
    } catch (IOException e) {
        log.error("IOException", e);
        return "ERROR " + e.getLocalizedMessage();
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JFrame(javax.swing.JFrame) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Positionable(jmri.jmrit.display.Positionable) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) PanelEditor(jmri.jmrit.display.panelEditor.PanelEditor) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 5 with PanelEditor

use of jmri.jmrit.display.panelEditor.PanelEditor 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)

Aggregations

PanelEditor (jmri.jmrit.display.panelEditor.PanelEditor)6 JFrame (javax.swing.JFrame)3 Positionable (jmri.jmrit.display.Positionable)3 Element (org.jdom2.Element)3 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Point (java.awt.Point)2 IOException (java.io.IOException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Enumeration (java.util.Enumeration)1 Locale (java.util.Locale)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ConfigureManager (jmri.ConfigureManager)1