Search in sources :

Example 31 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)

Example 32 with Positionable

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

the class CircuitBuilder method makeSelectionGroup.

/**
     * ************* end convert icons ******************
     */
/**
     * ************** select - deselect track icons ***********************
     */
/**
     * select block's track icons for editing -***could be
     * _circuitMap.get(block) is sufficient
     */
private ArrayList<Positionable> makeSelectionGroup(OBlock block, boolean showPortal) {
    ArrayList<Positionable> group = new ArrayList<Positionable>();
    List<Positionable> circuitIcons = _circuitMap.get(block);
    Iterator<Positionable> iter = circuitIcons.iterator();
    while (iter.hasNext()) {
        Positionable p = iter.next();
        if (p instanceof PortalIcon) {
            if (showPortal) {
                ((PortalIcon) p).setStatus(PortalIcon.VISIBLE);
                group.add(p);
            }
        } else {
            group.add(p);
        }
    }
    return group;
}
Also used : ArrayList(java.util.ArrayList) Positionable(jmri.jmrit.display.Positionable)

Example 33 with Positionable

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

the class ControlPanelEditor method copyItem.

/**
     * Set up selections for a paste. Note a copy of _selectionGroup is made
     * that is NOT in the _contents. This disconnected ArrayList is added to the
     * _contents when (if) a paste is made. The disconnected _selectionGroup can
     * be dragged to a new location.
     */
@Override
protected void copyItem(Positionable p) {
    if (log.isDebugEnabled()) {
        // avoid string concatination if not debug
        log.debug("Enter copyItem: _selectionGroup {}", _selectionGroup != null ? "size=" + _selectionGroup.size() : "null");
    }
    // If popup menu hit again, Paste selections and make another copy
    if (_pastePending) {
        pasteItems();
    }
    if (_selectionGroup != null && !_selectionGroup.contains(p)) {
        deselectSelectionGroup();
    }
    if (_selectionGroup == null) {
        _selectionGroup = new ArrayList<Positionable>();
        _selectionGroup.add(p);
    }
    ArrayList<Positionable> selectionGroup = new ArrayList<Positionable>();
    for (Positionable comp : _selectionGroup) {
        Positionable pos = comp.deepClone();
        selectionGroup.add(pos);
    }
    // group is now disconnected
    _selectionGroup = selectionGroup;
    _pastePending = true;
//        if (_debug) log.debug("Exit copyItem: _selectionGroup.size()= "+_selectionGroup.size());
}
Also used : ArrayList(java.util.ArrayList) Positionable(jmri.jmrit.display.Positionable)

Example 34 with Positionable

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

the class ControlPanelEditor method setDefaultPortalIcons.

public void setDefaultPortalIcons(HashMap<String, NamedIcon> map) {
    _portalIconMap = map;
    Iterator<Positionable> it = _contents.iterator();
    while (it.hasNext()) {
        Positionable pos = it.next();
        if (pos instanceof PortalIcon) {
            ((PortalIcon) pos).initMap();
        }
    }
}
Also used : Positionable(jmri.jmrit.display.Positionable)

Example 35 with Positionable

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

the class ControlPanelEditor method getCurrentSelection.

protected Positionable getCurrentSelection(MouseEvent event) {
    if (_pastePending && !event.isPopupTrigger() && !event.isMetaDown() && !event.isAltDown()) {
        return getCopySelection(event);
    }
    List<Positionable> selections = getSelectedItems(event);
    if (_disableShapeSelection || _disablePortalSelection) {
        ArrayList<Positionable> list = new ArrayList<Positionable>();
        Iterator<Positionable> it = selections.iterator();
        while (it.hasNext()) {
            Positionable pos = it.next();
            if (_disableShapeSelection && pos instanceof jmri.jmrit.display.controlPanelEditor.shape.PositionableShape) {
                continue;
            }
            if (_disablePortalSelection && pos instanceof PortalIcon) {
                continue;
            }
            list.add(pos);
        }
        selections = list;
    }
    Positionable selection = null;
    if (selections.size() > 0) {
        if (event.isControlDown()) {
            if (event.isShiftDown() && selections.size() > 3) {
                if (_manualSelection) {
                    // selection made - don't change it
                    deselectSelectionGroup();
                    return _currentSelection;
                }
                // show list
                String[] selects = new String[selections.size()];
                Iterator<Positionable> iter = selections.iterator();
                int i = 0;
                while (iter.hasNext()) {
                    Positionable pos = iter.next();
                    if (pos instanceof jmri.NamedBean) {
                        selects[i++] = ((jmri.NamedBean) pos).getDisplayName();
                    } else {
                        selects[i++] = pos.getNameString();
                    }
                }
                Object select = JOptionPane.showInputDialog(this, Bundle.getMessage("multipleSelections"), Bundle.getMessage("QuestionTitle"), JOptionPane.QUESTION_MESSAGE, null, selects, null);
                if (select != null) {
                    iter = selections.iterator();
                    while (iter.hasNext()) {
                        Positionable pos = iter.next();
                        String name = null;
                        if (pos instanceof jmri.NamedBean) {
                            name = ((jmri.NamedBean) pos).getDisplayName();
                        } else {
                            name = pos.getNameString();
                        }
                        if (((String) select).equals(name)) {
                            _manualSelection = true;
                            return pos;
                        }
                    }
                } else {
                    selection = selections.get(selections.size() - 1);
                }
            } else {
                // select bottom-most item over the background, otherwise take the background item
                selection = selections.get(selections.size() - 1);
                if (selection.getDisplayLevel() <= BKG && selections.size() > 1) {
                    selection = selections.get(selections.size() - 2);
                }
            //              _manualSelection = false;
            }
        } else {
            if (event.isShiftDown() && selections.size() > 1) {
                selection = selections.get(1);
            } else {
                selection = selections.get(0);
            }
            if (selection.getDisplayLevel() <= BKG) {
                selection = null;
            }
            _manualSelection = false;
        }
    }
    if (!isEditable() && selection != null && selection.isHidden()) {
        selection = null;
    }
    return selection;
}
Also used : ArrayList(java.util.ArrayList) Point(java.awt.Point) LinkingObject(jmri.jmrit.display.LinkingObject) Positionable(jmri.jmrit.display.Positionable)

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