source: tmcsimulator/trunk/src/tmcsim/simulationmanager/dialogs/CMSDiversionDialog.java @ 2

Revision 2, 9.1 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files

Line 
1package tmcsim.simulationmanager.dialogs;
2
3import java.awt.Dimension;
4import java.awt.Frame;
5import java.awt.Toolkit;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.util.Vector;
9
10import javax.swing.BorderFactory;
11import javax.swing.Box;
12import javax.swing.BoxLayout;
13import javax.swing.JButton;
14import javax.swing.JDialog;
15import javax.swing.JLabel;
16import javax.swing.JSeparator;
17import javax.swing.JTextField;
18import javax.swing.border.CompoundBorder;
19
20import tmcsim.cadmodels.CMSDiversion;
21import tmcsim.cadmodels.CMSInfo;
22import tmcsim.simulationmanager.model.CMSDiversionSliderBox;
23
24
25/**
26 * The CMSDiversionDialog is used to allow the user to select diversion percentages
27 * for a specific changeable message sign.  This dialog is initialized with a CMSInfo
28 * object, which contains the unique identifying information and diversions for a CMS.
29 * A slider for each possible diversion is created and represented on the dialog. 
30 * The user may modify diversion percentages and then choose to apply them, or cancel
31 * and close the dialog.  Diversion percentages are updated in the RouteSliderBox,
32 * which points to the local CMSInfo object.
33 *
34 * @author Matthew Cechini (mcechini@calpoly.edu)
35 * @version $Date: 2006/06/06 20:46:41 $ $Revision: 1.7
36 */
37@SuppressWarnings("serial")
38public class CMSDiversionDialog extends JDialog {
39   
40    /** CMSInfo object associated with this dialog. */
41    public CMSInfo theCMSInfo = null;
42   
43    /** Vector of RouteSliderBox objects, each representing a possible diversion. */
44    private Vector<CMSDiversionSliderBox> diversionSliders;
45
46    /**
47     * Constructor.  Initialize GUI components.
48     *
49     * @param parent View object to attach.
50     * @param newCMSInfo CMSInfo object containing diversion info.
51     */ 
52    public CMSDiversionDialog(Frame parent) {
53        super(parent, "Add New Diversion", true);
54       
55        diversionSliders = new Vector<CMSDiversionSliderBox>();
56       
57    }
58   
59    public void showDialog(CMSInfo newCMSInfo) {
60        theCMSInfo = newCMSInfo;
61       
62        diversionSliders.clear();
63        getContentPane().removeAll();
64       
65        initComponents(theCMSInfo);     
66
67        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
68        setLocation(screenSize.width/2 - getWidth()/2, screenSize.height/2 - getHeight()/2);
69       
70        setVisible(true);
71    }
72   
73    /**
74     * Initialize GUI Swing elements and listeners.  Create RouteSliderBox
75     * objects for each possible diversion in the CMSInfo object.
76     *
77     * @param theDiversion
78     */
79    private void initComponents(CMSInfo theCMS) {
80
81        Box cmsSignInfoBox = new Box(BoxLayout.X_AXIS);
82        cmsSignInfoBox.setAlignmentX(Box.CENTER_ALIGNMENT);
83       
84        JLabel cmsIDLabel        = new JLabel("CMS ID:  ");
85        JLabel cmsPostmileLabel  = new JLabel("Postmile:  ");
86        JLabel cmsMainLineLabel  = new JLabel("Main Line:  ");     
87       
88        JTextField cmsIDTextField        = new JTextField(String.valueOf(theCMS.cmsID), 4);
89        JTextField cmsPostmileTextField  = new JTextField(String.valueOf(theCMS.postmile), 10);
90        JTextField cmsMainLineTextField  = new JTextField(theCMS.initialRoute, 10);     
91       
92        cmsIDTextField.setEditable(false);
93        cmsIDTextField.setMaximumSize(new Dimension(190, 25));
94        cmsIDTextField.setMinimumSize(new Dimension(120, 25));
95        cmsPostmileTextField.setEditable(false);
96        cmsPostmileTextField.setMaximumSize(new Dimension(60, 25));
97        cmsPostmileTextField.setMinimumSize(new Dimension(40, 25));;
98        cmsMainLineTextField.setEditable(false);               
99        cmsMainLineTextField.setMaximumSize(new Dimension(60, 25));
100        cmsMainLineTextField.setMinimumSize(new Dimension(40, 25));
101
102        cmsSignInfoBox.add(cmsIDLabel);
103        cmsSignInfoBox.add(cmsIDTextField);     
104        cmsSignInfoBox.add(Box.createHorizontalStrut(10));
105        cmsSignInfoBox.add(cmsPostmileLabel);
106        cmsSignInfoBox.add(cmsPostmileTextField);       
107        cmsSignInfoBox.add(Box.createHorizontalStrut(10));
108        cmsSignInfoBox.add(cmsMainLineLabel);
109        cmsSignInfoBox.add(cmsMainLineTextField);       
110       
111        Box diversionSlidersBox = new Box(BoxLayout.X_AXIS);
112        diversionSlidersBox.setAlignmentX(Box.CENTER_ALIGNMENT);
113       
114        Box labelBox = new Box(BoxLayout.Y_AXIS);
115        JLabel oldPathLabel = new JLabel("Old Path:");
116        oldPathLabel.setAlignmentX(Box.CENTER_ALIGNMENT);
117        JLabel spaceLabel = new JLabel(" ");
118        oldPathLabel.setAlignmentX(Box.CENTER_ALIGNMENT);
119        JLabel newPathLabel = new JLabel("New Path:");
120        newPathLabel.setAlignmentX(Box.CENTER_ALIGNMENT);
121       
122        labelBox.add(oldPathLabel);
123        labelBox.add(spaceLabel);
124        labelBox.add(newPathLabel);
125        labelBox.add(Box.createVerticalGlue());
126
127        diversionSlidersBox.add(labelBox);
128       
129        CMSDiversionSliderBox slider;
130        // For each diversion, create a new RouteSliderBox and
131        // needed labels and GUI elements.
132        for(CMSDiversion cmsd : theCMS.possibleDiversions) {
133           
134            Box singleDiversionBox = new Box(BoxLayout.Y_AXIS);
135            JLabel originalLabel = new JLabel(cmsd.originalPath);
136            JLabel toLabel       = new JLabel("To");
137            JLabel newLabel      = new JLabel(cmsd.newPath);
138           
139            originalLabel.setAlignmentX(Box.CENTER_ALIGNMENT);
140            toLabel.setAlignmentX(Box.CENTER_ALIGNMENT);
141            newLabel.setAlignmentX(Box.CENTER_ALIGNMENT);
142           
143            singleDiversionBox.add(originalLabel);
144            singleDiversionBox.add(toLabel);
145            singleDiversionBox.add(newLabel);
146   
147            slider = new CMSDiversionSliderBox(cmsd);
148            diversionSliders.add(slider);
149            singleDiversionBox.add(slider);
150            singleDiversionBox.add(Box.createHorizontalStrut(85));
151           
152            diversionSlidersBox.add(singleDiversionBox);
153           
154        }
155       
156        JLabel instructionsLabel = new JLabel("Choose the percentage of traffic " + 
157                                              "traveling on the main line that will be diverted.");
158        instructionsLabel.setAlignmentX(Box.CENTER_ALIGNMENT);
159       
160        Box diversionInteractionBox = new Box(BoxLayout.Y_AXIS);
161        diversionInteractionBox.setAlignmentX(Box.CENTER_ALIGNMENT);
162        diversionInteractionBox.add(cmsSignInfoBox);
163        diversionInteractionBox.add(Box.createVerticalStrut(5));
164        diversionInteractionBox.add(new JSeparator());
165        diversionInteractionBox.add(instructionsLabel);
166        diversionInteractionBox.add(Box.createVerticalStrut(10));
167        diversionInteractionBox.add(diversionSlidersBox);       
168
169        CompoundBorder cBorder = BorderFactory.createCompoundBorder(
170                BorderFactory.createTitledBorder(BorderFactory.createRaisedBevelBorder(), 
171                                                 "Diversion Information"),
172                BorderFactory.createEmptyBorder(5,5,5,5));
173        diversionInteractionBox.setBorder(cBorder);         
174       
175       
176        JButton applyButton = new JButton("Apply");
177        applyButton.addActionListener(new ActionListener() {
178            /*
179             * Set this dialog's visibility to false, and call the apply()
180             * method in all route slider boxes to set the new diversion
181             * percentages into the CMSDiversion objects. 
182             */
183            public void actionPerformed(ActionEvent evt) {
184                for(CMSDiversionSliderBox rs : diversionSliders)
185                    rs.apply();
186               
187                setVisible(false);             
188            }
189        }); 
190       
191        JButton cancelButton = new JButton("Cancel");
192        cancelButton.addActionListener(new ActionListener() {
193            /*
194             * Set this dialog's visibility to false, and call the reset()
195             * method in all route slider boxes to reset them to the
196             * original diversion percentage.
197             */         
198            public void actionPerformed(ActionEvent evt) {
199                theCMSInfo.reset();
200
201                setVisible(false);
202            }
203        }); 
204       
205        Box diversionButtonBox = new Box(BoxLayout.X_AXIS);
206        diversionButtonBox.setAlignmentX(Box.CENTER_ALIGNMENT);     
207       
208        diversionButtonBox.add(Box.createHorizontalGlue());
209        diversionButtonBox.add(applyButton);
210        diversionButtonBox.add(Box.createHorizontalStrut(20));
211        diversionButtonBox.add(cancelButton);
212        diversionButtonBox.add(Box.createHorizontalStrut(15));     
213       
214                   
215        Box diversionDialogBox = new Box(BoxLayout.Y_AXIS);
216        diversionDialogBox.add(Box.createVerticalStrut(10));       
217        diversionDialogBox.add(diversionInteractionBox);
218        diversionDialogBox.add(diversionButtonBox);
219   
220        getContentPane().add(diversionDialogBox);
221        pack();
222       
223        int width = 550 > diversionSliders.size()*100 ? 550 : diversionSliders.size()*100;
224        setSize(new Dimension(width, 500));
225        setResizable(false);       
226       
227    }   
228   
229}
230
231
232
233
234
Note: See TracBrowser for help on using the repository browser.