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