Search in sources :

Example 51 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class BlockContentsIconXml method load.

/**
     * Load, starting with the BlockContentsIcon element, then all the
     * value-icon pairs
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    Editor ed = null;
    BlockContentsIcon l;
    if (o instanceof LayoutEditor) {
        ed = (LayoutEditor) o;
        l = new jmri.jmrit.display.layoutEditor.BlockContentsIcon("   ", (LayoutEditor) ed);
    } else if (o instanceof jmri.jmrit.display.Editor) {
        ed = (Editor) o;
        l = new BlockContentsIcon("", ed);
    } else {
        log.error("Unrecognizable class - " + o.getClass().getName());
        return;
    }
    String name;
    Attribute attr = element.getAttribute("blockcontents");
    if (attr == null) {
        log.error("incorrect information for a block contents; must use block name");
        ed.loadFailed();
        return;
    } else {
        name = attr.getValue();
    }
    loadTextInfo(l, element);
    Block m = jmri.InstanceManager.getDefault(jmri.BlockManager.class).getBlock(name);
    if (m != null) {
        l.setBlock(name);
    } else {
        log.error("Block named '" + attr.getValue() + "' not found.");
        ed.loadFailed();
    }
    Attribute a = element.getAttribute("selectable");
    if (a != null && a.getValue().equals("yes")) {
        l.setSelectable(true);
    } else {
        l.setSelectable(false);
    }
    // get the icon pairs
    List<Element> items = element.getChildren("blockstate");
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        String iconName = item.getAttribute("icon").getValue();
        NamedIcon icon = NamedIcon.getIconByName(iconName);
        if (icon == null) {
            icon = ed.loadFailed("Memory " + name, iconName);
            if (icon == null) {
                log.info("Memory \"" + name + "\" icon removed for url= " + iconName);
            }
        }
        if (icon != null) {
            String keyValue = item.getAttribute("value").getValue();
            l.addKeyAndIcon(icon, keyValue);
        }
    }
    ed.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.MEMORIES, element);
    int x = 0;
    int y = 0;
    try {
        x = element.getAttribute("x").getIntValue();
        y = element.getAttribute("y").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert positional attribute");
    }
    l.setOriginalLocation(x, y);
    l.displayState();
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Attribute(org.jdom2.Attribute) BlockContentsIcon(jmri.jmrit.display.BlockContentsIcon) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor) Block(jmri.Block) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Editor(jmri.jmrit.display.Editor)

Example 52 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class IndicatorTrackIconXml method load.

/**
     * Create a IndicatorTrackIcon, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    Editor p = (Editor) o;
    IndicatorTrackIcon l = new IndicatorTrackIcon(p);
    Element elem = element.getChild("iconmap");
    if (elem != null) {
        List<Element> status = elem.getChildren();
        if (status.size() > 0) {
            for (int i = 0; i < status.size(); i++) {
                String msg = "IndicatorTrack \"" + l.getNameString() + "\" icon \"" + status.get(i).getName() + "\" ";
                NamedIcon icon = loadIcon(l, status.get(i).getName(), elem, msg, p);
                if (icon != null) {
                    l.setIcon(status.get(i).getName(), icon);
                } else {
                    log.info(msg + " removed for url= " + status.get(i).getName());
                    return;
                }
            }
        }
        Attribute attr = elem.getAttribute("family");
        if (attr != null) {
            l.setFamily(attr.getValue());
        }
    }
    Element name = element.getChild("occupancyblock");
    if (name != null) {
        l.setOccBlock(name.getText());
    } else {
        // only write sensor if no OBlock, don't write double sensing
        name = element.getChild("occupancysensor");
        if (name != null) {
            l.setOccSensor(name.getText());
        }
    }
    l.setShowTrain(false);
    name = element.getChild("showTrainName");
    if (name != null) {
        if ("yes".equals(name.getText())) {
            l.setShowTrain(true);
        }
    }
    elem = element.getChild("paths");
    if (elem != null) {
        ArrayList<String> paths = new ArrayList<String>();
        List<Element> pth = elem.getChildren();
        for (int i = 0; i < pth.size(); i++) {
            paths.add(pth.get(i).getText());
        }
        l.setPaths(paths);
    }
    l.displayState(l.getStatus());
    l.updateSize();
    p.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.TURNOUTS, element);
}
Also used : IndicatorTrackIcon(jmri.jmrit.display.IndicatorTrackIcon) NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Editor(jmri.jmrit.display.Editor)

Example 53 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class IndicatorTurnoutIconXml method store.

/**
     * Default implementation for storing the contents of a IndicatorTurnoutIcon
     *
     * @param o Object to store, of type IndicatorTurnoutIcon
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    IndicatorTurnoutIcon p = (IndicatorTurnoutIcon) o;
    if (!p.isActive()) {
        // if flagged as inactive, don't store
        return null;
    }
    Element element = new Element("indicatorturnouticon");
    storeCommonAttributes(p, element);
    NamedBeanHandle<Turnout> t = p.getNamedTurnout();
    if (t != null) {
        element.addContent(storeNamedBean("turnout", t));
    }
    NamedBeanHandle<OBlock> b = p.getNamedOccBlock();
    if (b != null) {
        element.addContent(storeNamedBean("occupancyblock", b));
    }
    NamedBeanHandle<Sensor> s = p.getNamedOccSensor();
    if (b == null && s != null) {
        // only write sensor if no OBlock
        element.addContent(storeNamedBean("occupancysensor", s));
    }
    Element elem = new Element("showTrainName");
    String show = "no";
    if (p.showTrain()) {
        show = "yes";
    }
    elem.addContent(show);
    element.addContent(elem);
    HashMap<String, HashMap<Integer, NamedIcon>> iconMaps = p.getIconMaps();
    Iterator<Entry<String, HashMap<Integer, NamedIcon>>> it = iconMaps.entrySet().iterator();
    Element el = new Element("iconmaps");
    String family = p.getFamily();
    if (family != null) {
        el.setAttribute("family", family);
    }
    while (it.hasNext()) {
        Entry<String, HashMap<Integer, NamedIcon>> ent = it.next();
        elem = new Element(ent.getKey());
        Iterator<Entry<Integer, NamedIcon>> iter = ent.getValue().entrySet().iterator();
        while (iter.hasNext()) {
            Entry<Integer, NamedIcon> entry = iter.next();
            elem.addContent(storeIcon(p.getStateName(entry.getKey()), entry.getValue()));
        }
        el.addContent(elem);
    }
    element.addContent(el);
    elem = new Element("paths");
    ArrayList<String> paths = p.getPaths();
    if (paths != null) {
        for (int i = 0; i < paths.size(); i++) {
            Element e = new Element("path");
            e.addContent(paths.get(i));
            elem.addContent(e);
        }
        element.addContent(elem);
    }
    element.setAttribute("class", "jmri.jmrit.display.configurexml.IndicatorTurnoutIconXml");
    return element;
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) HashMap(java.util.HashMap) Element(org.jdom2.Element) IndicatorTurnoutIcon(jmri.jmrit.display.IndicatorTurnoutIcon) Entry(java.util.Map.Entry) Turnout(jmri.Turnout) OBlock(jmri.jmrit.logix.OBlock) Sensor(jmri.Sensor)

Example 54 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class LinkingLabelXml method load.

/**
     * Create LinkingLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    LinkingLabel l = null;
    String url = element.getChild("url").getText();
    // get object class and determine editor being used
    Editor editor = (Editor) o;
    if (element.getAttribute("icon") != null) {
        NamedIcon icon = null;
        String name = element.getAttribute("icon").getValue();
        //            if (log.isDebugEnabled()) log.debug("icon attribute= "+name);
        if (name.equals("yes")) {
            icon = getNamedIcon("icon", element, "LinkingLabel ", editor);
        } else {
            icon = NamedIcon.getIconByName(name);
            if (icon == null) {
                icon = editor.loadFailed("LinkingLabel", name);
                if (icon == null) {
                    log.info("LinkingLabel icon removed for url= " + name);
                    return;
                }
            }
        }
        // abort if name != yes and have null icon
        if (icon == null && !name.equals("yes")) {
            log.info("LinkingLabel icon removed for url= " + name);
            return;
        }
        l = new LinkingLabel(icon, editor, url);
        // no text
        l.setPopupUtility(null);
        try {
            Attribute a = element.getAttribute("rotate");
            if (a != null && icon != null) {
                int rotation = element.getAttribute("rotate").getIntValue();
                icon.setRotation(rotation, l);
            }
        } catch (org.jdom2.DataConversionException e) {
        }
        if (name.equals("yes")) {
            NamedIcon nIcon = loadIcon(l, "icon", element, "LinkingLabel ", editor);
            if (nIcon != null) {
                l.updateIcon(nIcon);
            } else {
                log.info("LinkingLabel icon removed for url= " + name);
                return;
            }
        } else {
            l.updateIcon(icon);
        }
    //l.setSize(l.getPreferredSize().width, l.getPreferredSize().height);
    } else if (element.getAttribute("text") != null) {
        l = new LinkingLabel(element.getAttribute("text").getValue(), editor, url);
        loadTextInfo(l, element);
    } else {
        log.error("LinkingLabel is null!");
        if (log.isDebugEnabled()) {
            java.util.List<Attribute> attrs = element.getAttributes();
            log.debug("\tElement Has " + attrs.size() + " Attributes:");
            for (int i = 0; i < attrs.size(); i++) {
                Attribute a = attrs.get(i);
                log.debug("\t\t" + a.getName() + " = " + a.getValue());
            }
            java.util.List<Element> kids = element.getChildren();
            log.debug("\tElementHas " + kids.size() + " children:");
            for (int i = 0; i < kids.size(); i++) {
                Element e = kids.get(i);
                log.debug("\t\t" + e.getName() + " = \"" + e.getValue() + "\"");
            }
        }
        editor.loadFailed();
        return;
    }
    editor.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.LABELS, element);
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) LinkingLabel(jmri.jmrit.display.LinkingLabel) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor)

Example 55 with NamedIcon

use of jmri.jmrit.catalog.NamedIcon in project JMRI by JMRI.

the class LocoIconXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    Editor ed = (Editor) o;
    LocoIcon l = new LocoIcon(ed);
    // create the objects
    String textName = "error";
    try {
        textName = element.getAttribute("text").getValue();
    } catch (Exception e) {
        log.error("failed to get loco text attribute ex= " + e);
    }
    String name = "error";
    NamedIcon icon;
    try {
        name = element.getAttribute("icon").getValue();
    } catch (Exception e) {
        log.error("failed to get icon attribute ex= " + e);
    }
    if (name.equals("yes")) {
        icon = loadIcon(l, "icon", element, "LocoIcon", ed);
    } else {
        icon = NamedIcon.getIconByName(name);
        if (icon == null) {
            icon = ed.loadFailed("LocoIcon", name);
            if (icon == null) {
                log.info("LocoIcon icon removed for url= " + name);
                return;
            }
        }
    }
    l.updateIcon(icon);
    try {
        int x = element.getAttribute("dockX").getIntValue();
        int y = element.getAttribute("dockY").getIntValue();
        l.setDockingLocation(x, y);
    //           l.dock();
    } catch (Exception e) {
        log.warn("failed to get docking location= " + e);
    }
    String rosterId = null;
    try {
        rosterId = element.getAttribute("rosterentry").getValue();
        RosterEntry entry = Roster.getDefault().entryFromTitle(rosterId);
        l.setRosterEntry(entry);
    } catch (Exception e) {
        log.debug("no roster entry for " + rosterId + ", ex= " + e);
    }
    ed.putLocoIcon(l, textName);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.MARKERS, element);
    loadTextInfo(l, element);
    // to detect "background" color for use in Tracker, examine icon file 
    l.init();
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) LocoIcon(jmri.jmrit.display.LocoIcon) RosterEntry(jmri.jmrit.roster.RosterEntry) Editor(jmri.jmrit.display.Editor)

Aggregations

NamedIcon (jmri.jmrit.catalog.NamedIcon)128 Entry (java.util.Map.Entry)28 Element (org.jdom2.Element)27 HashMap (java.util.HashMap)23 JPanel (javax.swing.JPanel)13 Editor (jmri.jmrit.display.Editor)13 Attribute (org.jdom2.Attribute)13 ActionEvent (java.awt.event.ActionEvent)12 ActionListener (java.awt.event.ActionListener)12 JLabel (javax.swing.JLabel)11 JButton (javax.swing.JButton)10 DataFlavor (java.awt.datatransfer.DataFlavor)6 CatalogTreeNode (jmri.jmrit.catalog.CatalogTreeNode)6 DragJLabel (jmri.jmrit.catalog.DragJLabel)6 Sensor (jmri.Sensor)5 CatalogTreeLeaf (jmri.jmrit.catalog.CatalogTreeLeaf)5 RosterEntry (jmri.jmrit.roster.RosterEntry)5 Point (java.awt.Point)4 JComponent (javax.swing.JComponent)4 JDialog (javax.swing.JDialog)4