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