001package jmri.jmrit.operations.routes;
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.routes.tools.*;
014import jmri.jmrit.operations.setup.Control;
015import jmri.swing.JTablePersistenceManager;
016
017/**
018 * Frame for adding and editing the route roster for operations.
019 *
020 * @author Bob Jacobsen Copyright (C) 2001
021 * @author Daniel Boudreau Copyright (C) 2008, 2009
022 */
023public class RoutesTableFrame extends OperationsFrame {
024
025    RoutesTableModel routesModel = new RoutesTableModel();
026    JTable routesTable;
027
028    // labels
029    JLabel textSort = new JLabel(Bundle.getMessage("SortBy"));
030    JLabel textSep = new JLabel("          ");
031
032    // radio buttons
033    JRadioButton sortByName = new JRadioButton(Bundle.getMessage("Name"));
034    JRadioButton sortById = new JRadioButton(Bundle.getMessage("Id"));
035
036    // major buttons
037    JButton addButton = new JButton(Bundle.getMessage("AddRoute"));
038
039    public RoutesTableFrame() {
040        super(Bundle.getMessage("TitleRoutesTable"));
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        routesTable = new JTable(routesModel);
047        JScrollPane routesPane = new JScrollPane(routesTable);
048        routesPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
049        routesModel.initTable(this, routesTable);
050        getContentPane().add(routesPane);
051
052        // Set up the control panel
053        JPanel controlPanel = new JPanel();
054        controlPanel.setLayout(new FlowLayout());
055
056        controlPanel.add(textSort);
057        controlPanel.add(sortByName);
058        controlPanel.add(sortById);
059        controlPanel.add(textSep);
060        controlPanel.add(addButton);
061        controlPanel.setMaximumSize(new Dimension(Control.panelWidth1025, 50));
062
063        getContentPane().add(controlPanel);
064        
065        sortByName.setSelected(true);
066
067        // setup buttons
068        addButtonAction(addButton);
069        addButton.setToolTipText(Bundle.getMessage("AddRouteTip"));
070
071        addRadioButtonAction(sortByName);
072        addRadioButtonAction(sortById);
073        
074        ButtonGroup bGroup = new ButtonGroup();
075        bGroup.add(sortByName);
076        bGroup.add(sortById);
077
078        // build menu
079        JMenuBar menuBar = new JMenuBar();
080        JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools"));
081        toolMenu.add(new RouteCopyAction());
082        toolMenu.add(new SetTrainIconPositionAction());
083        toolMenu.add(new ExportRoutesAction());
084        toolMenu.addSeparator();
085        toolMenu.add(new ShowRoutesServingLocationAction(null));
086        toolMenu.addSeparator();
087        toolMenu.add(new PrintRoutesAction(false));
088        toolMenu.add(new PrintRoutesAction(true));
089
090        menuBar.add(toolMenu);
091        menuBar.add(new jmri.jmrit.operations.OperationsMenu());
092        setJMenuBar(menuBar);
093
094        // add help menu to window
095        addHelpMenu("package.jmri.jmrit.operations.Operations_Routes", true); // NOI18N
096
097        initMinimumSize(new Dimension(Control.panelWidth700, Control.panelHeight300));
098 
099        // create ShutDownTasks
100        createShutDownTask();
101    }
102
103    @Override
104    public void radioButtonActionPerformed(java.awt.event.ActionEvent ae) {
105        log.debug("radio button activated");
106        // clear any sorts by column
107        clearTableSort(routesTable);
108        if (ae.getSource() == sortByName) {
109            routesModel.setSort(routesModel.SORTBYNAME);
110        }
111        if (ae.getSource() == sortById) {
112            routesModel.setSort(routesModel.SORTBYID);
113        }
114    }
115
116    // add button
117    @Override
118    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
119        // log.debug("route button activated");
120        if (ae.getSource() == addButton) {
121            RouteEditFrame f = new RouteEditFrame();
122            f.initComponents(null);
123        }
124    }
125
126    @Override
127    public void dispose() {
128        InstanceManager.getOptionalDefault(JTablePersistenceManager.class).ifPresent(tpm -> {
129            tpm.stopPersisting(routesTable);
130        });
131        routesModel.dispose();
132        super.dispose();
133    }
134
135    private final static Logger log = LoggerFactory.getLogger(RoutesTableFrame.class);
136}