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