001package jmri.jmrit.display.layoutEditor;
002
003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
004import javax.swing.JCheckBoxMenuItem;
005import javax.swing.JPopupMenu;
006import jmri.jmrit.catalog.NamedIcon;
007import jmri.Reportable;
008
009/**
010 * An icon to display a status of a GlobalVariable.
011 *
012 * This is the same name as display.GlobalVariableIcon, but a very
013 * separate class. That's not good. Unfortunately, it's too
014 * hard to disentangle that now because it's resident in the
015 * panel file that have been written out, so we just annotated
016 * the fact, but now we want to leave it on the list to fix.
017 */
018@SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", justification="Cannot rename for user data compatiblity reasons.")
019public class GlobalVariableIcon extends jmri.jmrit.display.GlobalVariableIcon {
020
021    private final String defaultText = " ";
022
023    public GlobalVariableIcon(String s, LayoutEditor panel) {
024        super(s, panel);
025        log.debug("GlobalVariableIcon ctor= {}", GlobalVariableIcon.class.getName());
026    }
027
028    @Override
029    public void setText(String text) {
030        if (text == null || text.isEmpty()) {
031            super.setText(defaultText);
032        } else {
033            super.setText(text);
034        }
035    }
036
037    @Override
038    public void displayState() {
039        log.debug("displayState");
040        if (getGlobalVariable() == null) {  // use default if not connected yet
041            setText(defaultText);
042            updateSize();
043            return;
044        }
045
046        Object key = getGlobalVariable().getValue();
047        if (key != null) {
048            java.util.HashMap<String, NamedIcon> map = getMap();
049            if (map == null) {
050                // no map, attempt to show object directly
051                Object val = key;
052
053                if (val instanceof String) {
054                    if (((String)val).isEmpty()) {
055                        setText(defaultText);
056                    } else {
057                        setText((String) val);
058                    }
059                    setIcon(null);
060                    _text = true;
061                    _icon = false;
062                    setAttributes(getPopupUtility(), this);
063                    updateSize();
064                } else if (val instanceof javax.swing.ImageIcon) {
065                    setIcon((javax.swing.ImageIcon) val);
066                    setText(null);
067                    _text = false;
068                    _icon = true;
069                    updateSize();
070                } else if (val instanceof Number) {
071                    setText(val.toString());
072                    setIcon(null);
073                    _text = true;
074                    _icon = false;
075                    updateSize();
076                } else if (val instanceof jmri.IdTag){
077                    // most IdTags are Reportable objects, so
078                    // this needs to be before Reportable
079                    setText(((jmri.IdTag)val).getDisplayName());
080                    setIcon(null);
081                    _text = true;
082                    _icon = false;
083                    updateSize();
084                } else if (val instanceof Reportable) {
085                    setText(((Reportable)val).toReportString());
086                    setIcon(null);
087                    _text = true;
088                    _icon = false;
089                    updateSize();
090                } else {
091                    log.warn("can't display current value of {}, val= {} of Class {}", getNamedGlobalVariable().getName(), val, val.getClass().getName());
092                }
093            } else {
094                // map exists, use it
095                NamedIcon newicon = map.get(key.toString());
096                if (newicon != null) {
097
098                    setText(null);
099                    super.setIcon(newicon);
100                    _text = false;
101                    _icon = true;
102                    updateSize();
103                } else {
104                    // no match, use default
105                    setIcon(getDefaultIcon());
106
107                    setText(null);
108                    _text = false;
109                    _icon = true;
110                    updateSize();
111                }
112            }
113        } else {
114            setIcon(null);
115            setText(defaultText);
116            _text = true;
117            _icon = false;
118            updateSize();
119        }
120    }
121
122    @Override
123    public void setGlobalVariable(String pName) {
124        super.setGlobalVariable(pName);
125    }
126
127    @Override
128    protected void setValue(Object obj) {
129        getGlobalVariable().setValue(obj);
130        updateSize();
131    }
132
133    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GlobalVariableIcon.class);
134}