KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > model > StartedUserTask


1 package org.netbeans.modules.tasklist.usertasks.model;
2
3 import java.util.TimerTask JavaDoc;
4 import javax.swing.SwingUtilities JavaDoc;
5 import javax.swing.event.ChangeEvent JavaDoc;
6 import javax.swing.event.ChangeListener JavaDoc;
7 import javax.swing.event.EventListenerList JavaDoc;
8 import org.netbeans.modules.tasklist.core.util.ActivityListener;
9 import org.netbeans.modules.tasklist.usertasks.options.Settings;
10 import org.openide.awt.StatusDisplayer;
11 import org.openide.util.NbBundle;
12
13 /**
14  * Started user task.
15  * This code will not work if the value of spent time will be changed for
16  * a task that is currently running. The task should be stopped and
17  * then started again.
18  *
19  * @author tl
20  */

21 public class StartedUserTask {
22     private static final java.util.Timer JavaDoc TIMER = new java.util.Timer JavaDoc(true);
23
24     private static final int INACTIVITY_DURATION = 10 * 60 * 1000; // 10 minutes
25

26     private static final int STATE_WORKING = 0;
27     private static final int STATE_SUSPENDED = 1;
28     private static final int STATE_NOTASK = 2;
29             
30     private static final StartedUserTask INSTANCE =
31         new StartedUserTask();
32     
33     /**
34      * Returns the only instance of this class.
35      *
36      * @return the instance.
37      */

38     public static StartedUserTask getInstance() {
39         return INSTANCE;
40     }
41     
42     /**
43      * Currently working on this task
44      */

45     private UserTask started = null;
46     
47     /**
48      * Time as returned by System.currentMillis() when the task was started
49      */

50     private long startedAt;
51     
52     private int initialSpentTime;
53     private int state = STATE_NOTASK;
54     private UserTask.WorkPeriod workPeriod;
55     private EventListenerList JavaDoc list = new EventListenerList JavaDoc();
56
57     static {
58         TIMER.scheduleAtFixedRate(new TimerTask JavaDoc() {
59             public void run() {
60                 if (SwingUtilities.isEventDispatchThread())
61                     getInstance().timer();
62                 else
63                     SwingUtilities.invokeLater(this);
64             }
65         }, 0, 1000 * 15);
66         ActivityListener.init();
67     }
68     
69     /**
70      * Creates a new instance of StartingTask
71      */

72     private StartedUserTask() {
73     }
74
75     /**
76      * Fires a change event.
77      */

78     private void fireChange() {
79         ChangeEvent JavaDoc event = null;
80         Object JavaDoc[] listeners = list.getListenerList();
81         for (int i = listeners.length - 2; i >= 0; i -= 2) {
82             if (listeners[i] == ChangeListener JavaDoc.class) {
83                 if (event == null)
84                     event = new ChangeEvent JavaDoc(this);
85                 ((ChangeListener JavaDoc) listeners[i + 1]).stateChanged(event);
86             }
87         }
88     }
89     
90     /**
91      * Adds a listener that will be notified when the started task was
92      * changed.
93      *
94      * @param l a listener
95      */

96     public void addChangeListener(ChangeListener JavaDoc l) {
97         list.add(ChangeListener JavaDoc.class, l);
98     }
99     
100     /**
101      * Removes a listener registered with addChangeListener.
102      *
103      * @param l a listener
104      */

105     public void removeChangeListener(ChangeListener JavaDoc l) {
106         list.remove(ChangeListener JavaDoc.class, l);
107     }
108     
109     /**
110      * Executed once per minute
111      */

112     private void timer() {
113         switch (state) {
114             case STATE_NOTASK:
115                 break;
116             case STATE_SUSPENDED: {
117                 long lastActivity = ActivityListener.getLastActivityMillis();
118                 long cur = System.currentTimeMillis();
119
120                 // summer time and similar
121
if (lastActivity > cur)
122                     lastActivity = cur;
123                 
124                 if ((cur - lastActivity) < INACTIVITY_DURATION) {
125                     StatusDisplayer.getDefault().setStatusText(
126                             NbBundle.getMessage(StartedUserTask.class,
127                             "ActivityDetected"));
128                     state = STATE_WORKING;
129                     
130                     startedAt = lastActivity;
131                     int diff = (int) ((cur - startedAt) / (60 * 1000));
132                     
133                     started.setSpentTime(initialSpentTime + diff);
134                     
135                     if (Settings.getDefault().getCollectWorkPeriods()) {
136                         workPeriod = new UserTask.WorkPeriod(
137                             lastActivity, diff);
138                         started.getWorkPeriods().add(workPeriod);
139                     }
140                 }
141                 break;
142             }
143             case STATE_WORKING: {
144                 long now = System.currentTimeMillis();
145
146                 // summer time and similar
147
if (startedAt > now)
148                     startedAt = now;
149                     
150                 int diff = (int) ((now - startedAt) / (60 * 1000));
151
152                 if ((System.currentTimeMillis() -
153                     ActivityListener.getLastActivityMillis()) > INACTIVITY_DURATION &&
154                     Settings.getDefault().getDetectInactivity()) { // NOI18N
155
state = STATE_SUSPENDED;
156                     StatusDisplayer.getDefault().setStatusText(
157                             NbBundle.getMessage(StartedUserTask.class,
158                             "InactivityDetected"));
159                 }
160                 
161                 started.setSpentTime(initialSpentTime + diff);
162                 if (Settings.getDefault().getCollectWorkPeriods()) {
163                     // it is possible that workPeriod is null although
164
// getCollectWorkPeriods() returns true if the
165
// value of the option was changed during this
166
// task was running
167
if (workPeriod == null) {
168                         workPeriod = new UserTask.WorkPeriod(startedAt, diff);
169                         started.getWorkPeriods().add(workPeriod);
170                     } else {
171                         workPeriod.setDuration(diff);
172                     }
173                 }
174                 break;
175             }
176             default:
177                 throw new InternalError JavaDoc("wrong state"); // NOI18N
178
}
179     }
180     
181     /**
182      * Starts another task. If a task is currently running it should be
183      * stopped first.
184      *
185      * @param task currently working on this task. May be null.
186      */

187     public void start(UserTask task) {
188         switch (state) {
189             case STATE_WORKING: {
190                 if (task != null) {
191                     throw new InternalError JavaDoc("the task " + started + // NOI18N
192
" should be stopped first"); // NOI18N
193
} else {
194                     long now = System.currentTimeMillis();
195
196                     // summer time and similar
197
if (startedAt > now)
198                         startedAt = now;
199                     
200                     int diff = (int) ((now - startedAt) / (60 * 1000));
201                     
202                     updateSpentTime(diff);
203                     UserTask ut = started;
204                     clear();
205                     ut.clearEmptyWorkPeriods();
206                     ut.firePropertyChange("started", Boolean.TRUE,
207                         Boolean.FALSE); // NOI18N
208
fireChange();
209                 }
210                 break;
211             }
212             case STATE_SUSPENDED: {
213                 if (task != null) {
214                     throw new InternalError JavaDoc("the task " + started + // NOI18N
215
" should be stopped first"); // NOI18N
216
} else {
217                     long lastActivity = ActivityListener.getLastActivityMillis();
218                     long now = System.currentTimeMillis();
219
220                     // summer time and similar
221
if (lastActivity > now)
222                         lastActivity = now;
223                     
224                     int diff = (int) ((now - lastActivity) / (60 * 1000));
225                     
226                     workPeriod = null;
227                     updateSpentTime(diff);
228                     UserTask ut = started;
229                     clear();
230                     ut.clearEmptyWorkPeriods();
231                     ut.firePropertyChange("started", Boolean.TRUE,
232                         Boolean.FALSE); // NOI18N
233
fireChange();
234                 }
235                 break;
236             }
237             case STATE_NOTASK: {
238                 if (task == null) {
239                     throw new InternalError JavaDoc("no task is running"); // NOI18N
240
} else {
241                     task.setValuesComputed(false);
242                     started = task;
243                     startedAt = System.currentTimeMillis();
244                     initialSpentTime = task.getSpentTime();
245                     state = STATE_WORKING;
246                     updateSpentTime(0);
247                     started.firePropertyChange("started", Boolean.FALSE,
248                         Boolean.TRUE); // NOI18N
249
fireChange();
250                 }
251                 break;
252             }
253             default:
254                 throw new InternalError JavaDoc("wrong state"); // NOI18N
255
}
256     }
257
258     /**
259      * Updates the spentTime property of the running task.
260      *
261      * @param diff spent time since startedAt in minutes
262      */

263     private void updateSpentTime(int diff) {
264         started.setSpentTime(initialSpentTime + diff);
265         if (Settings.getDefault().getCollectWorkPeriods()) {
266             // it is possible that workPeriod is null although
267
// getCollectWorkPeriods() returns true if the
268
// value of the option was changed during this
269
// task was running
270
if (workPeriod == null) {
271                 workPeriod = new UserTask.WorkPeriod(startedAt, diff);
272                 started.getWorkPeriods().add(workPeriod);
273             } else {
274                 workPeriod.setDuration(diff);
275             }
276         }
277     }
278     
279     /**
280      * Clears all internal fields
281      * and returns to STATE_NOTASK.
282      */

283     private void clear() {
284         started = null;
285         startedAt = 0;
286         initialSpentTime = 0;
287         state = STATE_NOTASK;
288         workPeriod = null;
289     }
290     
291     /**
292      * Returns started task
293      *
294      * @return started task or null
295      */

296     public UserTask getStarted() {
297         return started;
298     }
299 }
300
Popular Tags