001package jmri.jmrit.display.configurexml; 002 003import jmri.configurexml.JmriConfigureXmlException; 004import jmri.jmrit.display.*; 005import jmri.jmrit.logixng.GlobalVariable; 006import jmri.jmrit.logixng.GlobalVariableManager; 007 008import org.jdom2.Attribute; 009import org.jdom2.Element; 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013/** 014 * Handle configuration for display.GlobalVariableSpinnerIcon objects. 015 * 016 * @author Bob Jacobsen Copyright: Copyright (c) 2009 017 * @author Daniel Bergqvist Copyright (C) 2022 018 */ 019public class GlobalVariableInputIconXml extends PositionableLabelXml { 020 021 public GlobalVariableInputIconXml() { 022 } 023 024 /** 025 * Default implementation for storing the contents of a GlobalVariableSpinnerIcon 026 * 027 * @param o Object to store, of type GlobalVariableSpinnerIcon 028 * @return Element containing the complete info 029 */ 030 @Override 031 public Element store(Object o) { 032 033 GlobalVariableInputIcon p = (GlobalVariableInputIcon) o; 034 035 Element element = new Element("globalVariableInputIcon"); 036 037 // include attributes 038 element.setAttribute("colWidth", "" + p.getNumColumns()); 039 element.setAttribute("globalVariable", p.getNamedGlobalVariable().getName()); 040 storeCommonAttributes(p, element); 041 storeTextInfo(p, element); 042 043 storeLogixNG_Data(p, element); 044 045 element.setAttribute("class", "jmri.jmrit.display.configurexml.GlobalVariableInputIconXml"); 046 return element; 047 } 048 049 /** 050 * Load, starting with the globalVariableInputIcon element, then all the value-icon 051 * pairs 052 * 053 * @param element Top level Element to unpack. 054 * @param o an Editor as an Object 055 * @throws JmriConfigureXmlException when a error prevents creating the objects as as 056 * required by the input XML 057 */ 058 @Override 059 public void load(Element element, Object o) throws JmriConfigureXmlException { 060 // create the objects 061 Editor p = (Editor) o; 062 063 int nCol = 2; 064 try { 065 nCol = element.getAttribute("colWidth").getIntValue(); 066 } catch (org.jdom2.DataConversionException e) { 067 log.error("failed to convert colWidth attribute"); 068 } 069 070 GlobalVariableInputIcon l = new GlobalVariableInputIcon(nCol, p); 071 072 loadTextInfo(l, element); 073 String name; 074 Attribute attr = element.getAttribute("globalVariable"); 075 if (attr == null) { 076 log.error("incorrect information for a globalVariable location; must use globalVariable name"); 077 p.loadFailed(); 078 return; 079 } else { 080 name = attr.getValue(); 081 } 082 083 GlobalVariable m = jmri.InstanceManager.getDefault(GlobalVariableManager.class).getGlobalVariable(name); 084 085 if (m != null) { 086 l.setGlobalVariable(name); 087 } else { 088 log.error("GlobalVariable named '{}' not found.", attr.getValue()); 089 p.loadFailed(); 090 return; 091 } 092 093 try { 094 p.putItem(l); 095 } catch (Positionable.DuplicateIdException e) { 096 throw new JmriConfigureXmlException("Positionable id is not unique", e); 097 } 098 099 loadLogixNG_Data(l, element); 100 101 // load individual item's option settings after editor has set its global settings 102 loadCommonAttributes(l, Editor.MEMORIES, element); 103 104 javax.swing.JComponent textField = l.getTextComponent(); 105 jmri.jmrit.display.PositionablePopupUtil util = l.getPopupUtility(); 106 if (util.hasBackground()) { 107 textField.setBackground(util.getBackground()); 108 } else { 109 textField.setBackground(null); 110 textField.setOpaque(false); 111 } 112 } 113 114 private final static Logger log = LoggerFactory.getLogger(GlobalVariableInputIconXml.class); 115}