001package jmri.jmrit.logixng.actions.swing;
002
003import javax.annotation.CheckForNull;
004import javax.annotation.Nonnull;
005import javax.swing.JPanel;
006
007import jmri.*;
008import jmri.jmrit.logixng.*;
009import jmri.jmrit.logixng.implementation.DefaultSymbolTable;
010import jmri.jmrit.logixng.swing.AbstractSwingConfigurator;
011import jmri.util.swing.JmriJOptionPane;
012
013/**
014 * Abstract class for SwingConfiguratorInterface
015 *
016 * @author Daniel Bergqvist Copyright 2021
017 */
018public abstract class AbstractDigitalActionSwing extends AbstractSwingConfigurator {
019
020    protected JPanel panel;
021
022    /** {@inheritDoc} */
023    @Override
024    public String getExecuteEvaluateMenuText() {
025        return Bundle.getMessage("MenuText_ExecuteEvaluate");
026    }
027
028    /** {@inheritDoc} */
029    @Override
030    public void executeEvaluate(@Nonnull Base object) {
031        ConditionalNG conditionalNG = object.getConditionalNG();
032        if (conditionalNG == null) throw new RuntimeException("Not supported yet");
033
034        SymbolTable symbolTable = new DefaultSymbolTable();
035        getAllSymbols(object, symbolTable);
036
037        conditionalNG.getCurrentThread().runOnLogixNGEventually(() -> {
038            SymbolTable oldSymbolTable = conditionalNG.getSymbolTable();
039
040            try {
041                conditionalNG.setCurrentConditionalNG(conditionalNG);
042                conditionalNG.setSymbolTable(symbolTable);
043                ((DigitalAction)object).execute();
044                jmri.util.ThreadingUtil.runOnGUIEventually(() -> {
045                    JmriJOptionPane.showMessageDialog(null,
046                            Bundle.getMessage("ExecuteEvaluate_ActionCompleted"),
047                            Bundle.getMessage("ExecuteEvaluate_Title"),
048                            JmriJOptionPane.PLAIN_MESSAGE);
049                });
050            } catch (JmriException | RuntimeException e) {
051    //                LoggingUtil.warnOnce(log, "ConditionalNG {} got an exception during execute: {}",
052    //                        conditionalNG.getSystemName(), e, e);
053                log.warn("ConditionalNG {} got an exception during execute: {}",
054                        conditionalNG.getSystemName(), e, e);
055            }
056
057            conditionalNG.setSymbolTable(oldSymbolTable);
058        });
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    public BaseManager<? extends NamedBean> getManager() {
064        return InstanceManager.getDefault(DigitalActionManager.class);
065    }
066
067    /** {@inheritDoc} */
068    @Override
069    public JPanel getConfigPanel(@Nonnull JPanel buttonPanel) throws IllegalArgumentException {
070        createPanel(null, buttonPanel);
071        return panel;
072    }
073
074    /** {@inheritDoc} */
075    @Override
076    public JPanel getConfigPanel(@Nonnull Base object, @Nonnull JPanel buttonPanel) throws IllegalArgumentException {
077        createPanel(object, buttonPanel);
078        return panel;
079    }
080
081    protected abstract void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel);
082
083    /** {@inheritDoc} */
084    @Override
085    public String getExampleSystemName() {
086        return InstanceManager.getDefault(DigitalActionManager.class).getSystemNamePrefix() + "DA10";
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public String getAutoSystemName() {
092        return InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName();
093    }
094
095
096    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractDigitalActionSwing.class);
097
098}