1 package org.netbeans.modules.tasklist.usertasks.model; 2 3 import java.util.TimerTask ; 4 import javax.swing.SwingUtilities ; 5 import javax.swing.event.ChangeEvent ; 6 import javax.swing.event.ChangeListener ; 7 import javax.swing.event.EventListenerList ; 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 21 public class StartedUserTask { 22 private static final java.util.Timer TIMER = new java.util.Timer (true); 23 24 private static final int INACTIVITY_DURATION = 10 * 60 * 1000; 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 38 public static StartedUserTask getInstance() { 39 return INSTANCE; 40 } 41 42 45 private UserTask started = null; 46 47 50 private long startedAt; 51 52 private int initialSpentTime; 53 private int state = STATE_NOTASK; 54 private UserTask.WorkPeriod workPeriod; 55 private EventListenerList list = new EventListenerList (); 56 57 static { 58 TIMER.scheduleAtFixedRate(new TimerTask () { 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 72 private StartedUserTask() { 73 } 74 75 78 private void fireChange() { 79 ChangeEvent event = null; 80 Object [] listeners = list.getListenerList(); 81 for (int i = listeners.length - 2; i >= 0; i -= 2) { 82 if (listeners[i] == ChangeListener .class) { 83 if (event == null) 84 event = new ChangeEvent (this); 85 ((ChangeListener ) listeners[i + 1]).stateChanged(event); 86 } 87 } 88 } 89 90 96 public void addChangeListener(ChangeListener l) { 97 list.add(ChangeListener .class, l); 98 } 99 100 105 public void removeChangeListener(ChangeListener l) { 106 list.remove(ChangeListener .class, l); 107 } 108 109 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 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 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()) { 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 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 ("wrong state"); } 179 } 180 181 187 public void start(UserTask task) { 188 switch (state) { 189 case STATE_WORKING: { 190 if (task != null) { 191 throw new InternalError ("the task " + started + " should be stopped first"); } else { 194 long now = System.currentTimeMillis(); 195 196 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); fireChange(); 209 } 210 break; 211 } 212 case STATE_SUSPENDED: { 213 if (task != null) { 214 throw new InternalError ("the task " + started + " should be stopped first"); } else { 217 long lastActivity = ActivityListener.getLastActivityMillis(); 218 long now = System.currentTimeMillis(); 219 220 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); fireChange(); 234 } 235 break; 236 } 237 case STATE_NOTASK: { 238 if (task == null) { 239 throw new InternalError ("no task is running"); } 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); fireChange(); 250 } 251 break; 252 } 253 default: 254 throw new InternalError ("wrong state"); } 256 } 257 258 263 private void updateSpentTime(int diff) { 264 started.setSpentTime(initialSpentTime + diff); 265 if (Settings.getDefault().getCollectWorkPeriods()) { 266 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 283 private void clear() { 284 started = null; 285 startedAt = 0; 286 initialSpentTime = 0; 287 state = STATE_NOTASK; 288 workPeriod = null; 289 } 290 291 296 public UserTask getStarted() { 297 return started; 298 } 299 } 300 | Popular Tags |