001package jmri.jmrit.operations.locations.schedules;
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.locations.schedules.tools.*;
014import jmri.jmrit.operations.locations.tools.PrintLocationsAction;
015import jmri.jmrit.operations.setup.Control;
016import jmri.swing.JTablePersistenceManager;
017
018/**
019 * Frame for adding and editing the Schedule roster for operations.
020 *
021 * @author Bob Jacobsen Copyright (C) 2001
022 * @author Daniel Boudreau Copyright (C) 2009, 2012
023 */
024public class SchedulesTableFrame extends OperationsFrame {
025
026    SchedulesTableModel schedulesModel = new SchedulesTableModel();
027    javax.swing.JTable schedulesTable = new javax.swing.JTable(schedulesModel);
028    JScrollPane schedulesPane;
029
030    // labels
031    javax.swing.JLabel textSort = new javax.swing.JLabel();
032    javax.swing.JLabel textSep = new javax.swing.JLabel();
033
034    // radio buttons
035    javax.swing.JRadioButton sortByName = new javax.swing.JRadioButton(Bundle.getMessage("Name"));
036    javax.swing.JRadioButton sortById = new javax.swing.JRadioButton(Bundle.getMessage("Id"));
037
038    // major buttons
039    // javax.swing.JButton addButton = new javax.swing.JButton();
040    public SchedulesTableFrame() {
041        super(Bundle.getMessage("TitleSchedulesTable"));
042        // general GUI config
043
044        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
045
046        // Set up the jtable in a Scroll Pane..
047        schedulesPane = new JScrollPane(schedulesTable);
048        schedulesPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
049        schedulesPane
050                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
051        schedulesModel.initTable(this, schedulesTable);
052        getContentPane().add(schedulesPane);
053
054        // Set up the control panel
055        JPanel controlPanel = new JPanel();
056        controlPanel.setLayout(new FlowLayout());
057
058        textSort.setText(Bundle.getMessage("SortBy"));
059        controlPanel.add(textSort);
060        controlPanel.add(sortByName);
061        sortByName.setSelected(true);
062        controlPanel.add(sortById);
063        textSep.setText("          ");
064        controlPanel.add(textSep);
065
066        // TODO allow user to add schedule to a spur
067        // addButton.setText(Bundle.getMessage("Add"));
068        // addButton.setVisible(true);
069        // controlPanel.add (addButton);
070        controlPanel.setMaximumSize(new Dimension(Control.panelWidth1025, 50));
071        getContentPane().add(controlPanel);
072
073        // set up buttons
074        // addButtonAction(addButton);
075        addRadioButtonAction(sortByName);
076        addRadioButtonAction(sortById);
077
078        // build menu
079        JMenuBar menuBar = new JMenuBar();
080        JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools"));
081        toolMenu.add(new ScheduleCopyAction());
082        toolMenu.add(new SchedulesResetHitsAction());
083        toolMenu.add(new ExportSchedulesAction());
084        toolMenu.addSeparator();
085        toolMenu.add(new SchedulesByLoadAction());
086        toolMenu.add(new SchedulesAndStagingAction());
087        toolMenu.addSeparator();
088        toolMenu.add(new PrintLocationsAction(false));
089        toolMenu.add(new PrintLocationsAction(true));       
090        menuBar.add(toolMenu);
091        setJMenuBar(menuBar);
092        addHelpMenu("package.jmri.jmrit.operations.Operations_LocationSchedules", true); // NOI18N
093
094        initMinimumSize();
095        // make panel a bit wider than minimum if the very first time opened
096        if (getWidth() == Control.panelWidth500) {
097            setSize(Control.panelWidth1025, getHeight());
098        }
099    }
100
101    @Override
102    public void radioButtonActionPerformed(java.awt.event.ActionEvent ae) {
103        log.debug("radio button activated");
104        // clear any sorts by column
105        clearTableSort(schedulesTable);
106        if (ae.getSource() == sortByName) {
107            sortByName.setSelected(true);
108            sortById.setSelected(false);
109            schedulesModel.setSort(schedulesModel.SORTBYNAME);
110        }
111        if (ae.getSource() == sortById) {
112            sortByName.setSelected(false);
113            sortById.setSelected(true);
114            schedulesModel.setSort(schedulesModel.SORTBYID);
115        }
116    }
117
118    @Override
119    public void dispose() {
120        schedulesModel.dispose();
121        InstanceManager.getOptionalDefault(JTablePersistenceManager.class).ifPresent(tpm -> {
122            tpm.stopPersisting(schedulesTable);
123        });
124        super.dispose();
125    }
126
127    private final static Logger log = LoggerFactory.getLogger(SchedulesTableFrame.class);
128}