Search in sources :

Example 56 with Positionable

use of jmri.jmrit.display.Positionable in project JMRI by JMRI.

the class EditPortalFrame method iconIntersectsBlock.

/**
     * Query whether icon intersects any track icons of block
     *
     * @return null if intersection, otherwise a messags
     */
private String iconIntersectsBlock(PortalIcon icon, OBlock block) {
    java.util.List<Positionable> list = _parent.getCircuitIcons(block);
    if (list == null || list.size() == 0) {
        return Bundle.getMessage("needIcons", block.getDisplayName(), Bundle.getMessage("editCircuitItem"));
    }
    Rectangle rect = new Rectangle();
    Rectangle iconRect = icon.getBounds(new Rectangle());
    for (int i = 0; i < list.size(); i++) {
        Positionable comp = list.get(i);
        if (CircuitBuilder.isTrack(comp)) {
            rect = list.get(i).getBounds(rect);
            if (iconRect.intersects(rect)) {
                return null;
            }
        }
    }
    return Bundle.getMessage("iconNotOnBlock", block.getDisplayName(), icon.getNameString());
}
Also used : Rectangle(java.awt.Rectangle) Positionable(jmri.jmrit.display.Positionable) Point(java.awt.Point)

Example 57 with Positionable

use of jmri.jmrit.display.Positionable in project JMRI by JMRI.

the class JmriJFrameServlet method sendClick.

void sendClick(String name, Component c, int xg, int yg, Container FrameContentPane) {
    // global positions
    int x = xg - c.getLocation().x;
    int y = yg - c.getLocation().y;
    // log.debug("component is {}", c);
    log.debug("Local click at {},{}", x, y);
    if (c.getClass().equals(JButton.class)) {
        ((AbstractButton) c).doClick();
    } else if (c.getClass().equals(JCheckBox.class)) {
        ((AbstractButton) c).doClick();
    } else if (c.getClass().equals(JRadioButton.class)) {
        ((AbstractButton) c).doClick();
    } else if (MouseListener.class.isAssignableFrom(c.getClass())) {
        log.debug("Invoke directly on MouseListener, at {},{}", x, y);
        sendClickSequence((MouseListener) c, c, x, y);
    } else if (c instanceof jmri.jmrit.display.MultiSensorIcon) {
        log.debug("Invoke Clicked on MultiSensorIcon");
        MouseEvent e = new MouseEvent(c, MouseEvent.MOUSE_CLICKED, // time
        0, // modifiers
        0, // this component expects global positions for some reason
        xg, // this component expects global positions for some reason
        yg, // one click
        1, // not a popup
        false);
        ((Positionable) c).doMouseClicked(e);
    } else if (Positionable.class.isAssignableFrom(c.getClass())) {
        log.debug("Invoke Pressed, Released and Clicked on Positionable");
        MouseEvent e = new MouseEvent(c, MouseEvent.MOUSE_PRESSED, // time
        0, // modifiers
        0, // x, y not in this component?
        x, // x, y not in this component?
        y, // one click
        1, // not a popup
        false);
        ((jmri.jmrit.display.Positionable) c).doMousePressed(e);
        e = new MouseEvent(c, MouseEvent.MOUSE_RELEASED, // time
        0, // modifiers
        0, // x, y not in this component?
        x, // x, y not in this component?
        y, // one click
        1, // not a popup
        false);
        ((jmri.jmrit.display.Positionable) c).doMouseReleased(e);
        e = new MouseEvent(c, MouseEvent.MOUSE_CLICKED, // time
        0, // modifiers
        0, // x, y not in this component?
        x, // x, y not in this component?
        y, // one click
        1, // not a popup
        false);
        ((jmri.jmrit.display.Positionable) c).doMouseClicked(e);
    } else {
        MouseListener[] la = c.getMouseListeners();
        log.debug("Invoke {} contained mouse listeners", la.length);
        log.debug("component is {}", c);
        // y -= (int)(pc.getY() - pf.getY());
        for (MouseListener ml : la) {
            log.debug("Send click sequence at {},{}", x, y);
            sendClickSequence(ml, c, x, y);
        }
    }
}
Also used : JCheckBox(javax.swing.JCheckBox) AbstractButton(javax.swing.AbstractButton) MouseListener(java.awt.event.MouseListener) MouseEvent(java.awt.event.MouseEvent) Positionable(jmri.jmrit.display.Positionable)

