001package jmri.jmrit.operations.automation;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005
006import javax.swing.*;
007
008import jmri.InstanceManager;
009import jmri.jmrit.operations.OperationsFrame;
010import jmri.jmrit.operations.OperationsXml;
011import jmri.jmrit.operations.setup.Control;
012import jmri.jmrit.operations.setup.Setup;
013
014/**
015 * Frame for user selection of a startup automation
016 *
017 * @author Bob Jacobsen Copyright (C) 2004
018 * @author Dan Boudreau Copyright (C) 2022
019 */
020public class AutomationStartupFrame extends OperationsFrame implements java.beans.PropertyChangeListener {
021
022    AutomationManager automationManager = InstanceManager.getDefault(AutomationManager.class);
023
024    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
025    JButton testButton = new JButton(Bundle.getMessage("ButtonTest"));
026    
027    JComboBox<Automation> comboBox = automationManager.getComboBox();
028
029    public AutomationStartupFrame() {
030        super(Bundle.getMessage("MenuStartupAutomation"));
031        // general GUI config
032        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
033
034        // setup by rows
035        JPanel pAutomation = new JPanel();
036        pAutomation.setLayout(new GridBagLayout());
037        pAutomation.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("MenuStartupAutomation")));
038        addItem(pAutomation, comboBox, 0, 0);
039
040        JPanel pB = new JPanel();
041        pB.setLayout(new GridBagLayout());
042        pB.setBorder(BorderFactory.createTitledBorder(""));
043        addItem(pB, testButton, 0, 0);
044        addItem(pB, saveButton, 1, 0);
045
046        getContentPane().add(pAutomation);
047        getContentPane().add(pB);
048
049        addButtonAction(testButton);
050        addButtonAction(saveButton);
051        comboBox.setSelectedItem(automationManager.getStartupAutomation());
052        
053        testButton.setToolTipText(Bundle.getMessage("ButtonTestTip"));
054
055        automationManager.addPropertyChangeListener(this);
056
057        initMinimumSize(new Dimension(Control.panelWidth500, Control.panelHeight200));
058    }
059
060    @Override
061    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
062        if (ae.getSource() == testButton) {
063            automationManager.runStartupAutomation();
064        }
065        if (ae.getSource() == saveButton) {
066            automationManager.setStartupAutomation((Automation) comboBox.getSelectedItem());
067            OperationsXml.save();
068            if (Setup.isCloseWindowOnSaveEnabled()) {
069                dispose();
070            }
071        }
072    }
073
074    @Override
075    public void dispose() {
076        automationManager.removePropertyChangeListener(this);
077        super.dispose();
078    }
079
080    @Override
081    public void propertyChange(java.beans.PropertyChangeEvent e) {
082        if (e.getPropertyName().equals(AutomationManager.LISTLENGTH_CHANGED_PROPERTY)) {
083            automationManager.updateComboBox(comboBox);
084            comboBox.setSelectedItem(automationManager.getStartupAutomation());
085        }
086    }
087}