001package jmri.jmrit.logixng.expressions; 002 003import java.beans.PropertyChangeEvent; 004import java.beans.PropertyChangeListener; 005import java.util.*; 006 007import jmri.*; 008import jmri.jmrit.logixng.*; 009import jmri.jmrit.logixng.util.LogixNG_SelectNamedBean; 010 011/** 012 * Reads a Memory. 013 * 014 * @author Daniel Bergqvist Copyright 2018 015 */ 016public class StringExpressionMemory extends AbstractStringExpression 017 implements PropertyChangeListener { 018 019 private final LogixNG_SelectNamedBean<Memory> _selectNamedBean = 020 new LogixNG_SelectNamedBean<>( 021 this, Memory.class, InstanceManager.getDefault(MemoryManager.class), this); 022 023 public StringExpressionMemory(String sys, String user) 024 throws BadUserNameException, BadSystemNameException { 025 026 super(sys, user); 027 } 028 029 @Override 030 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws JmriException { 031 StringExpressionManager manager = InstanceManager.getDefault(StringExpressionManager.class); 032 String sysName = systemNames.get(getSystemName()); 033 String userName = userNames.get(getSystemName()); 034 if (sysName == null) sysName = manager.getAutoSystemName(); 035 StringExpressionMemory copy = new StringExpressionMemory(sysName, userName); 036 copy.setComment(getComment()); 037 _selectNamedBean.copy(copy._selectNamedBean); 038 return manager.registerExpression(copy).deepCopyChildren(this, systemNames, userNames); 039 } 040 041 public LogixNG_SelectNamedBean<Memory> getSelectNamedBean() { 042 return _selectNamedBean; 043 } 044 045 /** {@inheritDoc} */ 046 @Override 047 public Category getCategory() { 048 return Category.ITEM; 049 } 050 051 /** {@inheritDoc} */ 052 @Override 053 public String evaluate() throws JmriException { 054 Memory memory = _selectNamedBean.evaluateNamedBean(getConditionalNG()); 055 if (memory != null) { 056 return jmri.util.TypeConversionUtil.convertToString(memory.getValue(), false); 057 } else { 058 return ""; 059 } 060 } 061 062 @Override 063 public FemaleSocket getChild(int index) 064 throws IllegalArgumentException, UnsupportedOperationException { 065 throw new UnsupportedOperationException("Not supported."); 066 } 067 068 @Override 069 public int getChildCount() { 070 return 0; 071 } 072 073 @Override 074 public String getShortDescription(Locale locale) { 075 return Bundle.getMessage(locale, "StringExpressionMemory_Short"); 076 } 077 078 @Override 079 public String getLongDescription(Locale locale) { 080 return Bundle.getMessage(locale, "StringExpressionMemory_Long", _selectNamedBean.getDescription(locale)); 081 } 082 083 /** {@inheritDoc} */ 084 @Override 085 public void setup() { 086 // Do nothing 087 } 088 089 /** {@inheritDoc} */ 090 @Override 091 public void registerListenersForThisClass() { 092 if (!_listenersAreRegistered) { 093 _selectNamedBean.addPropertyChangeListener("value", this); 094 _selectNamedBean.registerListeners(); 095 _listenersAreRegistered = true; 096 } 097 } 098 099 /** {@inheritDoc} */ 100 @Override 101 public void unregisterListenersForThisClass() { 102 if (_listenersAreRegistered) { 103 _selectNamedBean.removePropertyChangeListener("value", this); 104 _selectNamedBean.unregisterListeners(); 105 _listenersAreRegistered = false; 106 } 107 } 108 109 /** {@inheritDoc} */ 110 @Override 111 public void propertyChange(PropertyChangeEvent evt) { 112 if (getTriggerOnChange()) { 113 getConditionalNG().execute(); 114 } 115 } 116 117 /** {@inheritDoc} */ 118 @Override 119 public void disposeMe() { 120 } 121 122 /** {@inheritDoc} */ 123 @Override 124 public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) { 125 log.debug("getUsageReport :: StringExpressionMemory: bean = {}, report = {}", cdl, report); 126 _selectNamedBean.getUsageDetail(level, bean, report, cdl, this, LogixNG_SelectNamedBean.Type.Expression); 127 } 128 129 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StringExpressionMemory.class); 130 131}