| 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.UIManager; |
|---|
| 16 | |
|---|
| 17 | import tmcsim.simulationmanager.SimulationManagerView; |
|---|
| 18 | import timeselector.*; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * GotoTimeIndexDialog is a dialog used to prompt the user for the |
|---|
| 22 | * simulation time that the simulation will be reset to. |
|---|
| 23 | * When the dialog is closed, the gotoApplied flag is set to true |
|---|
| 24 | * if the user has chosen a time to seek to. Otherwise, the flag is false. |
|---|
| 25 | * |
|---|
| 26 | * @author Matthew Cechini |
|---|
| 27 | * Modifed to use timeselector by jdalbey 3/24/2019 |
|---|
| 28 | */ |
|---|
| 29 | @SuppressWarnings("serial") |
|---|
| 30 | public class GotoTimeIndexDialog extends JDialog { |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Boolean flag to designate whether the user has chosen to goto |
|---|
| 34 | * a new position in the simulation. |
|---|
| 35 | */ |
|---|
| 36 | public boolean gotoApplied = false; |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Constructor. |
|---|
| 40 | * |
|---|
| 41 | * @param simulationManagerView View object to attach this dialog to. |
|---|
| 42 | * @param initialValue Long value (in seconds) of time to initialize spinner with. |
|---|
| 43 | */ |
|---|
| 44 | public GotoTimeIndexDialog(SimulationManagerView simulationManagerView, long initialValue) { |
|---|
| 45 | super(simulationManagerView, "Goto CAD Time Index", true); |
|---|
| 46 | |
|---|
| 47 | initComponents(); |
|---|
| 48 | |
|---|
| 49 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
|---|
| 50 | setLocation(screenSize.width/2 - getWidth()/2, screenSize.height/2 - getHeight()/2); |
|---|
| 51 | |
|---|
| 52 | setSize(new Dimension(250, 150)); |
|---|
| 53 | setResizable(false); |
|---|
| 54 | setModal(true); |
|---|
| 55 | setVisible(true); |
|---|
| 56 | |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Initialize GUI Swing elements and listeners. |
|---|
| 61 | */ |
|---|
| 62 | private void initComponents() { |
|---|
| 63 | |
|---|
| 64 | addWindowListener(new WindowListener() { |
|---|
| 65 | public void windowClosed(WindowEvent e) {} |
|---|
| 66 | public void windowOpened(WindowEvent e) {} |
|---|
| 67 | public void windowIconified(WindowEvent e) {} |
|---|
| 68 | public void windowDeiconified(WindowEvent e) {} |
|---|
| 69 | public void windowActivated(WindowEvent e) {} |
|---|
| 70 | public void windowDeactivated(WindowEvent e) {} |
|---|
| 71 | public void windowClosing(WindowEvent e) { |
|---|
| 72 | gotoApplied = false; |
|---|
| 73 | } |
|---|
| 74 | }); |
|---|
| 75 | |
|---|
| 76 | gotoLabel = new JLabel("Choose the new simulation time:"); |
|---|
| 77 | gotoLabel.setAlignmentX(Box.CENTER_ALIGNMENT); |
|---|
| 78 | |
|---|
| 79 | okButton = new JButton("OK"); |
|---|
| 80 | okButton.addActionListener(new ActionListener() { |
|---|
| 81 | public void actionPerformed(ActionEvent e) { |
|---|
| 82 | gotoApplied = true; |
|---|
| 83 | setVisible(false); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | }); |
|---|
| 87 | |
|---|
| 88 | cancelButton = new JButton("Cancel"); |
|---|
| 89 | cancelButton.addActionListener(new ActionListener() { |
|---|
| 90 | public void actionPerformed(ActionEvent e) { |
|---|
| 91 | gotoApplied = false; |
|---|
| 92 | setVisible(false); |
|---|
| 93 | } |
|---|
| 94 | }); |
|---|
| 95 | |
|---|
| 96 | fldTimeSelection = new TimeSelectionField(false); |
|---|
| 97 | |
|---|
| 98 | buttonBox = new Box(BoxLayout.X_AXIS); |
|---|
| 99 | buttonBox.add(Box.createHorizontalStrut(20)); |
|---|
| 100 | buttonBox.add(Box.createHorizontalGlue()); |
|---|
| 101 | buttonBox.add(okButton); |
|---|
| 102 | buttonBox.add(Box.createHorizontalStrut(20)); |
|---|
| 103 | buttonBox.add(cancelButton); |
|---|
| 104 | buttonBox.add(Box.createHorizontalGlue()); |
|---|
| 105 | buttonBox.add(Box.createHorizontalStrut(20)); |
|---|
| 106 | buttonBox.setAlignmentX(Box.CENTER_ALIGNMENT); |
|---|
| 107 | |
|---|
| 108 | gotoIndexBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 109 | gotoIndexBox.add(Box.createVerticalStrut(10)); |
|---|
| 110 | gotoIndexBox.add(Box.createVerticalGlue()); |
|---|
| 111 | gotoIndexBox.add(gotoLabel); |
|---|
| 112 | gotoIndexBox.add(Box.createVerticalStrut(10)); |
|---|
| 113 | gotoIndexBox.add(fldTimeSelection); |
|---|
| 114 | gotoIndexBox.add(Box.createVerticalStrut(10)); |
|---|
| 115 | gotoIndexBox.add(buttonBox); |
|---|
| 116 | gotoIndexBox.add(Box.createVerticalGlue()); |
|---|
| 117 | gotoIndexBox.add(Box.createVerticalStrut(10)); |
|---|
| 118 | |
|---|
| 119 | getContentPane().add(gotoIndexBox); |
|---|
| 120 | pack(); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Accessor to the String representation of user selected time. The text |
|---|
| 125 | * field contains a String representation of the time in a format produced |
|---|
| 126 | * by SimpleDateFormat pattern "h:mm:ss", e.g., "3:26:00" |
|---|
| 127 | * |
|---|
| 128 | * @return String containing the selected time. |
|---|
| 129 | */ |
|---|
| 130 | public String getGotoTime() |
|---|
| 131 | { |
|---|
| 132 | String result = fldTimeSelection.getText(); |
|---|
| 133 | return result.substring(1, result.length()); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** Local main for unit testing */ |
|---|
| 137 | public static void main(String[] args) |
|---|
| 138 | { |
|---|
| 139 | try |
|---|
| 140 | { |
|---|
| 141 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|---|
| 142 | } catch (Exception ex) |
|---|
| 143 | { |
|---|
| 144 | ex.printStackTrace(); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | GotoTimeIndexDialog dlg = new GotoTimeIndexDialog(null,0); |
|---|
| 148 | System.out.println(dlg.getGotoTime()); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | private JLabel gotoLabel; |
|---|
| 152 | |
|---|
| 153 | private JButton cancelButton;; |
|---|
| 154 | private JButton okButton; |
|---|
| 155 | |
|---|
| 156 | private Box gotoIndexBox; |
|---|
| 157 | private Box buttonBox; |
|---|
| 158 | |
|---|
| 159 | private TimeSelectionField fldTimeSelection; |
|---|
| 160 | } |
|---|