001package jmri.jmrit.logixng.actions; 002 003import java.util.*; 004 005import jmri.*; 006import jmri.jmrit.logixng.*; 007import jmri.jmrit.logixng.util.*; 008import jmri.jmrit.logixng.util.parser.*; 009import jmri.util.ThreadingUtil; 010 011/** 012 * This action enables/disables a LogixNG. 013 * 014 * @author Daniel Bergqvist Copyright 2024 015 */ 016public class EnableLogixNG extends AbstractDigitalAction { 017 018 private final LogixNG_SelectNamedBean<LogixNG> _selectNamedBean = 019 new LogixNG_SelectNamedBean<>( 020 this, LogixNG.class, InstanceManager.getDefault(LogixNG_Manager.class)); 021 022 private final LogixNG_SelectEnum<Operation> _selectEnum = 023 new LogixNG_SelectEnum<>(this, Operation.values(), Operation.Disable); 024 025 026 public EnableLogixNG(String sys, String user) 027 throws BadUserNameException, BadSystemNameException { 028 super(sys, user); 029 } 030 031 @Override 032 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException { 033 DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class); 034 String sysName = systemNames.get(getSystemName()); 035 String userName = userNames.get(getSystemName()); 036 if (sysName == null) sysName = manager.getAutoSystemName(); 037 EnableLogixNG copy = new EnableLogixNG(sysName, userName); 038 copy.setComment(getComment()); 039 _selectNamedBean.copy(copy._selectNamedBean); 040 _selectEnum.copy(copy._selectEnum); 041 return manager.registerAction(copy); 042 } 043 044 public LogixNG_SelectNamedBean<LogixNG> getSelectNamedBean() { 045 return _selectNamedBean; 046 } 047 048 public LogixNG_SelectEnum<Operation> getSelectEnum() { 049 return _selectEnum; 050 } 051 052 /** {@inheritDoc} */ 053 @Override 054 public LogixNG_Category getCategory() { 055 return LogixNG_Category.ITEM; 056 } 057 058 /** {@inheritDoc} */ 059 @Override 060 public void execute() throws JmriException { 061 LogixNG logixNG = _selectNamedBean.evaluateNamedBean(getConditionalNG()); 062 063 if (logixNG == null) return; 064 065 Operation operation = _selectEnum.evaluateEnum(getConditionalNG()); 066 067 ThreadingUtil.runOnLayoutWithJmriException(() -> { 068 operation.runTask(logixNG); 069 }); 070 } 071 072 @Override 073 public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException { 074 throw new UnsupportedOperationException("Not supported."); 075 } 076 077 @Override 078 public int getChildCount() { 079 return 0; 080 } 081 082 @Override 083 public String getShortDescription(Locale locale) { 084 return Bundle.getMessage(locale, "EnableLogixNG_Short"); 085 } 086 087 @Override 088 public String getLongDescription(Locale locale) { 089 String namedBean = _selectNamedBean.getDescription(locale); 090 String state = _selectEnum.getDescription(locale); 091 092 return Bundle.getMessage(locale, "EnableLogixNG_Long", namedBean, state); 093 } 094 095 /** {@inheritDoc} */ 096 @Override 097 public void setup() { 098 // Do nothing 099 } 100 101 /** {@inheritDoc} */ 102 @Override 103 public void disposeMe() { 104 } 105 106 107 private interface LogixNG_Task { 108 void run(LogixNG logixNG); 109 } 110 111 112 public enum Operation { 113 Enable(Bundle.getMessage("EnableLogixNG_Enable"), (lng) -> {lng.setEnabled(true);}), 114 Disable(Bundle.getMessage("EnableLogixNG_Disable"), (lng) -> {lng.setEnabled(false);}), 115 Activate(Bundle.getMessage("EnableLogixNG_Activate"), (lng) -> {lng.setActive(true);}), 116 Deactivate(Bundle.getMessage("EnableLogixNG_Deactivate"), (lng) -> {lng.setActive(false);}); 117 118 private final String _text; 119 private final LogixNG_Task _task; 120 121 private Operation(String text, LogixNG_Task task) { 122 this._text = text; 123 this._task = task; 124 } 125 126 public void runTask(LogixNG logixNG) { 127 _task.run(logixNG); 128 } 129 130 @Override 131 public String toString() { 132 return _text; 133 } 134 135 } 136 137// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(EnableLogix.class); 138 139}