001package jmri.jmrit.operations.locations.tools;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
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.OperationsXml;
014import jmri.jmrit.operations.locations.Location;
015import jmri.jmrit.operations.locations.LocationManager;
016import jmri.jmrit.operations.setup.Control;
017import jmri.jmrit.operations.setup.Setup;
018import jmri.jmrit.operations.trains.TrainManager;
019
020/**
021 * Frame for user edit of location
022 *
023 * @author Dan Boudreau Copyright (C) 2015
024 * 
025 */
026public class LocationTrackBlockingOrderFrame extends OperationsFrame {
027
028    LocationTrackBlockingOrderTableModel trackModel = new LocationTrackBlockingOrderTableModel();
029    JTable trackTable = new JTable(trackModel);
030    JScrollPane trackPane = new JScrollPane(trackTable);
031
032    LocationManager locationManager = InstanceManager.getDefault(LocationManager.class);
033
034    Location _location = null;
035    
036    JLabel locationName = new JLabel();
037
038    // major buttons
039    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
040    JButton resetButton = new JButton(Bundle.getMessage("Reset"));
041    JButton reorderButton = new JButton(Bundle.getMessage("Reorder"));
042
043    public LocationTrackBlockingOrderFrame() {
044        super(Bundle.getMessage("TitleTrackBlockingOrder"));
045    }
046
047    public void initComponents(Location location) {
048        _location = location;
049        
050        trackPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
051        trackPane.setBorder(BorderFactory.createTitledBorder(""));
052
053        if (_location != null) {
054            trackModel.initTable(trackTable, location);
055            locationName.setText(_location.getName());
056            trackTable.setEnabled(!Setup.isSortByTrackNameEnabled());
057            enableButtons(!Setup.isSortByTrackNameEnabled());
058        } else {
059            enableButtons(false);
060        }
061
062        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
063
064        // Layout the panel by rows       
065        JPanel pName = new JPanel();
066        pName.setLayout(new GridBagLayout());
067        pName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Location")));
068
069        addItem(pName, locationName, 0, 0);
070        
071        // row buttons
072        JPanel pB = new JPanel();
073        pB.setLayout(new GridBagLayout());
074        addItem(pB, resetButton, 0, 0);
075        addItem(pB, reorderButton, 1, 0);
076        addItem(pB, saveButton, 2, 0);
077        
078        // Notes
079        JLabel note1 = new JLabel(Bundle.getMessage("ServiceOrderMessage"));
080        JLabel note2 = new JLabel(Bundle.getMessage("ServiceOrderEastSouth"));
081
082        getContentPane().add(pName);
083        getContentPane().add(note1);
084        getContentPane().add(note2);
085        getContentPane().add(trackPane);
086        getContentPane().add(pB);
087
088        // setup buttons
089        addButtonAction(resetButton);
090        addButtonAction(reorderButton);
091        addButtonAction(saveButton);
092
093        // add tool tips
094        resetButton.setToolTipText(Bundle.getMessage("TipResetButton"));
095        reorderButton.setToolTipText(Bundle.getMessage("TipReorderButton"));
096
097        // build menu
098//        JMenuBar menuBar = new JMenuBar();
099//        JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools"));
100//        menuBar.add(toolMenu);
101//        setJMenuBar(menuBar);
102        addHelpMenu("package.jmri.jmrit.operations.Operations_TrackBlockingOrder", true); // NOI18N
103
104        initMinimumSize(new Dimension(Control.panelWidth600, Control.panelHeight500));
105
106    }
107
108    // Reset and Save
109    @Override
110    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
111        if (ae.getSource() == resetButton && _location != null) {
112            _location.resetTracksByBlockingOrder();
113        }
114        if (ae.getSource() == reorderButton && _location != null) {
115            _location.resequnceTracksByBlockingOrder();
116        }
117        if (ae.getSource() == saveButton) {
118            if (trackTable.isEditing()) {
119                log.debug("track table edit true");
120                trackTable.getCellEditor().stopCellEditing();
121            }
122            // recreate all train manifests
123            InstanceManager.getDefault(TrainManager.class).setTrainsModified();
124            // save location file
125            OperationsXml.save();
126            if (Setup.isCloseWindowOnSaveEnabled()) {
127                dispose();
128            }
129        }
130    }
131
132    private void enableButtons(boolean enabled) {
133        resetButton.setEnabled(enabled);
134        reorderButton.setEnabled(enabled);
135        saveButton.setEnabled(enabled);
136    }
137
138    @Override
139    public void dispose() {
140        trackModel.dispose();
141        super.dispose();
142    }
143
144    private final static Logger log = LoggerFactory.getLogger(LocationTrackBlockingOrderFrame.class);
145}