001package jmri.jmrit.logixng.expressions; 002 003import java.beans.*; 004import java.util.*; 005 006import jmri.*; 007import jmri.jmrit.logixng.*; 008import jmri.jmrit.logixng.util.LogixNG_SelectNamedBean; 009import jmri.jmrit.logixng.util.parser.ParserException; 010 011/** 012 * Returns the last result of a digital expression. 013 * 014 * @author Daniel Bergqvist Copyright 2021 015 */ 016public class LastResultOfDigitalExpression extends AbstractDigitalExpression 017 implements PropertyChangeListener, VetoableChangeListener { 018 019 private final LogixNG_SelectNamedBean<MaleDigitalExpressionSocket> _selectNamedBean = 020 new LogixNG_SelectNamedBean<>( 021 this, MaleDigitalExpressionSocket.class, InstanceManager.getDefault(DigitalExpressionManager.class), this); 022 023 public LastResultOfDigitalExpression(String sys, String user) 024 throws BadUserNameException, BadSystemNameException { 025 super(sys, user); 026 _selectNamedBean.setOnlyDirectAddressingAllowed(); 027 } 028 029 @Override 030 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException { 031 DigitalExpressionManager manager = InstanceManager.getDefault(DigitalExpressionManager.class); 032 String sysName = systemNames.get(getSystemName()); 033 String userName = userNames.get(getSystemName()); 034 if (sysName == null) sysName = manager.getAutoSystemName(); 035 LastResultOfDigitalExpression copy = new LastResultOfDigitalExpression(sysName, userName); 036 copy.setComment(getComment()); 037 _selectNamedBean.copy(copy._selectNamedBean); 038 return manager.registerExpression(copy); 039 } 040 041 public LogixNG_SelectNamedBean<MaleDigitalExpressionSocket> getSelectNamedBean() { 042 return _selectNamedBean; 043 } 044 045 /** {@inheritDoc} */ 046 @Override 047 public Category getCategory() { 048 return Category.OTHER; 049 } 050 051 /** {@inheritDoc} */ 052 @Override 053 public boolean evaluate() throws JmriException { 054 MaleDigitalExpressionSocket expression = 055 _selectNamedBean.evaluateNamedBean(getConditionalNG()); 056 057 if (expression != null) return expression.getLastResult(); 058 059 return false; 060 } 061 062 @Override 063 public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException { 064 throw new UnsupportedOperationException("Not supported."); 065 } 066 067 @Override 068 public int getChildCount() { 069 return 0; 070 } 071 072 @Override 073 public String getShortDescription(Locale locale) { 074 return Bundle.getMessage(locale, "LastResultOfDigitalExpression_Short"); 075 } 076 077 @Override 078 public String getLongDescription(Locale locale) { 079 String beanName = _selectNamedBean.getDescription(locale); 080 081 return Bundle.getMessage(locale, "LastResultOfDigitalExpression_Long", 082 beanName); 083 } 084 085 /** {@inheritDoc} */ 086 @Override 087 public void setup() { 088 _selectNamedBean.setup(); 089 } 090 091 /** {@inheritDoc} */ 092 @Override 093 public void registerListenersForThisClass() { 094 if (!_listenersAreRegistered) { 095 _selectNamedBean.addPropertyChangeListener(PROPERTY_LAST_RESULT_CHANGED, this); 096 _selectNamedBean.registerListeners(); 097 _listenersAreRegistered = true; 098 } 099 } 100 101 /** {@inheritDoc} */ 102 @Override 103 public void unregisterListenersForThisClass() { 104 if (_listenersAreRegistered) { 105 _selectNamedBean.removePropertyChangeListener(PROPERTY_LAST_RESULT_CHANGED, this); 106 _selectNamedBean.unregisterListeners(); 107 _listenersAreRegistered = false; 108 } 109 } 110 111 /** {@inheritDoc} */ 112 @Override 113 public void propertyChange(PropertyChangeEvent evt) { 114 getConditionalNG().execute(); 115 } 116 117 /** {@inheritDoc} */ 118 @Override 119 public void disposeMe() { 120 } 121 122// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LastResultOfDigitalExpression.class); 123 124}