| 1 | package paramsim.paramicssimulator.gui; |
|---|
| 2 | |
|---|
| 3 | import java.awt.event.ActionEvent; |
|---|
| 4 | import java.awt.event.ActionListener; |
|---|
| 5 | import java.util.List; |
|---|
| 6 | |
|---|
| 7 | import javax.swing.BorderFactory; |
|---|
| 8 | import javax.swing.Box; |
|---|
| 9 | import javax.swing.BoxLayout; |
|---|
| 10 | import javax.swing.ButtonGroup; |
|---|
| 11 | import javax.swing.JPanel; |
|---|
| 12 | import javax.swing.JRadioButton; |
|---|
| 13 | |
|---|
| 14 | import paramsim.paramicssimulator.ParamicsSimulator; |
|---|
| 15 | import paramsim.paramicssimulator.SimulationCamera; |
|---|
| 16 | import paramsim.paramicssimulator.ParamicsSimulator.NETWORK_STATUS; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * A JPanel which provides an interface to control the Paramics Simulator. |
|---|
| 20 | * Contains a Paramics Status interface using radio buttons, and multiple |
|---|
| 21 | * Camera Status interfaces using radio buttons. |
|---|
| 22 | * |
|---|
| 23 | * @author Greg Eddington |
|---|
| 24 | */ |
|---|
| 25 | @SuppressWarnings("serial") |
|---|
| 26 | public class ParamicsControlPanel extends JPanel implements ActionListener |
|---|
| 27 | { |
|---|
| 28 | /** The simulator **/ |
|---|
| 29 | private ParamicsSimulator simulator; |
|---|
| 30 | |
|---|
| 31 | /** Loading Status **/ |
|---|
| 32 | private final static String LOADING = "Loading"; |
|---|
| 33 | |
|---|
| 34 | /** Warming Status **/ |
|---|
| 35 | private final static String WARMING = "Warming"; |
|---|
| 36 | |
|---|
| 37 | /** Loaded Status **/ |
|---|
| 38 | private final static String LOADED = "Loaded"; |
|---|
| 39 | |
|---|
| 40 | /** Free Flow Speed Label **/ |
|---|
| 41 | private final static String FREE_FLOW = "Free Flow"; |
|---|
| 42 | |
|---|
| 43 | /** Slow Speed Label **/ |
|---|
| 44 | private final static String SLOW = "Slow"; |
|---|
| 45 | |
|---|
| 46 | /** Stopped Speed Label **/ |
|---|
| 47 | private final static String STOPPED = "Stopped"; |
|---|
| 48 | |
|---|
| 49 | /** Fast Camera Status Character ID **/ |
|---|
| 50 | private final static Character FAST_CHARACTER = 'F'; |
|---|
| 51 | |
|---|
| 52 | /** Medium Camera Status Character ID **/ |
|---|
| 53 | private final static Character MEDIUM_CHARACTER = 'M'; |
|---|
| 54 | |
|---|
| 55 | /** Slow Camera Status Character ID **/ |
|---|
| 56 | private final static Character SLOW_CHARACTER = 'S'; |
|---|
| 57 | |
|---|
| 58 | /** Constructor **/ |
|---|
| 59 | public ParamicsControlPanel(ParamicsSimulator simulator) |
|---|
| 60 | { |
|---|
| 61 | this.simulator = simulator; |
|---|
| 62 | |
|---|
| 63 | Box controlsBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 64 | controlsBox.setAlignmentY(Box.CENTER_ALIGNMENT); |
|---|
| 65 | |
|---|
| 66 | controlsBox.add(Box.createVerticalStrut(20)); |
|---|
| 67 | controlsBox.add(CreateStatusBox()); |
|---|
| 68 | controlsBox.add(Box.createVerticalStrut(20)); |
|---|
| 69 | controlsBox.add(CreateCameraBox()); |
|---|
| 70 | |
|---|
| 71 | add(controlsBox); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** Create a Paramics Status GUI box **/ |
|---|
| 75 | private Box CreateStatusBox() |
|---|
| 76 | { |
|---|
| 77 | Box radioBox = new Box(BoxLayout.X_AXIS); |
|---|
| 78 | |
|---|
| 79 | ButtonGroup radioButtons = new ButtonGroup(); |
|---|
| 80 | |
|---|
| 81 | JRadioButton loadingButton = new JRadioButton(LOADING); |
|---|
| 82 | loadingButton.setActionCommand(LOADING); |
|---|
| 83 | JRadioButton warmingButton = new JRadioButton(WARMING); |
|---|
| 84 | warmingButton.setActionCommand(WARMING); |
|---|
| 85 | JRadioButton loadedButton = new JRadioButton(LOADED); |
|---|
| 86 | loadedButton.setActionCommand(LOADED); |
|---|
| 87 | |
|---|
| 88 | loadingButton.addActionListener(this); |
|---|
| 89 | warmingButton.addActionListener(this); |
|---|
| 90 | loadedButton.addActionListener(this); |
|---|
| 91 | |
|---|
| 92 | loadingButton.setSelected(true); |
|---|
| 93 | |
|---|
| 94 | radioButtons.add(loadingButton); |
|---|
| 95 | radioButtons.add(warmingButton); |
|---|
| 96 | radioButtons.add(loadedButton); |
|---|
| 97 | |
|---|
| 98 | radioBox.add(loadingButton); |
|---|
| 99 | radioBox.add(Box.createHorizontalStrut(25)); |
|---|
| 100 | radioBox.add(warmingButton); |
|---|
| 101 | radioBox.add(Box.createHorizontalStrut(25)); |
|---|
| 102 | radioBox.add(loadedButton); |
|---|
| 103 | |
|---|
| 104 | Box statusBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 105 | statusBox.setBorder( BorderFactory.createCompoundBorder( |
|---|
| 106 | BorderFactory.createTitledBorder("Paramics Status"), |
|---|
| 107 | BorderFactory.createEmptyBorder(0, 40, 10, 40) ) ); |
|---|
| 108 | statusBox.add(radioBox); |
|---|
| 109 | |
|---|
| 110 | return statusBox; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** Creates a Camera Status GUI box **/ |
|---|
| 114 | private Box CreateCameraBox() |
|---|
| 115 | { |
|---|
| 116 | Box camerasBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 117 | Box cameraLineBox = new Box(BoxLayout.X_AXIS); |
|---|
| 118 | |
|---|
| 119 | for (int i = 1; i <= simulator.getCameras().size(); i++) |
|---|
| 120 | { |
|---|
| 121 | Box cameraBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 122 | cameraBox.setBorder( BorderFactory.createCompoundBorder( |
|---|
| 123 | BorderFactory.createTitledBorder("Camera " + simulator.getCameras().get(i-1).id), |
|---|
| 124 | BorderFactory.createEmptyBorder(0, 5, 10, 5) ) ); |
|---|
| 125 | ButtonGroup radioButtons = new ButtonGroup(); |
|---|
| 126 | |
|---|
| 127 | JRadioButton fastButton = new JRadioButton(FREE_FLOW); |
|---|
| 128 | JRadioButton mediumButton = new JRadioButton(SLOW); |
|---|
| 129 | JRadioButton slowButton = new JRadioButton(STOPPED); |
|---|
| 130 | slowButton.setSelected(true); |
|---|
| 131 | |
|---|
| 132 | fastButton.setActionCommand(FAST_CHARACTER + Integer.toString(i-1)); |
|---|
| 133 | mediumButton.setActionCommand(MEDIUM_CHARACTER + Integer.toString(i-1)); |
|---|
| 134 | slowButton.setActionCommand(SLOW_CHARACTER + Integer.toString(i-1)); |
|---|
| 135 | |
|---|
| 136 | fastButton.addActionListener(this); |
|---|
| 137 | mediumButton.addActionListener(this); |
|---|
| 138 | slowButton.addActionListener(this); |
|---|
| 139 | |
|---|
| 140 | radioButtons.add(fastButton); |
|---|
| 141 | radioButtons.add(mediumButton); |
|---|
| 142 | radioButtons.add(slowButton); |
|---|
| 143 | cameraBox.add(fastButton); |
|---|
| 144 | cameraBox.add(mediumButton); |
|---|
| 145 | cameraBox.add(slowButton); |
|---|
| 146 | |
|---|
| 147 | cameraLineBox.add(cameraBox); |
|---|
| 148 | cameraLineBox.add(Box.createHorizontalStrut(20)); |
|---|
| 149 | |
|---|
| 150 | if (i % 3 == 0 && i != simulator.getCameras().size()) |
|---|
| 151 | { |
|---|
| 152 | camerasBox.add(cameraLineBox); |
|---|
| 153 | camerasBox.add(Box.createVerticalStrut(15)); |
|---|
| 154 | cameraLineBox = new Box(BoxLayout.X_AXIS); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | camerasBox.add(cameraLineBox); |
|---|
| 158 | camerasBox.add(Box.createVerticalStrut(15)); |
|---|
| 159 | cameraLineBox = new Box(BoxLayout.X_AXIS); |
|---|
| 160 | |
|---|
| 161 | return camerasBox; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | /** |
|---|
| 165 | * Listens for actions performed in the GUI. |
|---|
| 166 | * |
|---|
| 167 | * A Paramics Status action will be either LOADING, WARMING, or LOADED. If |
|---|
| 168 | * recieved, the writeParamicsStatus function will be called from the simulator |
|---|
| 169 | * with the appropriate message. |
|---|
| 170 | * |
|---|
| 171 | * Any other action is considered a Camera Status Action. The first character |
|---|
| 172 | * of the action command is either FAST_CHARACTER, MEDIUM_CHARACTER, or SLOW_CHARACTER, |
|---|
| 173 | * with the remainder of the command being the ID# of the camera. The writeCameraStatus |
|---|
| 174 | * function of the simulator will be called with the appropriate information. |
|---|
| 175 | */ |
|---|
| 176 | public void actionPerformed(ActionEvent e) |
|---|
| 177 | { |
|---|
| 178 | String command = e.getActionCommand(); |
|---|
| 179 | if (command == LOADING) |
|---|
| 180 | { |
|---|
| 181 | simulator.writeParamicsStatus(NETWORK_STATUS.LOADING); |
|---|
| 182 | } |
|---|
| 183 | else if (command == WARMING) |
|---|
| 184 | { |
|---|
| 185 | simulator.writeParamicsStatus(NETWORK_STATUS.WARMING); |
|---|
| 186 | } |
|---|
| 187 | else if (command == LOADED) |
|---|
| 188 | { |
|---|
| 189 | simulator.writeParamicsStatus(NETWORK_STATUS.LOADED); |
|---|
| 190 | } |
|---|
| 191 | else // Camera Speed Status |
|---|
| 192 | { |
|---|
| 193 | List<SimulationCamera> cameras = simulator.getCameras(); |
|---|
| 194 | SimulationCamera camera = cameras.get(Integer.parseInt(command.substring(1))); |
|---|
| 195 | |
|---|
| 196 | if (e.getActionCommand().charAt(0) == FAST_CHARACTER) |
|---|
| 197 | { |
|---|
| 198 | camera.averageSpeedNE = camera.averageSpeedSW = |
|---|
| 199 | ((camera.minFreeFlowSpeed + camera.maxFreeFlowSpeed) / 2); |
|---|
| 200 | } |
|---|
| 201 | else if (e.getActionCommand().charAt(0) == MEDIUM_CHARACTER) |
|---|
| 202 | { |
|---|
| 203 | camera.averageSpeedNE = camera.averageSpeedSW = |
|---|
| 204 | ((camera.minSlowSpeed + camera.maxSlowSpeed) / 2); |
|---|
| 205 | } |
|---|
| 206 | else if (e.getActionCommand().charAt(0) == SLOW_CHARACTER) |
|---|
| 207 | { |
|---|
| 208 | camera.averageSpeedNE = camera.averageSpeedSW = |
|---|
| 209 | ((camera.minStoppedSpeed + camera.maxStoppedSpeed) / 2); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | simulator.writeCameraStatus(cameras); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|