001package jmri.jmrit.operations.automation;
002
003import java.awt.Dimension;
004import java.awt.FlowLayout;
005
006import javax.swing.*;
007
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011import jmri.InstanceManager;
012import jmri.jmrit.operations.OperationsFrame;
013import jmri.jmrit.operations.setup.Control;
014import jmri.swing.JTablePersistenceManager;
015
016/**
017 * Frame for adding and editing the automation roster for operations.
018 *
019 * @author Bob Jacobsen Copyright (C) 2001
020 * @author Daniel Boudreau Copyright (C) 2016
021 */
022public class AutomationsTableFrame extends OperationsFrame {
023
024    AutomationsTableModel automationsModel = new AutomationsTableModel();
025    javax.swing.JTable automationsTable = new javax.swing.JTable(automationsModel);
026    JScrollPane automationsPane;
027
028    // labels
029    javax.swing.JLabel textSort = new javax.swing.JLabel();
030    javax.swing.JLabel textSep = new javax.swing.JLabel();
031
032    // radio buttons
033    javax.swing.JRadioButton sortByNameRadioButton = new javax.swing.JRadioButton(Bundle.getMessage("Name"));
034    javax.swing.JRadioButton sortByIdRadioButton = new javax.swing.JRadioButton(Bundle.getMessage("Id"));
035
036    // major buttons
037    JButton addButton = new JButton(Bundle.getMessage("AddAutomation"));
038    
039    public AutomationsTableFrame() {
040        super(Bundle.getMessage("TitleAutomationsTableFrame"));
041        // general GUI config
042
043        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
044
045        // Set up the jtable in a Scroll Pane..
046        automationsPane = new JScrollPane(automationsTable);
047        automationsPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
048        automationsPane
049                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
050        automationsModel.initTable(this, automationsTable);
051        getContentPane().add(automationsPane);
052
053        // Set up the control panel
054        JPanel controlPanel = new JPanel();
055        controlPanel.setLayout(new FlowLayout());
056
057        textSort.setText(Bundle.getMessage("SortBy"));
058        controlPanel.add(textSort);
059        controlPanel.add(sortByNameRadioButton);
060        sortByNameRadioButton.setSelected(true);
061        controlPanel.add(sortByIdRadioButton);
062        textSep.setText("          ");
063        controlPanel.add(textSep);
064        controlPanel.add(addButton);
065
066        controlPanel.setMaximumSize(new Dimension(Control.panelWidth1025, 50));
067        getContentPane().add(controlPanel);
068
069        // setup buttons
070        addButtonAction(addButton);
071        addRadioButtonAction(sortByNameRadioButton);
072        addRadioButtonAction(sortByIdRadioButton);
073
074        // build menu
075        JMenuBar menuBar = new JMenuBar();
076        JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools"));
077        menuBar.add(toolMenu);
078        toolMenu.add(new AutomationCopyAction());
079        toolMenu.add(new AutomationResumeAction());
080        toolMenu.add(new AutomationStartupAction());
081        setJMenuBar(menuBar);
082        addHelpMenu("package.jmri.jmrit.operations.Operations_Automation", true); // NOI18N
083
084        initMinimumSize();
085        // make panel a bit wider than minimum if the very first time opened
086        if (getWidth() == Control.panelWidth500) {
087            setSize(Control.panelWidth1025, getHeight());
088        }
089    }
090
091    @Override
092    public void radioButtonActionPerformed(java.awt.event.ActionEvent ae) {
093        log.debug("radio button activated");
094        // clear any sorts by column
095        clearTableSort(automationsTable);
096        if (ae.getSource() == sortByNameRadioButton) {
097            sortByNameRadioButton.setSelected(true);
098            sortByIdRadioButton.setSelected(false);
099            automationsModel.setSort(automationsModel.SORTBYNAME);
100        }
101        if (ae.getSource() == sortByIdRadioButton) {
102            sortByNameRadioButton.setSelected(false);
103            sortByIdRadioButton.setSelected(true);
104            automationsModel.setSort(automationsModel.SORTBYID);
105        }
106    }
107
108    // add button
109    @Override
110    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
111        log.debug("add automation button activated");
112        if (ae.getSource() == addButton) {
113            new AutomationTableFrame(null);
114        }
115    }
116    
117    @Override
118    public void dispose() {
119        InstanceManager.getOptionalDefault(JTablePersistenceManager.class).ifPresent(tpm -> {
120            tpm.stopPersisting(automationsTable);
121        });
122        automationsModel.dispose();
123        super.dispose();
124    }
125
126    private final static Logger log = LoggerFactory.getLogger(AutomationsTableFrame.class);
127}