001package jmri.jmrit.logixng.actions.swing; 002 003import java.util.List; 004 005import javax.annotation.CheckForNull; 006import javax.annotation.Nonnull; 007import javax.swing.*; 008 009import jmri.InstanceManager; 010import jmri.Turnout; 011import jmri.TurnoutManager; 012import jmri.jmrit.logixng.*; 013import jmri.jmrit.logixng.actions.ActionTurnout; 014import jmri.jmrit.logixng.actions.ActionTurnout.TurnoutState; 015import jmri.jmrit.logixng.swing.SwingConfiguratorInterface; 016import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing; 017import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing; 018 019/** 020 * Configures an ActionTurnout object with a Swing JPanel. 021 * 022 * @author Daniel Bergqvist Copyright 2021 023 */ 024public class ActionTurnoutSwing extends AbstractDigitalActionSwing { 025 026 private LogixNG_SelectNamedBeanSwing<Turnout> _selectNamedBeanSwing; 027 private LogixNG_SelectEnumSwing<TurnoutState> _selectEnumSwing; 028 029 030 public ActionTurnoutSwing() { 031 } 032 033 public ActionTurnoutSwing(JDialog dialog) { 034 super.setJDialog(dialog); 035 } 036 037 @Override 038 protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) { 039 ActionTurnout action = (ActionTurnout)object; 040 041 _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>( 042 InstanceManager.getDefault(TurnoutManager.class), getJDialog(), this); 043 044 _selectEnumSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this); 045 046 panel = new JPanel(); 047 048 JPanel tabbedPaneNamedBean; 049 JPanel tabbedPaneState; 050 051 if (action != null) { 052 tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(action.getSelectNamedBean()); 053 tabbedPaneState = _selectEnumSwing.createPanel(action.getSelectEnum(), TurnoutState.values()); 054 } else { 055 tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null); 056 tabbedPaneState = _selectEnumSwing.createPanel(null, TurnoutState.values()); 057 } 058 059 JComponent[] components = new JComponent[]{ 060 tabbedPaneNamedBean, 061 tabbedPaneState}; 062 063 List<JComponent> componentList = SwingConfiguratorInterface.parseMessage( 064 Bundle.getMessage("ActionTurnout_Components"), components); 065 066 for (JComponent c : componentList) panel.add(c); 067 } 068 069 /** {@inheritDoc} */ 070 @Override 071 public boolean validate(@Nonnull List<String> errorMessages) { 072 // Create a temporary action to test formula 073 ActionTurnout action = new ActionTurnout("IQDA1", null); 074 075 _selectNamedBeanSwing.validate(action.getSelectNamedBean(), errorMessages); 076 _selectEnumSwing.validate(action.getSelectEnum(), errorMessages); 077 078 return errorMessages.isEmpty(); 079 } 080 081 /** {@inheritDoc} */ 082 @Override 083 public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) { 084 ActionTurnout action = new ActionTurnout(systemName, userName); 085 updateObject(action); 086 return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action); 087 } 088 089 /** {@inheritDoc} */ 090 @Override 091 public void updateObject(@Nonnull Base object) { 092 if (! (object instanceof ActionTurnout)) { 093 throw new IllegalArgumentException("object must be an ActionTurnout but is a: "+object.getClass().getName()); 094 } 095 ActionTurnout action = (ActionTurnout)object; 096 _selectNamedBeanSwing.updateObject(action.getSelectNamedBean()); 097 _selectEnumSwing.updateObject(action.getSelectEnum()); 098 } 099 100 /** {@inheritDoc} */ 101 @Override 102 public String toString() { 103 return Bundle.getMessage("Turnout_Short"); 104 } 105 106 @Override 107 public void dispose() { 108 _selectNamedBeanSwing.dispose(); 109 _selectEnumSwing.dispose(); 110 } 111 112 113// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionTurnoutSwing.class); 114 115}