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