| 1 | package tmcsim.simulationmanager.model; |
|---|
| 2 | |
|---|
| 3 | import javax.swing.AbstractSpinnerModel; |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * IncidentTimeSpinnerModel is an AbstractSpinnerModel used in rescheduling |
|---|
| 7 | * and adding incidents into the simulation. The spinner shows a simulation time |
|---|
| 8 | * of format H:MM:SS. The next and previous values are calculated by adding or |
|---|
| 9 | * subtracting 30 seconds from the current value. |
|---|
| 10 | * |
|---|
| 11 | * @author Matthew Cechini |
|---|
| 12 | * @version |
|---|
| 13 | */ |
|---|
| 14 | public class IncidentTimeSpinnerModel extends AbstractSpinnerModel { |
|---|
| 15 | |
|---|
| 16 | /** Current simulation time value (seconds). */ |
|---|
| 17 | private long currentTime = 0; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Constructor. |
|---|
| 21 | */ |
|---|
| 22 | public IncidentTimeSpinnerModel() { } |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Method returns the next time value by adding 30 seconds |
|---|
| 26 | * to the current time value. The returned value is a string |
|---|
| 27 | * of format H:MM:SS |
|---|
| 28 | * |
|---|
| 29 | * @return H:MM:SS formatted time |
|---|
| 30 | */ |
|---|
| 31 | public Object getNextValue() { |
|---|
| 32 | currentTime += 30 - (currentTime % 30); |
|---|
| 33 | |
|---|
| 34 | return longToTime(); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Method returns the previous time value by subtracting 30 seconds |
|---|
| 39 | * to the current time value. If the current value is less than 0:00:30, |
|---|
| 40 | * the time is set to 0. The returned value is a string of format H:MM:SS. |
|---|
| 41 | * |
|---|
| 42 | * @return H:MM:SS formatted time |
|---|
| 43 | */ |
|---|
| 44 | public Object getPreviousValue() { |
|---|
| 45 | if(currentTime > 30) |
|---|
| 46 | currentTime -= 30 + (currentTime % 30); |
|---|
| 47 | else |
|---|
| 48 | currentTime = 0; |
|---|
| 49 | |
|---|
| 50 | return longToTime(); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Method returns the current time value as a string of format H:MM:SS. |
|---|
| 55 | * |
|---|
| 56 | * @return H:MM:SS formatted time |
|---|
| 57 | */ |
|---|
| 58 | public Object getValue() { |
|---|
| 59 | return longToTime(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Sets the current time. If the parameter is a Long, set |
|---|
| 64 | * the current time to this value and call this class again |
|---|
| 65 | * with the String representation to update the spinner. |
|---|
| 66 | */ |
|---|
| 67 | public void setValue(Object o) { |
|---|
| 68 | if(o.getClass() == String.class) |
|---|
| 69 | fireStateChanged(); |
|---|
| 70 | else if (o.getClass() == Long.class) { |
|---|
| 71 | currentTime = ((Long)o).longValue(); |
|---|
| 72 | setValue(longToTime()); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Converts the currentTime value into string of format H:MM:SS. |
|---|
| 78 | * @return H:MM:SS format of the current simulation time. |
|---|
| 79 | */ |
|---|
| 80 | private String longToTime() { |
|---|
| 81 | |
|---|
| 82 | String time = new String(); |
|---|
| 83 | long tempTime = currentTime; |
|---|
| 84 | |
|---|
| 85 | time += String.valueOf(tempTime / 3600) + ":"; |
|---|
| 86 | |
|---|
| 87 | tempTime = tempTime % 3600; |
|---|
| 88 | |
|---|
| 89 | if(tempTime / 60 < 10) |
|---|
| 90 | time += "0"; |
|---|
| 91 | |
|---|
| 92 | time += String.valueOf(tempTime / 60) + ":"; |
|---|
| 93 | tempTime = tempTime % 60; |
|---|
| 94 | |
|---|
| 95 | if(tempTime < 10) |
|---|
| 96 | time += "0"; |
|---|
| 97 | |
|---|
| 98 | time += String.valueOf(tempTime); |
|---|
| 99 | return time; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | } |
|---|