001package jmri.jmrit.logixng.expressions.swing;
002
003import javax.annotation.CheckForNull;
004import javax.annotation.Nonnull;
005import javax.swing.JOptionPane;
006import javax.swing.JPanel;
007
008import jmri.*;
009import jmri.jmrit.logixng.*;
010import jmri.jmrit.logixng.implementation.DefaultSymbolTable;
011import jmri.jmrit.logixng.swing.AbstractSwingConfigurator;
012
013/**
014 * Abstract class for SwingConfiguratorInterface
015 * 
016 * @author Daniel Bergqvist Copyright 2021
017 */
018public abstract class AbstractStringExpressionSwing 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.setSymbolTable(symbolTable);
042                String result = ((StringExpression)object).evaluate();
043                jmri.util.ThreadingUtil.runOnGUIEventually(() -> {
044                    JOptionPane.showMessageDialog(null,
045                            Bundle.getMessage("ExecuteEvaluate_EvaluationCompleted", result),
046                            Bundle.getMessage("ExecuteEvaluate_Title"),
047                            JOptionPane.PLAIN_MESSAGE);
048                });
049            } catch (JmriException | RuntimeException e) {
050                log.warn("ConditionalNG {} got an exception during execute: {}",
051                        conditionalNG.getSystemName(), e, e);
052            }
053            
054            conditionalNG.setSymbolTable(oldSymbolTable);
055        });
056    }
057    
058    /** {@inheritDoc} */
059    @Override
060    public BaseManager<? extends NamedBean> getManager() {
061        return InstanceManager.getDefault(StringExpressionManager.class);
062    }
063    
064    /** {@inheritDoc} */
065    @Override
066    public JPanel getConfigPanel(@Nonnull JPanel buttonPanel) throws IllegalArgumentException {
067        createPanel(null, buttonPanel);
068        return panel;
069    }
070    
071    /** {@inheritDoc} */
072    @Override
073    public JPanel getConfigPanel(@Nonnull Base object, @Nonnull JPanel buttonPanel) throws IllegalArgumentException {
074        createPanel(object, buttonPanel);
075        return panel;
076    }
077    
078    protected abstract void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel);
079    
080    /** {@inheritDoc} */
081    @Override
082    public String getExampleSystemName() {
083        return InstanceManager.getDefault(StringExpressionManager.class).getSystemNamePrefix() + "SE10";
084    }
085    
086    /** {@inheritDoc} */
087    @Override
088    public String getAutoSystemName() {
089        return InstanceManager.getDefault(StringExpressionManager.class).getAutoSystemName();
090    }
091    
092    
093    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractStringExpressionSwing.class);
094    
095}