001package jmri.jmrit.operations.logixng; 002 003import java.beans.PropertyChangeEvent; 004import java.beans.PropertyChangeListener; 005import java.util.*; 006 007import jmri.*; 008import jmri.jmrit.logixng.*; 009import jmri.jmrit.logixng.actions.AbstractDigitalAction; 010import jmri.jmrit.logixng.util.*; 011import jmri.jmrit.logixng.util.parser.*; 012import jmri.jmrit.operations.automation.Automation; 013import jmri.jmrit.operations.automation.AutomationManager; 014import jmri.util.ThreadingUtil; 015 016/** 017 * This action starts an OperationsPro automation. 018 * 019 * @author Daniel Bergqvist Copyright 2025 020 */ 021public class OperationsProStartAutomation extends AbstractDigitalAction 022 implements PropertyChangeListener { 023 024 private final LogixNG_SelectComboBox _selectAutomation = 025 new LogixNG_SelectComboBox(this, new AutomationItem[]{}, null); 026 027 private final AutomationManager _automationManager; 028 private final List<AutomationItem> _automationList = new ArrayList<>(); 029 030 031 public OperationsProStartAutomation(String sys, String user) 032 throws BadUserNameException, BadSystemNameException { 033 super(sys, user); 034 035 _automationManager = InstanceManager.getDefault(AutomationManager.class); 036 updateList(); 037 _automationManager.addPropertyChangeListener(AutomationManager.LISTLENGTH_CHANGED_PROPERTY, this); 038 } 039 040 @Override 041 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException { 042 DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class); 043 String sysName = systemNames.get(getSystemName()); 044 String userName = userNames.get(getSystemName()); 045 if (sysName == null) sysName = manager.getAutoSystemName(); 046 OperationsProStartAutomation copy = new OperationsProStartAutomation(sysName, userName); 047 copy.setComment(getComment()); 048 _selectAutomation.copy(copy._selectAutomation); 049 return manager.registerAction(copy); 050 } 051 052 public LogixNG_SelectComboBox getSelectAutomations() { 053 return _selectAutomation; 054 } 055 056 /** {@inheritDoc} */ 057 @Override 058 public LogixNG_Category getCategory() { 059 return CategoryOperations.OPERATIONS; 060 } 061 062 /** {@inheritDoc} */ 063 @Override 064 public void execute() throws JmriException { 065 AutomationItem automation = (AutomationItem) _selectAutomation 066 .evaluateValue(getConditionalNG()); 067 068 if (automation != null) { 069 ThreadingUtil.runOnGUIWithJmriException(() -> { 070 automation._automation.run(); 071 }); 072 } 073 } 074 075 @Override 076 public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException { 077 throw new UnsupportedOperationException("Not supported."); 078 } 079 080 @Override 081 public int getChildCount() { 082 return 0; 083 } 084 085 @Override 086 public String getShortDescription(Locale locale) { 087 return Bundle.getMessage(locale, "OperationsProStartAutomation_Short"); 088 } 089 090 @Override 091 public String getLongDescription(Locale locale) { 092 String automation = _selectAutomation.getDescription(locale); 093 094 return Bundle.getMessage(locale, "OperationsProStartAutomation_Long", automation); 095 } 096 097 /** {@inheritDoc} */ 098 @Override 099 public void setup() { 100 // Do nothing 101 } 102 103 /** {@inheritDoc} */ 104 @Override 105 public void propertyChange(PropertyChangeEvent evt) { 106 if (AutomationManager.LISTLENGTH_CHANGED_PROPERTY.equals(evt.getPropertyName())) { 107 updateList(); 108 } 109 } 110 111 private void updateList() { 112 _automationList.clear(); 113 for (Automation automation : _automationManager.getAutomationsByNameList()) { 114 _automationList.add(new AutomationItem(automation)); 115 } 116 AutomationItem[] automations = _automationList.toArray(AutomationItem[]::new); 117 _selectAutomation.setValues(automations); 118 } 119 120 /** {@inheritDoc} */ 121 @Override 122 public void disposeMe() { 123 } 124 125 126 public static class AutomationItem implements LogixNG_SelectComboBox.Item { 127 128 private final Automation _automation; 129 130 public AutomationItem(Automation automation) { 131 this._automation = automation; 132 } 133 134 @Override 135 public String getKey() { 136 return _automation.getId(); 137 } 138 139 @Override 140 public String toString() { 141 return _automation.getName(); 142 } 143 } 144 145// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionSensor.class); 146}