001package jmri.jmrit.logixng.util.configurexml; 002 003import jmri.*; 004import jmri.configurexml.JmriConfigureXmlException; 005import jmri.jmrit.logixng.NamedBeanAddressing; 006import jmri.jmrit.logixng.util.LogixNG_SelectEnum; 007import jmri.jmrit.logixng.util.parser.ParserException; 008 009import org.jdom2.Element; 010 011/** 012 * Xml class for jmri.jmrit.logixng.util.LogixNG_SelectEnum. 013 * 014 * @param <E> the type of enum 015 * 016 * @author Daniel Bergqvist (C) 2022 017 */ 018public class LogixNG_SelectEnumXml<E extends Enum<?>> { 019 020 /** 021 * Default implementation for storing the contents of a LogixNG_SelectEnum 022 * 023 * @param selectEnum the LogixNG_SelectTable object 024 * @param tagName the name of the element 025 * @return Element containing the complete info 026 */ 027 public Element store(LogixNG_SelectEnum<E> selectEnum, String tagName) { 028 029 LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml(); 030 031 Element enumElement = new Element(tagName); 032 033 enumElement.addContent(new Element("addressing").addContent(selectEnum.getAddressing().name())); 034 enumElement.addContent(new Element("enum").addContent(selectEnum.getEnum().name())); 035 if (selectEnum.getReference() != null && !selectEnum.getReference().isEmpty()) { 036 enumElement.addContent(new Element("reference").addContent(selectEnum.getReference())); 037 } 038 var memory = selectEnum.getMemory(); 039 if (memory != null) { 040 enumElement.addContent(new Element("memory").addContent(memory.getName())); 041 } 042 // Not used anymore. It's kept to not change old tables and panels files 043 enumElement.addContent(new Element("listenToMemory").addContent("no")); 044 if (selectEnum.getLocalVariable() != null && !selectEnum.getLocalVariable().isEmpty()) { 045 enumElement.addContent(new Element("localVariable").addContent(selectEnum.getLocalVariable())); 046 } 047 if (selectEnum.getFormula() != null && !selectEnum.getFormula().isEmpty()) { 048 enumElement.addContent(new Element("formula").addContent(selectEnum.getFormula())); 049 } 050 051 if (selectEnum.getAddressing() == NamedBeanAddressing.Table) { 052 enumElement.addContent(selectTableXml.store(selectEnum.getSelectTable(), "table")); 053 } 054 055 return enumElement; 056 } 057 058 public void load(Element enumElement, LogixNG_SelectEnum<E> selectEnum) 059 throws JmriConfigureXmlException { 060 061 if (enumElement != null) { 062 063 LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml(); 064 065 try { 066 Element elem = enumElement.getChild("addressing"); 067 if (elem != null) { 068 selectEnum.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim())); 069 } 070 071 elem = enumElement.getChild("enum"); 072 if (elem != null) { 073 selectEnum.setEnum(selectEnum.getEnum(elem.getTextTrim())); 074 } 075 076 elem = enumElement.getChild("reference"); 077 if (elem != null) selectEnum.setReference(elem.getTextTrim()); 078 079 Element memoryName = enumElement.getChild("memory"); 080 if (memoryName != null) { 081 Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim()); 082 if (m != null) selectEnum.setMemory(m); 083 else selectEnum.removeMemory(); 084 } 085 086 elem = enumElement.getChild("localVariable"); 087 if (elem != null) selectEnum.setLocalVariable(elem.getTextTrim()); 088 089 elem = enumElement.getChild("formula"); 090 if (elem != null) selectEnum.setFormula(elem.getTextTrim()); 091 092 if (enumElement.getChild("table") != null) { 093 selectTableXml.load(enumElement.getChild("table"), selectEnum.getSelectTable()); 094 } 095 096 } catch (ParserException e) { 097 throw new JmriConfigureXmlException(e); 098 } 099 } 100 } 101 102 /** 103 * This method is for backward compability up to and including 4.99.4.Remove this class after 5.0. 104 * 105 * @param shared the shared element 106 * @param selectEnum the LogixNG_SelectEnum 107 * @param addressingElementName the name of the element of the addressing, for example "state" 108 * @param enumElementName the name of the element of the enum, for example "state" 109 * @param referenceElementName the name of the element of the reference, for example "state" 110 * @param localVariableElementName the name of the element of the local variable, for example "state" 111 * @param formulaElementName the name of the element of the formula, for example "state" 112 * @throws JmriConfigureXmlException if an exception occurs 113 */ 114 public void loadLegacy( 115 Element shared, 116 LogixNG_SelectEnum<E> selectEnum, 117 String addressingElementName, 118 String enumElementName, 119 String referenceElementName, 120 String localVariableElementName, 121 String formulaElementName) 122 throws JmriConfigureXmlException { 123 124 Element name = shared.getChild(enumElementName); 125 if (name != null) { 126 selectEnum.setEnum(selectEnum.getEnum(name.getTextTrim())); 127 } 128 129 try { 130 Element elem; 131 if (addressingElementName != null) { 132 elem = shared.getChild(addressingElementName); 133 if (elem != null) { 134 selectEnum.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim())); 135 } 136 } 137 138 if (referenceElementName != null) { 139 elem = shared.getChild(referenceElementName); 140 if (elem != null) selectEnum.setReference(elem.getTextTrim()); 141 } 142 143 if (localVariableElementName != null) { 144 elem = shared.getChild(localVariableElementName); 145 if (elem != null) selectEnum.setLocalVariable(elem.getTextTrim()); 146 } 147 148 if (formulaElementName != null) { 149 elem = shared.getChild(formulaElementName); 150 if (elem != null) selectEnum.setFormula(elem.getTextTrim()); 151 } 152 } catch (ParserException e) { 153 throw new JmriConfigureXmlException(e); 154 } 155 } 156 157}