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

Revision 2, 7.6 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.TreeMap;
9import java.util.Vector;
10
11import javax.swing.BorderFactory;
12import javax.swing.Box;
13import javax.swing.BoxLayout;
14import javax.swing.JButton;
15import javax.swing.JDialog;
16import javax.swing.JLabel;
17import javax.swing.JScrollPane;
18import javax.swing.JSpinner;
19import javax.swing.JTable;
20import javax.swing.border.EtchedBorder;
21import javax.swing.border.TitledBorder;
22
23import tmcsim.client.cadclientgui.data.Incident;
24import tmcsim.simulationmanager.model.AddIncidentListTableModel;
25import tmcsim.simulationmanager.model.IncidentTimeCellEditor;
26import tmcsim.simulationmanager.model.IncidentTimeCellRenderer;
27import tmcsim.simulationmanager.model.IncidentTimeSpinnerModel;
28import tmcsim.simulationmanager.model.AddIncidentListTableModel.ADD_INCIDENT_LIST_COLUMNS;
29
30
31/**
32 * This dialog is used when the user chooses to add a new incident into the
33 * simulation.  To show the dialog, mthe showDialog() method is called with
34 * the Incidents to display in the dialog.  These incidents are displayed to
35 * the user, who may then select which incidents to add to the simulation,
36 * and assign a time that each incident will occur. 
37 *
38 * @author Matthew Cechini (mcechini@calpoly.edu)
39 * @version $Date: 2006/06/14 00:12:38 $ $Revision: 1.7
40 */
41@SuppressWarnings("serial")
42public class AddIncidentDialog extends JDialog implements ActionListener {
43   
44    /**
45     * Constructor.  Initialize lists and GUI elements.
46     */ 
47    public AddIncidentDialog(Frame parent) {
48        super(parent);
49
50        setTitle("Add New Incidents");
51        setModal(true);
52       
53        setSize(new Dimension(600, 350));
54        setResizable(false);
55   
56        initComponents();   
57    }
58   
59    /**
60     * Populate the Incident Table with the parameter data.
61     *
62     * @param incidentList Vector of Incidents that have been read in from a
63     * selected file.
64     */
65    public void setModelData(Vector<Incident> incidentList) {
66        incidentListTableModel.clearModelData();
67       
68        for(Incident inc : incidentList) {
69            incidentListTableModel.addIncident(inc);
70        }
71
72    }
73   
74    /**
75     * Reposition the dialog to the center of the screen at set visible.
76     */
77    public void showDialog() {
78        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
79        setLocation(screenSize.width/2 - getWidth()/2, screenSize.height/2 - getHeight()/2);
80       
81        setVisible(true);
82    }
83   
84    /**
85     * Method gets a map of incidents, and the scheduled start time
86     * for all incidents that have been selected in the table.
87     *
88     * @return Map of incident log numbers (key) and scheduled start time (value)
89     */
90    public TreeMap<Integer, Long> getSelectedIncidentTimes() {
91        return incidentListTableModel.getSelectedIncidentTimes();
92    }
93   
94    public void actionPerformed(ActionEvent evt) {
95   
96        if(evt.getSource().equals(okButton)) {
97            incidentListTable.getColumnModel().getColumn(
98                    ADD_INCIDENT_LIST_COLUMNS.SCHEDULED_COL.colNum)
99                    .getCellEditor().stopCellEditing(); 
100           
101            setVisible(false);
102        }
103        else if(evt.getSource().equals(cancelButton)) {             
104            incidentListTable.getColumnModel().getColumn(
105                    ADD_INCIDENT_LIST_COLUMNS.SCHEDULED_COL.colNum)
106                    .getCellEditor().cancelCellEditing();
107           
108            incidentListTableModel.clearModelData();
109
110            setVisible(false);
111        }
112           
113    }
114   
115    /**
116     * Initialize GUI Swing elements and listeners.
117     */
118    private void initComponents() {
119
120        initIncidentTable();
121        initButtons();
122             
123        addIncidentBox = new Box(BoxLayout.Y_AXIS);
124        addIncidentBox.add(Box.createVerticalStrut(10));
125        addIncidentBox.add(incidentListPane); 
126        addIncidentBox.add(Box.createVerticalStrut(5));       
127        addIncidentBox.add(buttonBox);       
128        addIncidentBox.add(Box.createVerticalStrut(10));
129       
130        getContentPane().add(addIncidentBox);
131        pack();
132       
133    }   
134   
135    /**
136     * Initialize Incident Table Swing elements
137     */
138    private void initIncidentTable() {
139       
140        incidentTimeModel = new IncidentTimeSpinnerModel();
141        incidentSpinner   = new JSpinner(incidentTimeModel);
142       
143        incidentListTableModel = new AddIncidentListTableModel();       
144        incidentListTable = new JTable(incidentListTableModel);
145        incidentListTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
146        incidentListTable.setDragEnabled(false);     
147               
148        for(int c = 0; c < incidentListTable.getColumnCount(); c++) {
149            incidentListTable.getColumnModel().getColumn(c).setMinWidth(
150                    incidentListTableModel.getColumnMinWidth(c));
151            incidentListTable.getColumnModel().getColumn(c).setMaxWidth(
152                    incidentListTableModel.getColumnMaxWidth(c));
153            incidentListTable.getColumnModel().getColumn(c).setPreferredWidth(
154                    incidentListTableModel.getColumnPrefWidth(c));
155            incidentListTable.getColumnModel().getColumn(c).setResizable(true);
156           
157            if(c == ADD_INCIDENT_LIST_COLUMNS.EVENT_LEN_COL.colNum) {
158                incidentListTable.getColumnModel().getColumn(c).setCellRenderer(
159                        new IncidentTimeCellRenderer());               
160            }
161            else if(c == ADD_INCIDENT_LIST_COLUMNS.SCHEDULED_COL.colNum) {
162                incidentListTable.getColumnModel().getColumn(c).setCellRenderer(
163                        new IncidentTimeCellRenderer());
164                incidentListTable.getColumnModel().getColumn(c).setCellEditor(
165                        new IncidentTimeCellEditor(incidentSpinner));
166            }               
167        }
168       
169               
170        incidentListPane  = new JScrollPane();     
171        incidentListPane.setMaximumSize(new Dimension(650, 400));       
172        incidentListPane.setMinimumSize(new Dimension(525, 200));   
173        incidentListPane.setPreferredSize(new Dimension(525, 200));
174        incidentListPane.setViewportView(incidentListTable);
175        incidentListPane.setAlignmentX(Box.CENTER_ALIGNMENT);       
176
177        TitledBorder title = BorderFactory.createTitledBorder(
178                BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), 
179                "Possible Incidents");
180        title.setTitleJustification(TitledBorder.LEFT);
181        incidentListPane.setBorder(title);               
182       
183    }
184   
185    /**
186     * Intialize Ok and Cancel button Swing elements and listeners.
187     */
188    private void initButtons() {
189       
190        okButton = new JButton("OK");
191        okButton.addActionListener(this);
192       
193        cancelButton = new JButton("Cancel");       
194        cancelButton.addActionListener(this);
195       
196        buttonBox = new Box(BoxLayout.X_AXIS);
197        buttonBox.add(okButton);
198        buttonBox.add(Box.createHorizontalStrut(50));
199        buttonBox.add(cancelButton);
200        buttonBox.setAlignmentX(Box.CENTER_ALIGNMENT);     
201    }   
202
203
204    private JTable incidentListTable;
205    private AddIncidentListTableModel incidentListTableModel;
206    private JScrollPane incidentListPane;
207    private JSpinner incidentSpinner;
208    private IncidentTimeSpinnerModel incidentTimeModel; 
209   
210    private JButton cancelButton;;
211    private JButton okButton;
212   
213    private Box addIncidentBox;
214    private Box buttonBox; 
215}
Note: See TracBrowser for help on using the repository browser.