Example 58 with Positionable

use of jmri.jmrit.display.Positionable in project JMRI by JMRI.

the class LayoutPanelServlet method getXmlPanel.

@Override
protected String getXmlPanel(String name) {
    log.debug("Getting {} for {}", getPanelType(), name);
    try {
        LayoutEditor editor = (LayoutEditor) getEditor(name);
        Element panel = new Element("panel");
        panel.setAttribute("name", name);
        panel.setAttribute("paneltype", getPanelType());
        panel.setAttribute("height", Integer.toString(editor.getLayoutHeight()));
        panel.setAttribute("width", Integer.toString(editor.getLayoutWidth()));
        panel.setAttribute("panelheight", Integer.toString(editor.getLayoutHeight()));
        panel.setAttribute("panelwidth", Integer.toString(editor.getLayoutWidth()));
        panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
        panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
        panel.setAttribute("xscale", Float.toString((float) editor.getXScale()));
        panel.setAttribute("yscale", Float.toString((float) editor.getYScale()));
        panel.setAttribute("mainlinetrackwidth", Integer.toString(editor.getMainlineTrackWidth()));
        panel.setAttribute("sidetrackwidth", Integer.toString(editor.getSideTrackWidth()));
        panel.setAttribute("turnoutcircles", (editor.getTurnoutCircles()) ? "yes" : "no");
        panel.setAttribute("turnoutcirclesize", Integer.toString(editor.getTurnoutCircleSize()));
        panel.setAttribute("turnoutdrawunselectedleg", (editor.getTurnoutDrawUnselectedLeg()) ? "yes" : "no");
        if (editor.getBackgroundColor() == null) {
            panel.setAttribute("backgroundcolor", ColorUtil.colorToString(Color.lightGray));
        } else {
            panel.setAttribute("backgroundcolor", ColorUtil.colorToString(editor.getBackgroundColor()));
        }
        panel.setAttribute("defaulttrackcolor", editor.getDefaultTrackColor());
        panel.setAttribute("defaultoccupiedtrackcolor", editor.getDefaultOccupiedTrackColor());
        panel.setAttribute("defaultalternativetrackcolor", editor.getDefaultAlternativeTrackColor());
        panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
        panel.setAttribute("turnoutcirclecolor", editor.getTurnoutCircleColor());
        // include positionable elements
        List<Positionable> contents = editor.getContents();
        log.debug("N positionable 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, ex);
                }
            }
        }
        // include PositionablePoints
        int num = editor.pointList.size();
        log.debug("N positionablepoint elements: {}", num);
        if (num > 0) {
            for (int i = 0; i < num; i++) {
                Object sub = editor.pointList.get(i);
                try {
                    Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        panel.addContent(e);
                    }
                } catch (Exception e) {
                    log.error("Error storing panel positionalpoint element: " + e);
                }
            }
        }
        // include LayoutBlocks
        LayoutBlockManager tm = InstanceManager.getDefault(LayoutBlockManager.class);
        java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
        SensorManager sm = InstanceManager.sensorManagerInstance();
        num = 0;
        while (iter.hasNext()) {
            String sname = iter.next();
            if (sname == null) {
                log.error("System name null during LayoutBlock store");
            }
            LayoutBlock b = tm.getBySystemName(sname);
            if (b.getUseCount() > 0) {
                // save only those LayoutBlocks that are in use--skip abandoned ones
                Element elem = new Element("layoutblock").setAttribute("systemname", sname);
                String uname = b.getUserName();
                if (uname != null && !uname.isEmpty()) {
                    elem.setAttribute("username", uname);
                }
                // get occupancy sensor from layoutblock if it is valid
                if (!b.getOccupancySensorName().isEmpty()) {
                    Sensor s = sm.getSensor(b.getOccupancySensorName());
                    if (s != null) {
                        //send systemname
                        elem.setAttribute("occupancysensor", s.getSystemName());
                    }
                //if layoutblock has no occupancy sensor, use one from block, if it is populated
                } else {
                    Sensor s = b.getBlock().getSensor();
                    if (s != null) {
                        //send systemname
                        elem.setAttribute("occupancysensor", s.getSystemName());
                    }
                }
                elem.setAttribute("occupiedsense", Integer.toString(b.getOccupiedSense()));
                elem.setAttribute("trackcolor", ColorUtil.colorToString(b.getBlockTrackColor()));
                elem.setAttribute("occupiedcolor", ColorUtil.colorToString(b.getBlockOccupiedColor()));
                elem.setAttribute("extracolor", ColorUtil.colorToString(b.getBlockExtraColor()));
                if (!b.getMemoryName().isEmpty()) {
                    elem.setAttribute("memory", b.getMemoryName());
                }
                if (!b.useDefaultMetric()) {
                    elem.addContent(new Element("metric").addContent(Integer.toString(b.getBlockMetric())));
                }
                //add to the panel xml
                panel.addContent(elem);
                num++;
            }
        }
        log.debug("N layoutblock elements: {}", num);
        // include LevelXings
        num = editor.xingList.size();
        log.debug("N levelxing elements: {}", num);
        if (num > 0) {
            for (int i = 0; i < num; i++) {
                Object sub = editor.xingList.get(i);
                try {
                    Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        panel.addContent(e);
                    }
                } catch (Exception e) {
                    log.error("Error storing panel levelxing element: " + e);
                }
            }
        }
        // include LayoutTurnouts
        num = editor.turnoutList.size();
        log.debug("N layoutturnout elements: {}", num);
        if (num > 0) {
            for (int i = 0; i < num; i++) {
                Object sub = editor.turnoutList.get(i);
                try {
                    Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        panel.addContent(e);
                    }
                } catch (Exception e) {
                    log.error("Error storing panel layoutturnout element: " + e);
                }
            }
        }
        // include TrackSegments
        num = editor.trackList.size();
        log.debug("N tracksegment elements: {}", num);
        if (num > 0) {
            for (int i = 0; i < num; i++) {
                Object sub = editor.trackList.get(i);
                try {
                    Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        panel.addContent(e);
                    }
                } catch (Exception e) {
                    log.error("Error storing panel tracksegment element: " + e);
                }
            }
        }
        // include LayoutSlips
        num = editor.slipList.size();
        log.debug("N layoutSlip elements: {}", num);
        if (num > 0) {
            for (int i = 0; i < num; i++) {
                Object sub = editor.slipList.get(i);
                try {
                    Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        panel.addContent(e);
                    }
                } catch (Exception e) {
                    log.error("Error storing panel layoutSlip element: " + e);
                }
            }
        }
        // include LayoutTurntables
        num = editor.turntableList.size();
        log.debug("N turntable elements: {}", num);
        if (num > 0) {
            for (int i = 0; i < num; i++) {
                Object sub = editor.turntableList.get(i);
                try {
                    Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        panel.addContent(e);
                    }
                } catch (Exception e) {
                    log.error("Error storing panel turntable element: " + e);
                }
            }
        }
        //write out formatted document
        Document doc = new Document(panel);
        XMLOutputter fmt = new XMLOutputter();
        fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
        return fmt.outputString(doc);
    } catch (NullPointerException ex) {
        log.warn("Requested Layout panel [" + name + "] does not exist.");
        return "ERROR Requested panel [" + name + "] does not exist.";
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Element(org.jdom2.Element) Document(org.jdom2.Document) LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) SensorManager(jmri.SensorManager) LayoutBlockManager(jmri.jmrit.display.layoutEditor.LayoutBlockManager) Positionable(jmri.jmrit.display.Positionable) Sensor(jmri.Sensor)

Example 59 with Positionable

use of jmri.jmrit.display.Positionable 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 60 with Positionable

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

Aggregations

Positionable (jmri.jmrit.display.Positionable)60 ArrayList (java.util.ArrayList)18 Point (java.awt.Point)15 IndicatorTrack (jmri.jmrit.display.IndicatorTrack)12 Portal (jmri.jmrit.logix.Portal)7 Element (org.jdom2.Element)7 JFrame (javax.swing.JFrame)6 OBlock (jmri.jmrit.logix.OBlock)6 ActionEvent (java.awt.event.ActionEvent)5 ActionListener (java.awt.event.ActionListener)5 IOException (java.io.IOException)4 JMenuItem (javax.swing.JMenuItem)4 Color (java.awt.Color)3 Dimension (java.awt.Dimension)3 Rectangle (java.awt.Rectangle)3 PanelEditor (jmri.jmrit.display.panelEditor.PanelEditor)3 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 Graphics2D (java.awt.Graphics2D)2 Clipboard (java.awt.datatransfer.Clipboard)2