001package jmri.jmrit.operations.locations;
002
003import java.awt.GridBagLayout;
004
005import javax.swing.*;
006
007import jmri.jmrit.operations.locations.schedules.tools.SchedulesAndStagingAction;
008import jmri.jmrit.operations.locations.tools.TrackDestinationEditAction;
009import jmri.jmrit.operations.routes.Route;
010import jmri.jmrit.operations.trains.Train;
011
012/**
013 * Frame for user edit of a staging track
014 *
015 * @author Dan Boudreau Copyright (C) 2008, 2011
016 */
017public class StagingEditFrame extends TrackEditFrame {
018
019    // check boxes
020    JCheckBox swapLoadsCheckBox = new JCheckBox(Bundle.getMessage("SwapCarLoads"));
021    JCheckBox emptyCheckBox = new JCheckBox(Bundle.getMessage("EmptyDefaultCarLoads"));
022    JCheckBox emptyCustomCheckBox = new JCheckBox(Bundle.getMessage("EmptyCarLoads"));
023    JCheckBox loadCheckBox = new JCheckBox(Bundle.getMessage("LoadCarLoads"));
024    JCheckBox loadAnyCheckBox = new JCheckBox(Bundle.getMessage("LoadAnyCarLoads"));
025    JCheckBox loadAnyStagingCheckBox = new JCheckBox(Bundle.getMessage("LoadsStaging"));
026    JCheckBox blockCarsCheckBox = new JCheckBox(Bundle.getMessage("BlockCars"));
027
028    JPanel panelLoad = panelOpt4;
029
030    public StagingEditFrame() {
031        super(Bundle.getMessage("AddStaging"));
032    }
033    
034    @Override
035    public void initComponents(Track track) {
036        setTitle(Bundle.getMessage("EditStaging", track.getLocation().getName()));
037        initComponents(track.getLocation(), track);
038    }
039
040    @Override
041    public void initComponents(Location location, Track track) {
042        _type = Track.STAGING;
043
044        // setup the optional panel with staging stuff
045        panelLoad.setLayout(new BoxLayout(panelLoad, BoxLayout.X_AXIS));
046
047        JPanel p1 = new JPanel();
048        p1.setLayout(new GridBagLayout());
049        p1.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("OptionalLoads")));
050        addItemLeft(p1, swapLoadsCheckBox, 0, 0);
051        addItemLeft(p1, emptyCheckBox, 0, 1);
052
053        JPanel p2 = new JPanel();
054        p2.setLayout(new GridBagLayout());
055        p2.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("OptionalCustomLoads")));
056        addItemLeft(p2, emptyCustomCheckBox, 0, 0);
057        addItemLeft(p2, loadCheckBox, 0, 1);
058        addItemLeft(p2, loadAnyCheckBox, 0, 2);
059        addItemLeft(p2, loadAnyStagingCheckBox, 0, 3);
060
061        JPanel p3 = new JPanel();
062        p3.setLayout(new GridBagLayout());
063        p3.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("OptionalBlocking")));
064        addItemLeft(p3, blockCarsCheckBox, 0, 0);
065
066        // load tool tips
067        blockCarsCheckBox.setToolTipText(Bundle.getMessage("TipBlockByPickUp"));
068
069        panelLoad.add(p1);
070        panelLoad.add(p2);
071        panelLoad.add(p3);
072
073        super.initComponents(location, track);
074
075        _toolMenu.insert(new TrackDestinationEditAction(this), TOOL_MENU_OFFSET);
076        // place at end of menu
077        _toolMenu.add(new SchedulesAndStagingAction());
078        addHelpMenu("package.jmri.jmrit.operations.Operations_Staging", true); // NOI18N
079
080        // override text strings for tracks
081        panelTrainDir.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TrainStaging")));
082        paneCheckBoxes
083                .setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TypesStaging")));
084        dropPanel
085                .setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectTrainArrival")));
086        pickupPanel.setBorder(BorderFactory.createTitledBorder(Bundle
087                .getMessage("SelectTrainDeparture")));
088        deleteTrackButton.setText(Bundle.getMessage("DeleteStaging"));
089        addTrackButton.setText(Bundle.getMessage("AddStaging"));
090        saveTrackButton.setText(Bundle.getMessage("SaveStaging"));
091
092        // setup the check boxes
093        if (_track != null) {
094            swapLoadsCheckBox.setSelected(_track.isLoadSwapEnabled());
095            emptyCheckBox.setSelected(_track.isLoadEmptyEnabled());
096            emptyCustomCheckBox.setSelected(_track.isRemoveCustomLoadsEnabled());
097            loadCheckBox.setSelected(_track.isAddCustomLoadsEnabled());
098            loadAnyCheckBox.setSelected(_track.isAddCustomLoadsAnySpurEnabled());
099            loadAnyStagingCheckBox.setSelected(_track.isAddCustomLoadsAnyStagingTrackEnabled());
100            blockCarsCheckBox.setSelected(_track.isBlockCarsEnabled());
101            if (loadCheckBox.isSelected() || loadAnyCheckBox.isSelected()
102                    || loadAnyStagingCheckBox.isSelected()) {
103                blockCarsCheckBox.setSelected(false);
104            }
105        }
106
107        addCheckBoxAction(swapLoadsCheckBox);
108        addCheckBoxAction(emptyCheckBox);
109        addCheckBoxAction(loadCheckBox);
110        addCheckBoxAction(loadAnyCheckBox);
111        addCheckBoxAction(loadAnyStagingCheckBox);
112
113        // finish
114        panelOrder.setVisible(false); // Car order out of staging isn't necessary
115        pack();
116        setVisible(true);
117    }
118
119    @Override
120    protected void saveTrack(Track track) {
121        track.setLoadSwapEnabled(swapLoadsCheckBox.isSelected());
122        track.setLoadEmptyEnabled(emptyCheckBox.isSelected());
123        track.setRemoveCustomLoadsEnabled(emptyCustomCheckBox.isSelected());
124        track.setAddCustomLoadsEnabled(loadCheckBox.isSelected());
125        track.setAddCustomLoadsAnySpurEnabled(loadAnyCheckBox.isSelected());
126        track.setAddCustomLoadsAnyStagingTrackEnabled(loadAnyStagingCheckBox.isSelected());
127        track.setBlockCarsEnabled(blockCarsCheckBox.isSelected());
128        super.saveTrack(track);
129    }
130
131    @Override
132    protected void enableButtons(boolean enabled) {
133        swapLoadsCheckBox.setEnabled(enabled);
134        emptyCheckBox.setEnabled(enabled);
135        emptyCustomCheckBox.setEnabled(enabled);
136        loadCheckBox.setEnabled(enabled);
137        loadAnyCheckBox.setEnabled(enabled);
138        loadAnyStagingCheckBox.setEnabled(enabled);
139        blockCarsCheckBox.setEnabled(_track != null && !_track.isAddCustomLoadsEnabled() && !_track.isAddCustomLoadsAnySpurEnabled()
140                && !_track.isAddCustomLoadsAnyStagingTrackEnabled() && enabled);
141        // show ship loads
142        pShipLoadOption.setVisible(_track == null || _track.isAddCustomLoadsEnabled()
143                || _track.isAddCustomLoadsAnySpurEnabled() || _track.isAddCustomLoadsAnyStagingTrackEnabled()
144                || !_track.getShipLoadOption().equals(Track.ALL_LOADS));
145        super.enableButtons(enabled);
146    }
147    
148    @Override
149    protected void updateTrainComboBox() {
150        super.updateTrainComboBox();
151        // only show trains that depart from this staging location
152        if (autoPickupCheckBox.isSelected()) {
153            for (int i = 1; i < comboBoxPickupTrains.getItemCount(); i++) {
154                Train train = comboBoxPickupTrains.getItemAt(i);
155                if (!train.getTrainDepartsName().equals(_location.getName())) {
156                    comboBoxPickupTrains.removeItemAt(i--);
157                }
158            }
159        }
160        // only show trains that terminate into this staging location
161        if (autoDropCheckBox.isSelected()) {
162            for (int i = 1; i < comboBoxDropTrains.getItemCount(); i++) {
163                Train train = comboBoxDropTrains.getItemAt(i);
164                if (!train.getTrainTerminatesName().equals(_location.getName())) {
165                    comboBoxDropTrains.removeItemAt(i--);
166                }
167            }
168        }
169    }
170    
171    @Override
172    protected void updateRouteComboBox() {
173        super.updateRouteComboBox();
174        // only show routes that depart from this staging location
175        if (autoPickupCheckBox.isSelected()) {
176            for (int i = 1; i < comboBoxPickupRoutes.getItemCount(); i++) {
177                Route route = comboBoxPickupRoutes.getItemAt(i);
178                if (route.getLocationsBySequenceList().get(0).getLocation() != _location) {
179                    comboBoxPickupRoutes.removeItemAt(i--);
180                }
181            }
182        }
183        // only show routes that terminate into this staging location
184        if (autoDropCheckBox.isSelected()) {
185            for (int i = 1; i < comboBoxDropRoutes.getItemCount(); i++) {
186                Route route = comboBoxDropRoutes.getItemAt(i);
187                if (route.getLocationsBySequenceList().get(route.getLocationsBySequenceList().size()-1).getLocation() != _location) {
188                    comboBoxDropRoutes.removeItemAt(i--);
189                }
190            }
191        }
192    }
193
194    @Override
195    public void checkBoxActionPerformed(java.awt.event.ActionEvent ae) {
196        if (ae.getSource() == swapLoadsCheckBox) {
197            if (swapLoadsCheckBox.isSelected()) {
198                emptyCheckBox.setSelected(false);
199            }
200        } else if (ae.getSource() == emptyCheckBox) {
201            if (emptyCheckBox.isSelected()) {
202                swapLoadsCheckBox.setSelected(false);
203            }
204        } else if (ae.getSource() == loadCheckBox) {
205            if (loadCheckBox.isSelected()) {
206                loadAnyCheckBox.setSelected(false);
207                blockCarsCheckBox.setSelected(false);
208                blockCarsCheckBox.setEnabled(false);
209            } else if (!loadAnyCheckBox.isSelected() && !loadAnyStagingCheckBox.isSelected()) {
210                blockCarsCheckBox.setEnabled(true);
211            }
212        } else if (ae.getSource() == loadAnyCheckBox) {
213            if (loadAnyCheckBox.isSelected()) {
214                loadCheckBox.setSelected(false);
215                blockCarsCheckBox.setSelected(false);
216                blockCarsCheckBox.setEnabled(false);
217            } else if (!loadCheckBox.isSelected() && !loadAnyStagingCheckBox.isSelected()) {
218                blockCarsCheckBox.setEnabled(true);
219            }
220        } else if (ae.getSource() == loadAnyStagingCheckBox) {
221            if (loadAnyStagingCheckBox.isSelected()) {
222                blockCarsCheckBox.setSelected(false);
223                blockCarsCheckBox.setEnabled(false);
224            } else if (!loadCheckBox.isSelected() && !loadAnyCheckBox.isSelected()) {
225                blockCarsCheckBox.setEnabled(true);
226            }
227        } else {
228            super.checkBoxActionPerformed(ae);
229        }
230    }
231
232//    private final static Logger log = LoggerFactory.getLogger(StagingEditFrame.class);
233}