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.add(new ImportRoutesAction());
085        toolMenu.addSeparator();
086        toolMenu.add(new ShowRoutesServingLocationAction(null));
087        toolMenu.addSeparator();
088        toolMenu.add(new PrintRoutesAction(false));
089        toolMenu.add(new PrintRoutesAction(true));
090
091        menuBar.add(toolMenu);
092        menuBar.add(new jmri.jmrit.operations.OperationsMenu());
093        setJMenuBar(menuBar);
094
095        // add help menu to window
096        addHelpMenu("package.jmri.jmrit.operations.Operations_Routes", true); // NOI18N
097
098        initMinimumSize(new Dimension(Control.panelWidth700, Control.panelHeight300));
099 
100        // create ShutDownTasks
101        createShutDownTask();
102    }
103
104    @Override
105    public void radioButtonActionPerformed(java.awt.event.ActionEvent ae) {
106        log.debug("radio button activated");
107        // clear any sorts by column
108        clearTableSort(routesTable);
109        if (ae.getSource() == sortByName) {
110            routesModel.setSort(routesModel.SORTBYNAME);
111        }
112        if (ae.getSource() == sortById) {
113            routesModel.setSort(routesModel.SORTBYID);
114        }
115    }
116
117    // add button
118    @Override
119    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
120        // log.debug("route button activated");
121        if (ae.getSource() == addButton) {
122            RouteEditFrame f = new RouteEditFrame();
123            f.initComponents(null);
124        }
125    }
126
127    @Override
128    public void dispose() {
129        InstanceManager.getOptionalDefault(JTablePersistenceManager.class).ifPresent(tpm -> {
130            tpm.stopPersisting(routesTable);
131        });
132        routesModel.dispose();
133        super.dispose();
134    }
135
136    private final static Logger log = LoggerFactory.getLogger(RoutesTableFrame.class);
137}