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