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        loadCheckBox.setToolTipText(Bundle.getMessage("TipIgnoresAlternate"));
068        blockCarsCheckBox.setToolTipText(Bundle.getMessage("TipBlockByPickUp"));
069
070        panelLoad.add(p1);
071        panelLoad.add(p2);
072        panelLoad.add(p3);
073
074        super.initComponents(location, track);
075
076        _toolMenu.insert(new TrackDestinationEditAction(this), TOOL_MENU_OFFSET);
077        // place at end of menu
078        _toolMenu.add(new SchedulesAndStagingAction());
079        addHelpMenu("package.jmri.jmrit.operations.Operations_Staging", true); // NOI18N
080
081        // override text strings for tracks
082        panelTrainDir.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TrainStaging")));
083        paneCheckBoxes
084                .setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TypesStaging")));
085        dropPanel
086                .setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectTrainArrival")));
087        pickupPanel.setBorder(BorderFactory.createTitledBorder(Bundle
088                .getMessage("SelectTrainDeparture")));
089        deleteTrackButton.setText(Bundle.getMessage("DeleteStaging"));
090        addTrackButton.setText(Bundle.getMessage("AddStaging"));
091        saveTrackButton.setText(Bundle.getMessage("SaveStaging"));
092
093        // setup the check boxes
094        if (_track != null) {
095            swapLoadsCheckBox.setSelected(_track.isLoadSwapEnabled());
096            emptyCheckBox.setSelected(_track.isLoadEmptyEnabled());
097            emptyCustomCheckBox.setSelected(_track.isRemoveCustomLoadsEnabled());
098            loadCheckBox.setSelected(_track.isAddCustomLoadsEnabled());
099            loadAnyCheckBox.setSelected(_track.isAddCustomLoadsAnySpurEnabled());
100            loadAnyStagingCheckBox.setSelected(_track.isAddCustomLoadsAnyStagingTrackEnabled());
101            blockCarsCheckBox.setSelected(_track.isBlockCarsEnabled());
102            if (loadCheckBox.isSelected() || loadAnyCheckBox.isSelected()
103                    || loadAnyStagingCheckBox.isSelected()) {
104                blockCarsCheckBox.setSelected(false);
105            }
106        }
107
108        addCheckBoxAction(swapLoadsCheckBox);
109        addCheckBoxAction(emptyCheckBox);
110        addCheckBoxAction(loadCheckBox);
111        addCheckBoxAction(loadAnyCheckBox);
112        addCheckBoxAction(loadAnyStagingCheckBox);
113
114        // finish
115        panelOrder.setVisible(false); // Car order out of staging isn't necessary
116        pack();
117        setVisible(true);
118    }
119
120    @Override
121    protected void saveTrack(Track track) {
122        track.setLoadSwapEnabled(swapLoadsCheckBox.isSelected());
123        track.setLoadEmptyEnabled(emptyCheckBox.isSelected());
124        track.setRemoveCustomLoadsEnabled(emptyCustomCheckBox.isSelected());
125        track.setAddCustomLoadsEnabled(loadCheckBox.isSelected());
126        track.setAddCustomLoadsAnySpurEnabled(loadAnyCheckBox.isSelected());
127        track.setAddCustomLoadsAnyStagingTrackEnabled(loadAnyStagingCheckBox.isSelected());
128        track.setBlockCarsEnabled(blockCarsCheckBox.isSelected());
129        super.saveTrack(track);
130    }
131
132    @Override
133    protected void enableButtons(boolean enabled) {
134        swapLoadsCheckBox.setEnabled(enabled);
135        emptyCheckBox.setEnabled(enabled);
136        emptyCustomCheckBox.setEnabled(enabled);
137        loadCheckBox.setEnabled(enabled);
138        loadAnyCheckBox.setEnabled(enabled);
139        loadAnyStagingCheckBox.setEnabled(enabled);
140        blockCarsCheckBox.setEnabled(_track != null && !_track.isAddCustomLoadsEnabled() && !_track.isAddCustomLoadsAnySpurEnabled()
141                && !_track.isAddCustomLoadsAnyStagingTrackEnabled() && enabled);
142        // show ship loads
143        pShipLoadOption.setVisible(_track == null || _track.isAddCustomLoadsEnabled()
144                || _track.isAddCustomLoadsAnySpurEnabled() || _track.isAddCustomLoadsAnyStagingTrackEnabled()
145                || !_track.getShipLoadOption().equals(Track.ALL_LOADS));
146        super.enableButtons(enabled);
147    }
148    
149    @Override
150    protected void updateTrainComboBox() {
151        super.updateTrainComboBox();
152        // only show trains that depart from this staging location
153        if (autoPickupCheckBox.isSelected()) {
154            for (int i = 1; i < comboBoxPickupTrains.getItemCount(); i++) {
155                Train train = comboBoxPickupTrains.getItemAt(i);
156                if (!train.getTrainDepartsName().equals(_location.getName())) {
157                    comboBoxPickupTrains.removeItemAt(i--);
158                }
159            }
160        }
161        // only show trains that terminate into this staging location
162        if (autoDropCheckBox.isSelected()) {
163            for (int i = 1; i < comboBoxDropTrains.getItemCount(); i++) {
164                Train train = comboBoxDropTrains.getItemAt(i);
165                if (!train.getTrainTerminatesName().equals(_location.getName())) {
166                    comboBoxDropTrains.removeItemAt(i--);
167                }
168            }
169        }
170    }
171    
172    @Override
173    protected void updateRouteComboBox() {
174        super.updateRouteComboBox();
175        // only show routes that depart from this staging location
176        if (autoPickupCheckBox.isSelected()) {
177            for (int i = 1; i < comboBoxPickupRoutes.getItemCount(); i++) {
178                Route route = comboBoxPickupRoutes.getItemAt(i);
179                if (route.getLocationsBySequenceList().get(0).getLocation() != _location) {
180                    comboBoxPickupRoutes.removeItemAt(i--);
181                }
182            }
183        }
184        // only show routes that terminate into this staging location
185        if (autoDropCheckBox.isSelected()) {
186            for (int i = 1; i < comboBoxDropRoutes.getItemCount(); i++) {
187                Route route = comboBoxDropRoutes.getItemAt(i);
188                if (route.getLocationsBySequenceList().get(route.getLocationsBySequenceList().size()-1).getLocation() != _location) {
189                    comboBoxDropRoutes.removeItemAt(i--);
190                }
191            }
192        }
193    }
194
195    @Override
196    public void checkBoxActionPerformed(java.awt.event.ActionEvent ae) {
197        if (ae.getSource() == swapLoadsCheckBox) {
198            if (swapLoadsCheckBox.isSelected()) {
199                emptyCheckBox.setSelected(false);
200            }
201        } else if (ae.getSource() == emptyCheckBox) {
202            if (emptyCheckBox.isSelected()) {
203                swapLoadsCheckBox.setSelected(false);
204            }
205        } else if (ae.getSource() == loadCheckBox) {
206            if (loadCheckBox.isSelected()) {
207                loadAnyCheckBox.setSelected(false);
208                blockCarsCheckBox.setSelected(false);
209                blockCarsCheckBox.setEnabled(false);
210            } else if (!loadAnyCheckBox.isSelected() && !loadAnyStagingCheckBox.isSelected()) {
211                blockCarsCheckBox.setEnabled(true);
212            }
213        } else if (ae.getSource() == loadAnyCheckBox) {
214            if (loadAnyCheckBox.isSelected()) {
215                loadCheckBox.setSelected(false);
216                blockCarsCheckBox.setSelected(false);
217                blockCarsCheckBox.setEnabled(false);
218            } else if (!loadCheckBox.isSelected() && !loadAnyStagingCheckBox.isSelected()) {
219                blockCarsCheckBox.setEnabled(true);
220            }
221        } else if (ae.getSource() == loadAnyStagingCheckBox) {
222            if (loadAnyStagingCheckBox.isSelected()) {
223                blockCarsCheckBox.setSelected(false);
224                blockCarsCheckBox.setEnabled(false);
225            } else if (!loadCheckBox.isSelected() && !loadAnyCheckBox.isSelected()) {
226                blockCarsCheckBox.setEnabled(true);
227            }
228        } else {
229            super.checkBoxActionPerformed(ae);
230        }
231    }
232
233//    private final static Logger log = LoggerFactory.getLogger(StagingEditFrame.class);
234}