Search in sources :

Example 11 with NamedIcon

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

the class TurnoutIconXml method loadTurnoutIcon.

private NamedIcon loadTurnoutIcon(String state, int rotation, TurnoutIcon l, Element element, String name, Editor ed) {
    NamedIcon icon = null;
    if (element.getAttribute(state) != null) {
        String iconName = element.getAttribute(state).getValue();
        icon = NamedIcon.getIconByName(iconName);
        if (icon == null) {
            icon = ed.loadFailed("Turnout \"" + name + "\" icon \"" + state + "\" ", iconName);
            if (icon == null) {
                log.info("Turnout \"" + name + "\" icon \"" + state + "\" removed for url= " + iconName);
            }
        } else {
            icon.setRotation(rotation, l);
        }
    } else {
        log.warn("did not locate " + state + " icon file for Turnout " + name);
    }
    if (icon == null) {
        log.info("Turnout Icon \"" + name + "\": icon \"" + state + "\" removed");
    } else {
        l.setIcon(_nameMap.get(state), icon);
    }
    return icon;
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon)

Example 12 with NamedIcon

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

the class TurnoutIconXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       Editor as an Object
     */
@SuppressWarnings("null")
@Override
public void load(Element element, Object o) {
    // create the objects
    Editor p = (Editor) o;
    TurnoutIcon l = new TurnoutIcon(p);
    String name;
    try {
        name = element.getAttribute("turnout").getValue();
    } catch (NullPointerException e) {
        log.error("incorrect information for turnout; must use turnout name");
        p.loadFailed();
        return;
    }
    l.setTurnout(name);
    Attribute a = element.getAttribute("tristate");
    if ((a == null) || a.getValue().equals("true")) {
        l.setTristate(true);
    } else {
        l.setTristate(false);
    }
    a = element.getAttribute("momentary");
    if ((a != null) && a.getValue().equals("true")) {
        l.setMomentary(true);
    } else {
        l.setMomentary(false);
    }
    a = element.getAttribute("directControl");
    if ((a != null) && a.getValue().equals("true")) {
        l.setDirectControl(true);
    } else {
        l.setDirectControl(false);
    }
    List<Element> states = element.getChildren();
    if (states.size() > 0) {
        if (log.isDebugEnabled()) {
            log.debug("Main element has" + states.size() + " items");
        }
        // the element containing the icons
        Element elem = element;
        Element icons = element.getChild("icons");
        if (icons != null) {
            List<Element> s = icons.getChildren();
            states = s;
            // the element containing the icons
            elem = icons;
            if (log.isDebugEnabled()) {
                log.debug("icons element has" + states.size() + " items");
            }
        }
        for (int i = 0; i < states.size(); i++) {
            String state = states.get(i).getName();
            if (log.isDebugEnabled()) {
                log.debug("setIcon for state \"" + state + "\" and " + _nameMap.get(state));
            }
            NamedIcon icon = loadIcon(l, state, elem, "TurnoutIcon \"" + name + "\": icon \"" + state + "\" ", p);
            if (icon != null) {
                l.setIcon(_nameMap.get(state), icon);
            } else {
                log.info("TurnoutIcon \"" + name + "\": icon \"" + state + "\" removed");
                return;
            }
        }
        log.debug(states.size() + " icons loaded for " + l.getNameString());
    } else {
        // case when everything was attributes
        int rotation = 0;
        try {
            rotation = element.getAttribute("rotate").getIntValue();
        } catch (org.jdom2.DataConversionException e) {
        } catch (NullPointerException e) {
        // considered normal if the attributes are not present
        }
        if (loadTurnoutIcon("thrown", rotation, l, element, name, p) == null) {
            return;
        }
        if (loadTurnoutIcon("closed", rotation, l, element, name, p) == null) {
            return;
        }
        if (loadTurnoutIcon("unknown", rotation, l, element, name, p) == null) {
            return;
        }
        if (loadTurnoutIcon("inconsistent", rotation, l, element, name, p) == null) {
            return;
        }
    }
    Element elem = element.getChild("iconmaps");
    if (elem != null) {
        Attribute attr = elem.getAttribute("family");
        if (attr != null) {
            l.setFamily(attr.getValue());
        }
    }
    p.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.TURNOUTS, element);
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor) TurnoutIcon(jmri.jmrit.display.TurnoutIcon)

Example 13 with NamedIcon

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

the class CircuitBuilder method convertSeg.

