Search in sources :

Example 6 with LayoutBlockManager

use of jmri.jmrit.display.layoutEditor.LayoutBlockManager in project JMRI by JMRI.

the class Section method setNameFromActiveBlock.

public void setNameFromActiveBlock(Object value) {
    LayoutBlockManager lbm = InstanceManager.getDefault(LayoutBlockManager.class);
    boolean beenSet = false;
    if (value == null || getState() == FREE || getState() == UNKNOWN) {
        setNameInBlocks(value);
    } else if (getState() == FORWARD) {
        for (int i = 0; i < mBlockEntries.size(); i++) {
            Block b = mBlockEntries.get(i);
            if (b.getState() == Block.OCCUPIED) {
                beenSet = true;
            }
            if (beenSet) {
                b.setValue(value);
            }
        }
    } else if (getState() == REVERSE) {
        for (int i = mBlockEntries.size(); i < 0; i--) {
            Block b = mBlockEntries.get(i);
            if (b.getState() == Block.OCCUPIED) {
                beenSet = true;
            }
            if (beenSet) {
                b.setValue(value);
            }
        }
    }
    if (!beenSet) {
        setNameInBlocks(value);
    }
}
Also used : LayoutBlockManager(jmri.jmrit.display.layoutEditor.LayoutBlockManager) LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) PositionablePoint(jmri.jmrit.display.layoutEditor.PositionablePoint)

Example 7 with LayoutBlockManager

use of jmri.jmrit.display.layoutEditor.LayoutBlockManager in project JMRI by JMRI.

the class LayoutBlockManagerXml method store.

/**
     * Implementation for storing the contents of a LayoutBlockManager
     *
     * @param o Object to store, of type LayoutBlockManager
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    Element layoutblocks = new Element("layoutblocks");
    setStoreElementClass(layoutblocks);
    LayoutBlockManager tm = (LayoutBlockManager) o;
    if (tm.isAdvancedRoutingEnabled()) {
        layoutblocks.setAttribute("blockrouting", "yes");
    }
    if (tm.getNamedStabilisedSensor() != null) {
        layoutblocks.setAttribute("routingStablisedSensor", tm.getNamedStabilisedSensor().getName());
    }
    java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
    // don't return an element if there is nothing to include
    if (!iter.hasNext()) {
        return null;
    }
    while (iter.hasNext()) {
        String sname = iter.next();
        if (sname == null) {
            log.error("System name null during LayoutBlock store");
        } else {
            log.debug("layoutblock system name is " + sname);
            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);
                elem.addContent(new Element("systemName").addContent(sname));
                storeCommon(b, elem);
                if (!b.getOccupancySensorName().equals("")) {
                    elem.setAttribute("occupancysensor", b.getOccupancySensorName());
                }
                elem.setAttribute("occupiedsense", "" + b.getOccupiedSense());
                elem.setAttribute("trackcolor", ColorUtil.colorToString(b.getBlockTrackColor()));
                elem.setAttribute("occupiedcolor", ColorUtil.colorToString(b.getBlockOccupiedColor()));
                elem.setAttribute("extracolor", ColorUtil.colorToString(b.getBlockExtraColor()));
                layoutblocks.addContent(elem);
                if (!b.getMemoryName().equals("")) {
                    elem.setAttribute("memory", b.getMemoryName());
                }
                if (!b.useDefaultMetric()) {
                    elem.addContent(new Element("metric").addContent("" + b.getBlockMetric()));
                }
            }
        }
    }
    return (layoutblocks);
}
Also used : LayoutBlock(jmri.jmrit.display.layoutEditor.LayoutBlock) Element(org.jdom2.Element) LayoutBlockManager(jmri.jmrit.display.layoutEditor.LayoutBlockManager)

Example 8 with LayoutBlockManager

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

Aggregations

LayoutBlockManager (jmri.jmrit.display.layoutEditor.LayoutBlockManager)8 LayoutBlock (jmri.jmrit.display.layoutEditor.LayoutBlock)6 PositionablePoint (jmri.jmrit.display.layoutEditor.PositionablePoint)3 Element (org.jdom2.Element)3 Color (java.awt.Color)1 ConfigureManager (jmri.ConfigureManager)1 Sensor (jmri.Sensor)1 SensorManager (jmri.SensorManager)1 Positionable (jmri.jmrit.display.Positionable)1 ConnectivityUtil (jmri.jmrit.display.layoutEditor.ConnectivityUtil)1 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)1 LayoutSlip (jmri.jmrit.display.layoutEditor.LayoutSlip)1 LayoutTurnout (jmri.jmrit.display.layoutEditor.LayoutTurnout)1 LevelXing (jmri.jmrit.display.layoutEditor.LevelXing)1 TrackNode (jmri.jmrit.display.layoutEditor.TrackNode)1 TrackSegment (jmri.jmrit.display.layoutEditor.TrackSegment)1 Attribute (org.jdom2.Attribute)1 Document (org.jdom2.Document)1 XMLOutputter (org.jdom2.output.XMLOutputter)1