OOP2

ClockModel.java

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