private void convertSeg() {
    IndicatorTrackIcon t = new IndicatorTrackIcon(_editor);
    t.setOccBlockHandle(InstanceManager.getDefault(NamedBeanHandleManager.class).getNamedBeanHandle(_currentBlock.getSystemName(), _currentBlock));
    t.setFamily(_trackPanel.getFamilyName());
    HashMap<String, NamedIcon> iconMap = _trackPanel.getIconMap();
    if (iconMap != null) {
        Iterator<Entry<String, NamedIcon>> it = iconMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, NamedIcon> entry = it.next();
            if (log.isDebugEnabled()) {
                log.debug("key= " + entry.getKey());
            }
            t.setIcon(entry.getKey(), new NamedIcon(entry.getValue()));
        }
    }
    t.setLevel(Editor.TURNOUTS);
    t.setScale(_oldIcon.getScale());
    t.rotate(_oldIcon.getDegrees());
    finishConvert(t);
}
Also used : IndicatorTrackIcon(jmri.jmrit.display.IndicatorTrackIcon) NamedIcon(jmri.jmrit.catalog.NamedIcon) Entry(java.util.Map.Entry)

Example 14 with NamedIcon

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

the class MemoryIcon method displayState.

@Override
public void displayState() {
    log.debug("displayState");
    if (getMemory() == null) {
        // use default if not connected yet
        setText(defaultText);
        updateSize();
        return;
    }
    if (re != null) {
        jmri.InstanceManager.throttleManagerInstance().removeListener(re.getDccLocoAddress(), this);
        re = null;
    }
    Object key = getMemory().getValue();
    if (key != null) {
        java.util.HashMap<String, NamedIcon> map = getMap();
        if (map == null) {
            // no map, attempt to show object directly
            Object val = key;
            if (val instanceof jmri.jmrit.roster.RosterEntry) {
                jmri.jmrit.roster.RosterEntry roster = (jmri.jmrit.roster.RosterEntry) val;
                val = updateIconFromRosterVal(roster);
                flipRosterIcon = false;
                if (val == null) {
                    return;
                }
            }
            if (val instanceof String) {
                if (val.equals("")) {
                    setText(defaultText);
                } else {
                    setText((String) val);
                }
                setIcon(null);
                _text = true;
                _icon = false;
                updateSize();
                return;
            } else if (val instanceof javax.swing.ImageIcon) {
                setIcon((javax.swing.ImageIcon) val);
                setText(null);
                _text = false;
                _icon = true;
                updateSize();
                return;
            } else if (val instanceof Number) {
                setText(val.toString());
                setIcon(null);
                _text = true;
                _icon = false;
                updateSize();
                return;
            } else {
                log.warn("can't display current value of " + getNamedMemory().getName() + ", val= " + val + " of Class " + val.getClass().getName());
            }
        } else {
            // map exists, use it
            NamedIcon newicon = map.get(key.toString());
            if (newicon != null) {
                setText(null);
                super.setIcon(newicon);
                _text = false;
                _icon = true;
                updateSize();
                return;
            } else {
                // no match, use default
                setIcon(getDefaultIcon());
                setText(null);
                _text = false;
                _icon = true;
                updateSize();
            }
        }
    } else {
        setIcon(null);
        setText(defaultText);
        _text = true;
        _icon = false;
        updateSize();
    }
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) RosterEntry(jmri.jmrit.roster.RosterEntry) RosterEntry(jmri.jmrit.roster.RosterEntry)

Example 15 with NamedIcon

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

the class IconItemPanel method addIconsToPanel.

/**
     * Add icons to panel.
     * 
     * @param iconMap set of icons to add to panel
     */
protected void addIconsToPanel(HashMap<String, NamedIcon> iconMap) {
    _iconPanel = new JPanel();
    Iterator<Entry<String, NamedIcon>> it = iconMap.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, NamedIcon> entry = it.next();
        // make copy for possible reduction
        NamedIcon icon = new NamedIcon(entry.getValue());
        JPanel panel = new JPanel();
        String borderName = ItemPalette.convertText(entry.getKey());
        panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), borderName));
        try {
            JLabel label = new IconDragJLabel(new DataFlavor(Editor.POSITIONABLE_FLAVOR), _level);
            label.setName(borderName);
            label.setToolTipText(icon.getName());
            panel.add(label);
            if (icon.getIconWidth() < 1 || icon.getIconHeight() < 1) {
                label.setText(Bundle.getMessage("invisibleIcon"));
                label.setForeground(Color.lightGray);
            } else {
                icon.reduceTo(50, 80, 0.2);
            }
            label.setIcon(icon);
            int width = Math.max(100, panel.getPreferredSize().width);
            panel.setPreferredSize(new java.awt.Dimension(width, panel.getPreferredSize().height));
        } catch (java.lang.ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        _iconPanel.add(panel);
    }
    add(_iconPanel, 1);
    _iconPanel.addMouseListener(this);
}
Also used : JPanel(javax.swing.JPanel) NamedIcon(jmri.jmrit.catalog.NamedIcon) JLabel(javax.swing.JLabel) DragJLabel(jmri.jmrit.catalog.DragJLabel) DataFlavor(java.awt.datatransfer.DataFlavor) Entry(java.util.Map.Entry)

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