001package jmri.jmrit.operations.locations.gui;
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.LocationManager;
014import jmri.jmrit.operations.locations.schedules.SchedulesTableAction;
015import jmri.jmrit.operations.locations.tools.*;
016import jmri.jmrit.operations.routes.tools.ShowRoutesServingLocationAction;
017import jmri.jmrit.operations.setup.Control;
018import jmri.jmrit.operations.setup.Setup;
019import jmri.swing.JTablePersistenceManager;
020
021/**
022 * Frame for adding and editing the location roster for operations.
023 *
024 * @author Bob Jacobsen Copyright (C) 2001
025 * @author Daniel Boudreau Copyright (C) 2008
026 */
027public class LocationsTableFrame extends OperationsFrame {
028
029    LocationsTableModel locationsModel = new LocationsTableModel();
030    javax.swing.JTable locationsTable = new javax.swing.JTable(locationsModel);
031    JScrollPane locationsPane;
032
033    // labels
034    JLabel textSort = new JLabel(Bundle.getMessage("SortBy"));
035    JLabel textSep = new JLabel("          ");
036
037    // radio buttons
038    javax.swing.JRadioButton sortByName = new javax.swing.JRadioButton(Bundle.getMessage("Name"));
039    javax.swing.JRadioButton sortById = new javax.swing.JRadioButton(Bundle.getMessage("Id"));
040
041    // major buttons
042    JButton addButton = new JButton(Bundle.getMessage("AddLocation"));
043
044    public LocationsTableFrame() {
045        super(Bundle.getMessage("TitleLocationsTable"));
046        // general GUI config
047
048        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
049
050        // Set up the jtable in a Scroll Pane..
051        locationsPane = new JScrollPane(locationsTable);
052        locationsPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
053        locationsPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
054        locationsModel.initTable(this, locationsTable);
055        getContentPane().add(locationsPane);
056
057        // Set up the control panel
058        JPanel controlPanel = new JPanel();
059        controlPanel.setLayout(new FlowLayout());
060
061        controlPanel.add(textSort);
062        controlPanel.add(sortByName);
063        controlPanel.add(sortById);
064        controlPanel.add(textSep);
065        controlPanel.add(addButton);
066        controlPanel.setMaximumSize(new Dimension(Control.panelWidth1025, 50));
067
068        getContentPane().add(controlPanel);
069
070        sortByName.setSelected(true);
071
072        // setup buttons
073        addButtonAction(addButton);
074
075        ButtonGroup buttonGroup = new ButtonGroup();
076        buttonGroup.add(sortByName);
077        buttonGroup.add(sortById);
078        addRadioButtonAction(sortByName);
079        addRadioButtonAction(sortById);
080        
081        // tool tips
082        addButton.setToolTipText(Bundle.getMessage("AddLocationTip"));
083
084        // build menu
085        JMenuBar menuBar = new JMenuBar();
086        JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools"));
087        toolMenu.add(new LocationCopyAction(null));
088        toolMenu.add(new TrackCopyAction());
089        toolMenu.addSeparator();
090        toolMenu.add(new SchedulesTableAction());
091        toolMenu.addSeparator();
092        toolMenu.add(new ModifyLocationsAction());
093        toolMenu.add(new ModifyLocationsCarLoadsAction());
094        toolMenu.addSeparator();
095        toolMenu.add(new ExportLocationsRosterAction());
096        toolMenu.add(new ImportLocationsRosterAction() );
097        if (Setup.isVsdPhysicalLocationEnabled()) {
098            toolMenu.add(new SetPhysicalLocationAction(null));
099        }
100        toolMenu.addSeparator();
101        toolMenu.add(new ShowCarsByLocationAction(false, null, null));
102        toolMenu.add(new ShowLocosByLocationAction(false, null, null));
103        toolMenu.addSeparator();
104        toolMenu.add(new ShowTrainsServingLocationAction(null, null));
105        toolMenu.add(new ShowRoutesServingLocationAction(null));
106        toolMenu.addSeparator();
107        toolMenu.add(new PrintLocationsAction(false));
108        toolMenu.add(new PrintLocationsAction(true));
109        menuBar.add(toolMenu);
110        menuBar.add(new jmri.jmrit.operations.OperationsMenu());
111        setJMenuBar(menuBar);
112        addHelpMenu("package.jmri.jmrit.operations.Operations_Locations", true); // NOI18N
113
114        initMinimumSize();
115        // make panel a bit wider than minimum if the very first time opened
116        if (getWidth() == Control.panelWidth500) {
117            setSize(Control.panelWidth700, getHeight());
118        }
119
120        // create ShutDownTasks
121        createShutDownTask();
122    }
123
124    @Override
125    public void radioButtonActionPerformed(java.awt.event.ActionEvent ae) {
126        log.debug("radio button activated");
127        // clear any sorts by column
128        clearTableSort(locationsTable);
129        if (ae.getSource() == sortByName) {
130            locationsModel.setSort(locationsModel.SORTBYNAME);
131        }
132        if (ae.getSource() == sortById) {
133            InstanceManager.getDefault(LocationManager.class).setShowIdEnabled(true);
134            locationsModel.setSort(locationsModel.SORTBYID);
135        }
136    }
137
138    // add button
139    @Override
140    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
141        if (ae.getSource() == addButton) {
142            LocationEditFrame f = new LocationEditFrame(null);
143            f.setTitle(Bundle.getMessage("TitleLocationAdd"));
144        }
145    }
146    
147    @Override
148    public void dispose() {
149        InstanceManager.getOptionalDefault(JTablePersistenceManager.class).ifPresent(tpm -> {
150            tpm.stopPersisting(locationsTable);
151        });
152        locationsModel.dispose();
153        super.dispose();
154    }
155
156    private final static Logger log = LoggerFactory.getLogger(LocationsTableFrame.class);
157}