001package jmri.jmrit.display.configurexml; 002 003import jmri.configurexml.JmriConfigureXmlException; 004import jmri.jmrit.catalog.NamedIcon; 005import jmri.jmrit.display.Editor; 006import jmri.jmrit.display.LogixNGIcon; 007import jmri.jmrit.display.Positionable; 008 009import org.jdom2.Attribute; 010import org.jdom2.Element; 011 012/** 013 * Handle configuration for display.LogixNGIcon objects. 014 * 015 * @author Daniel Bergqvist (C) 2023 016 */ 017public class LogixNGIconXml extends PositionableLabelXml { 018 019 /** 020 * Default implementation for storing the contents of a LogixNGIcon 021 * 022 * @param o Object to store, of type LogixNGIcon 023 * @return Element containing the complete info 024 */ 025 @Override 026 public Element store(Object o) { 027 LogixNGIcon p = (LogixNGIcon) o; 028 029 if (!p.isActive()) { 030 return null; // if flagged as inactive, don't store 031 } 032 Element element = new Element("logixngicon"); 033 storeCommonAttributes(p, element); 034 035 element.addContent(new Element("Identity").addContent(Integer.toString(p.getIdentity()))); 036 037 if (p.isText()) { 038 if (p.getUnRotatedText() != null) { 039 element.setAttribute("text", p.getUnRotatedText()); 040 } 041 storeTextInfo(p, element); 042 } 043 044 if (p.isIcon() && p.getIcon() != null) { 045 element.setAttribute("icon", "yes"); 046 element.addContent(storeIcon("icon", (NamedIcon) p.getIcon())); 047 } 048 049 storeLogixNG_Data(p, element); 050 051 element.setAttribute("class", "jmri.jmrit.display.configurexml.LogixNGIconXml"); 052 return element; 053 } 054 055 /** 056 * Create a LogixNGIcon, then add to a target JLayeredPane 057 * 058 * @param element Top level Element to unpack. 059 * @param o Editor as an Object 060 * @throws JmriConfigureXmlException when a error prevents creating the objects as as 061 * required by the input XML 062 */ 063 @Override 064 public void load(Element element, Object o) throws JmriConfigureXmlException { 065 // create the objects 066 LogixNGIcon l = null; 067 068 int identity = Integer.parseInt(element.getChildText("Identity")); 069 070 // get object class and determine editor being used 071 Editor editor = (Editor) o; 072 if (element.getAttribute("icon") != null) { 073 NamedIcon icon; 074 String name = element.getAttribute("icon").getValue(); 075// if (log.isDebugEnabled()) log.debug("icon attribute= "+name); 076 if (name.equals("yes")) { 077 icon = getNamedIcon("icon", element, "LogixNGIcon ", editor); 078 } else { 079 icon = NamedIcon.getIconByName(name); 080 if (icon == null) { 081 icon = editor.loadFailed("LogixNGIcon", name); 082 if (icon == null) { 083 log.info("LogixNGIcon icon removed for url= {}", name); 084 return; 085 } 086 } 087 } 088 // abort if name != yes and have null icon 089 if (icon == null && !name.equals("yes")) { 090 log.info("LogixNGIcon icon removed for url= {}", name); 091 return; 092 } 093 l = new LogixNGIcon(identity, icon, editor); 094 try { 095 Attribute a = element.getAttribute("rotate"); 096 if (a != null && icon != null) { 097 int rotation = element.getAttribute("rotate").getIntValue(); 098 icon.setRotation(rotation, l); 099 } 100 } catch (org.jdom2.DataConversionException e) { 101 } 102 103 if (name.equals("yes")) { 104 NamedIcon nIcon = loadIcon(l, "icon", element, "LogixNGIcon ", editor); 105 if (nIcon != null) { 106 l.updateIcon(nIcon); 107 } else { 108 log.info("LogixNGIcon icon removed for url= {}", name); 109 return; 110 } 111 } else { 112 l.updateIcon(icon); 113 } 114 } 115 116 if (element.getAttribute("text") != null) { 117 if (l == null) { 118 l = new LogixNGIcon(identity, element.getAttribute("text").getValue(), editor); 119 } 120 loadTextInfo(l, element); 121 122 } else if (l == null) { 123 log.error("LogixNGIcon is null!"); 124 if (log.isDebugEnabled()) { 125 java.util.List<Attribute> attrs = element.getAttributes(); 126 log.debug("\tElement Has {} Attributes:", attrs.size()); 127 for (Attribute a : attrs) { 128 log.debug(" attribute: {} = {}", a.getName(), a.getValue()); 129 } 130 java.util.List<Element> kids = element.getChildren(); 131 log.debug("\tElementHas {} children:", kids.size()); 132 for (Element e : kids) { 133 log.debug(" child: {} = \"{}\"", e.getName(), e.getValue()); 134 } 135 } 136 editor.loadFailed(); 137 return; 138 } 139 try { 140 editor.putItem(l); 141 } catch (Positionable.DuplicateIdException e) { 142 // This should never happen 143 log.error("Editor.putItem() with null id has thrown DuplicateIdException", e); 144 } 145 146 loadLogixNG_Data(l, element); 147 148 // load individual item's option settings after editor has set its global settings 149 loadCommonAttributes(l, Editor.LABELS, element); 150 } 151 152 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogixNGIconXml.class); 153}