ClockModel.java
1 |
|
2 |
import java.util.Date; |
3 |
import java.util.GregorianCalendar; |
4 |
import java.util.Observable; |
5 |
import java.util.Timer; |
6 |
import java.util.TimerTask; |
7 |
import javax.swing.SwingUtilities; |
8 |
|
9 |
/** |
10 |
* |
11 |
* @author jvermeulen |
12 |
*/ |
13 |
public class ClockModel extends Observable { |
14 |
private int mHour; |
15 |
private int mMinute; |
16 |
private int mSecond; |
17 |
|
18 |
private final long mTickInterval = 1000; // tick every second |
19 |
private Timer mTimer; |
20 |
private boolean mIsRunning; |
21 |
|
22 |
public ClockModel() { |
23 |
// By default, set the clock time to the current system time. |
24 |
Calendar now = new GregorianCalendar(); |
25 |
setTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), |
26 |
now.get(Calendar.SECOND)); |
27 |
} |
28 |
|
29 |
public ClockModel(int hour, int minute, int second) { |
30 |
setTime(hour, minute, second); |
31 |
} |
32 |
/** |
33 |
* Helper class to run the tick() method multiple times |
34 |
* (e.g., every 1 second). |
35 |
*/ |
36 |
class TickTask extends TimerTask { |
37 |
public void run() { |
38 |
tick(); |
39 |
} |
40 |
} |
41 |
|
42 |
public void start() { |
43 |
if (!mIsRunning) { |
44 |
mIsRunning = true; |
45 |
|
46 |
// Schedule the tick() method to run every mTickInterval milliseconds. |
47 |
mTimer = new Timer(); |
48 |
mTimer.schedule(new TickTask(), 0, mTickInterval); |
49 |
|
50 |
ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, |
51 |
mIsRunning); |
52 |
setChanged(); |
53 |
notifyObservers(info); |
54 |
} |
55 |
} |
56 |
|
57 |
public void stop() { |
58 |
if (mIsRunning) { |
59 |
mIsRunning = false; |
60 |
|
61 |
// Stop the timer. |
62 |
mTimer.cancel(); |
63 |
mTimer = null; |
64 |
|
65 |
ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, |
66 |
mIsRunning); |
67 |
setChanged(); |
68 |
notifyObservers(info); |
69 |
} |
70 |
} |
71 |
|
72 |
/** |
73 |
* Sets the current time. Notifies observers of any change in time. |
74 |
* @param hour The new hour. |
75 |
* @param minute The new minute |
76 |
* @param second The new second |
77 |
*/ |
78 |
public void setTime(int hour, int minute, int second) { |
79 |
if (hour != mHour && isValidHour(hour)) { |
80 |
mHour = hour; |
81 |
setChanged(); |
82 |
} |
83 |
|
84 |
if (minute != mMinute && isValidMinute(minute)) { |
85 |
mMinute = minute; |
86 |
setChanged(); |
87 |
} |
88 |
|
89 |
if (second != mSecond && isValidSecond(second)) { |
90 |
mSecond = second; |
91 |
setChanged(); |
92 |
} |
93 |
|
94 |
// If the model has changed, notify Views. |
95 |
if (hasChanged()) { |
96 |
ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, |
97 |
mIsRunning); |
98 |
notifyObservers(info); |
99 |
} |
100 |
} |
101 |
|
102 |
/** |
103 |
* Checks to see if a number is a valid hour (i.e. in [0,23]). |
104 |
* @param hour The hour to check |
105 |
* @return True if this is a valid hour |
106 |
*/ |
107 |
private boolean isValidHour(int hour) { |
108 |
return (hour >= 0 && hour <= 23); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Checks to see if a number is a valid minute (i.e. in [0,59]). |
113 |
* @param minute The minute to check |
114 |
* @return True if this is a valid minute |
115 |
*/ |
116 |
private boolean isValidMinute(int minute) { |
117 |
return (minute >= 0 && minute <= 59); |
118 |
} |
119 |
|
120 |
/** |
121 |
* Checks to see if a number is a valid minute (i.e. in [0,59]). |
122 |
* @param second The second to check |
123 |
* @return True if this is a valid second |
124 |
*/ |
125 |
private boolean isValidSecond(int second) { |
126 |
return (second >= 0 && second <= 59); |
127 |
} |
128 |
|
129 |
private void tick() { |
130 |
// Get the current time |
131 |
int h = mHour; |
132 |
int m = mMinute; |
133 |
int s = mSecond; |
134 |
|
135 |
// Increment the current second, adjusting the minute and hour |
136 |
// if necessary. |
137 |
s += 1; |
138 |
if (s > 59) { |
139 |
s = 0; |
140 |
m += 1; |
141 |
if (m > 59) { |
142 |
m = 0; |
143 |
h += 1; |
144 |
if (h > 23) { |
145 |
h = 0; |
146 |
} |
147 |
} |
148 |
} |
149 |
|
150 |
// Set the new time. |
151 |
setTime(h, m, s); |
152 |
} |
153 |
} |
154 |