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;
}
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);
}
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);
}
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();
}
}
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);
}
Aggregations