| 1 | package tmcsim.simulationmanager.dialogs; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Dimension; |
|---|
| 4 | import java.awt.Toolkit; |
|---|
| 5 | import java.awt.event.ActionEvent; |
|---|
| 6 | import java.awt.event.ActionListener; |
|---|
| 7 | import java.awt.event.WindowEvent; |
|---|
| 8 | import java.awt.event.WindowListener; |
|---|
| 9 | |
|---|
| 10 | import javax.swing.Box; |
|---|
| 11 | import javax.swing.BoxLayout; |
|---|
| 12 | import javax.swing.JButton; |
|---|
| 13 | import javax.swing.JDialog; |
|---|
| 14 | import javax.swing.JLabel; |
|---|
| 15 | import javax.swing.JSpinner; |
|---|
| 16 | |
|---|
| 17 | import tmcsim.simulationmanager.SimulationManagerView; |
|---|
| 18 | import tmcsim.simulationmanager.model.IncidentTimeSpinnerModel; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * GotoTimeIndexDialog is a dialog used to prompt the user for the |
|---|
| 23 | * simulation time that the simulation will be reset to. |
|---|
| 24 | * When the dialog is closed, the gotoApplied flag is set to true |
|---|
| 25 | * if the user has chosen a time to seek to. Otherwise, the flag is false. |
|---|
| 26 | * |
|---|
| 27 | * @author Matthew Cechini |
|---|
| 28 | * @version |
|---|
| 29 | */ |
|---|
| 30 | @SuppressWarnings("serial") |
|---|
| 31 | public class GotoTimeIndexDialog extends JDialog { |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Boolean flag to designate whether the user has chosen to goto |
|---|
| 35 | * a new position in the simulation. |
|---|
| 36 | */ |
|---|
| 37 | public boolean gotoApplied = false; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Constructor. |
|---|
| 41 | * |
|---|
| 42 | * @param simulationManagerView View object to attach this dialog to. |
|---|
| 43 | * @param initialValue Long value (in seconds) of time to initialize spinner with. |
|---|
| 44 | */ |
|---|
| 45 | public GotoTimeIndexDialog(SimulationManagerView simulationManagerView, long initialValue) { |
|---|
| 46 | super(simulationManagerView, "Goto CAD Time Index", true); |
|---|
| 47 | |
|---|
| 48 | initComponents(); |
|---|
| 49 | |
|---|
| 50 | timeSpinner.setValue(initialValue); |
|---|
| 51 | |
|---|
| 52 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
|---|
| 53 | setLocation(screenSize.width/2 - getWidth()/2, screenSize.height/2 - getHeight()/2); |
|---|
| 54 | |
|---|
| 55 | setSize(new Dimension(250, 150)); |
|---|
| 56 | setResizable(false); |
|---|
| 57 | setModal(true); |
|---|
| 58 | setVisible(true); |
|---|
| 59 | |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Initialize GUI Swing elements and listeners. |
|---|
| 64 | */ |
|---|
| 65 | private void initComponents() { |
|---|
| 66 | |
|---|
| 67 | addWindowListener(new WindowListener() { |
|---|
| 68 | public void windowClosed(WindowEvent e) {} |
|---|
| 69 | public void windowOpened(WindowEvent e) {} |
|---|
| 70 | public void windowIconified(WindowEvent e) {} |
|---|
| 71 | public void windowDeiconified(WindowEvent e) {} |
|---|
| 72 | public void windowActivated(WindowEvent e) {} |
|---|
| 73 | public void windowDeactivated(WindowEvent e) {} |
|---|
| 74 | public void windowClosing(WindowEvent e) { |
|---|
| 75 | gotoApplied = false; |
|---|
| 76 | } |
|---|
| 77 | }); |
|---|
| 78 | |
|---|
| 79 | gotoLabel = new JLabel("Choose the new simulation time:"); |
|---|
| 80 | gotoLabel.setAlignmentX(Box.CENTER_ALIGNMENT); |
|---|
| 81 | |
|---|
| 82 | okButton = new JButton("OK"); |
|---|
| 83 | okButton.addActionListener(new ActionListener() { |
|---|
| 84 | public void actionPerformed(ActionEvent e) { |
|---|
| 85 | gotoApplied = true; |
|---|
| 86 | setVisible(false); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | }); |
|---|
| 90 | |
|---|
| 91 | cancelButton = new JButton("Cancel"); |
|---|
| 92 | cancelButton.addActionListener(new ActionListener() { |
|---|
| 93 | public void actionPerformed(ActionEvent e) { |
|---|
| 94 | gotoApplied = false; |
|---|
| 95 | setVisible(false); |
|---|
| 96 | } |
|---|
| 97 | }); |
|---|
| 98 | |
|---|
| 99 | incidentTimeModel = new IncidentTimeSpinnerModel(); |
|---|
| 100 | timeSpinner = new JSpinner(incidentTimeModel); |
|---|
| 101 | timeSpinner.setMaximumSize(new Dimension(70,20)); |
|---|
| 102 | timeSpinner.setAlignmentX(Box.CENTER_ALIGNMENT); |
|---|
| 103 | |
|---|
| 104 | buttonBox = new Box(BoxLayout.X_AXIS); |
|---|
| 105 | buttonBox.add(Box.createHorizontalStrut(20)); |
|---|
| 106 | buttonBox.add(Box.createHorizontalGlue()); |
|---|
| 107 | buttonBox.add(okButton); |
|---|
| 108 | buttonBox.add(Box.createHorizontalStrut(20)); |
|---|
| 109 | buttonBox.add(cancelButton); |
|---|
| 110 | buttonBox.add(Box.createHorizontalGlue()); |
|---|
| 111 | buttonBox.add(Box.createHorizontalStrut(20)); |
|---|
| 112 | buttonBox.setAlignmentX(Box.CENTER_ALIGNMENT); |
|---|
| 113 | |
|---|
| 114 | gotoIndexBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 115 | gotoIndexBox.add(Box.createVerticalStrut(10)); |
|---|
| 116 | gotoIndexBox.add(Box.createVerticalGlue()); |
|---|
| 117 | gotoIndexBox.add(gotoLabel); |
|---|
| 118 | gotoIndexBox.add(Box.createVerticalStrut(10)); |
|---|
| 119 | gotoIndexBox.add(timeSpinner); |
|---|
| 120 | gotoIndexBox.add(Box.createVerticalStrut(10)); |
|---|
| 121 | gotoIndexBox.add(buttonBox); |
|---|
| 122 | gotoIndexBox.add(Box.createVerticalGlue()); |
|---|
| 123 | gotoIndexBox.add(Box.createVerticalStrut(10)); |
|---|
| 124 | |
|---|
| 125 | getContentPane().add(gotoIndexBox); |
|---|
| 126 | pack(); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * Gets a string representation of the time set by the time |
|---|
| 131 | * spinner in the dialog. Format: H:MM:SS |
|---|
| 132 | * @return String representation of spinner time. |
|---|
| 133 | */ |
|---|
| 134 | public String getGotoTime() { |
|---|
| 135 | return (String)timeSpinner.getValue(); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | private JLabel gotoLabel; |
|---|
| 140 | private JSpinner timeSpinner; |
|---|
| 141 | |
|---|
| 142 | private IncidentTimeSpinnerModel incidentTimeModel; |
|---|
| 143 | |
|---|
| 144 | private JButton cancelButton;; |
|---|
| 145 | private JButton okButton; |
|---|
| 146 | |
|---|
| 147 | private Box gotoIndexBox; |
|---|
| 148 | private Box buttonBox; |
|---|
| 149 | |
|---|
| 150 | } |
|---|