001package jmri.jmrit.logixng.expressions.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 2018
017 */
018public abstract class AbstractDigitalExpressionSwing 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                boolean result = ((DigitalExpression)object).evaluate();
044                jmri.util.ThreadingUtil.runOnGUIEventually(() -> {
045                    JmriJOptionPane.showMessageDialog(null,
046                            Bundle.getMessage("ExecuteEvaluate_EvaluationCompleted", result),
047                            Bundle.getMessage("ExecuteEvaluate_Title"),
048                            JmriJOptionPane.PLAIN_MESSAGE);
049                });
050            } catch (JmriException | RuntimeException e) {
051                log.warn("ConditionalNG {} got an exception during execute: {}",
052                        conditionalNG.getSystemName(), e, e);
053            }
054
055            conditionalNG.setSymbolTable(oldSymbolTable);
056        });
057    }
058
059    /** {@inheritDoc} */
060    @Override
061    public BaseManager<? extends NamedBean> getManager() {
062        return InstanceManager.getDefault(DigitalExpressionManager.class);
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public JPanel getConfigPanel(@Nonnull JPanel buttonPanel) throws IllegalArgumentException {
068        createPanel(null, buttonPanel);
069        return panel;
070    }
071
072    /** {@inheritDoc} */
073    @Override
074    public JPanel getConfigPanel(@Nonnull Base object, @Nonnull JPanel buttonPanel) throws IllegalArgumentException {
075        createPanel(object, buttonPanel);
076        return panel;
077    }
078
079    protected abstract void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel);
080
081    /** {@inheritDoc} */
082    @Override
083    public String getExampleSystemName() {
084        return InstanceManager.getDefault(DigitalExpressionManager.class).getSystemNamePrefix() + "DE10";
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public String getAutoSystemName() {
090        return InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName();
091    }
092
093
094    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractDigitalExpressionSwing.class);
095
096}