1 19 20 package org.netbeans.modules.tasklist.core; 21 22 23 import java.io.*; 24 import java.util.*; 25 26 32 public class TaskList implements ObservableList, TaskListener { 33 34 final static String USER_CATEGORY = "usertasks"; 37 48 private List tasks = new LinkedList(); 49 50 private List listCopy; 52 53 private final ArrayList listeners = new ArrayList(67); 54 55 56 protected boolean needSave = false; 57 protected boolean dontSave = false; 58 59 62 public TaskList() { 63 } 64 65 protected void setNeedSave(boolean b) { 66 needSave = b; 67 } 68 69 protected void setDontSave(boolean b) { 70 dontSave = b; 71 } 72 73 74 public final List getTasks() { 75 synchronized(tasks) { 76 if (listCopy == null) { 77 listCopy = Collections.unmodifiableList(new ArrayList(tasks)); 79 } 80 return listCopy; 81 } 82 } 83 84 105 public void addRemove(List addList, List removeList, boolean append, 106 Task parent, Task after) { 107 setSilentUpdate(true, false); 109 110 boolean modified = false; 111 112 Iterator it; 115 if (removeList != null) { 116 it = removeList.iterator(); 117 while (it.hasNext()) { 118 Task task = (Task) it.next(); 119 if (parent != null) { 120 parent.removeSubtask(task); 121 task.removeTaskListener(this); 122 } else { 123 removeTask(task); 124 } 125 modified = true; 126 } 127 } 128 129 136 if (addList != null && addList.size()>0) { 137 modified = true; 138 139 if (parent != null) { 141 it = addList.iterator(); 142 while (it.hasNext()) { 143 Task next = (Task) it.next(); 144 next.addTaskListener(this); 145 } 146 parent.addSubtasks(addList, append, after); 147 } else { 148 if (after != null) { 149 addTasks(addList, after); 150 } else { 151 addTasks(addList, append); 152 } 153 } 154 } 155 156 159 setSilentUpdate(false, modified); 163 } 164 165 169 public final void addTasks(List tasks_, boolean append) { 170 Iterator it = tasks_.iterator(); 171 int slot = 0; 172 while (it.hasNext()) { 173 Task task = (Task) it.next(); 174 task.addTaskListener(this); 175 if (append) { 176 synchronized(tasks) { 177 tasks.add(task); 178 listCopy = null; 179 } 180 } else { 181 synchronized(tasks) { 182 tasks.add(slot++, task); 183 listCopy = null; 184 } 185 } 186 fireAdded(task); } 188 } 189 190 private void addTasks(List tasks, Task after) { 191 int index = tasks.indexOf(after); 192 if (index == -1) { 193 addTasks(tasks, true); 194 } else { 195 tasks.addAll(index+1, tasks); 196 Iterator it = tasks.iterator(); 197 while (it.hasNext()) { 198 Task task = (Task) it.next(); 199 task.addTaskListener(this); 200 fireAdded(task); 201 } 202 } 203 } 204 205 208 public final void appendTask(Task task) { 209 task.addTaskListener(this); 210 synchronized(tasks) { 211 tasks.add(task); 212 listCopy = null; 213 } 214 fireAdded(task); 215 } 216 217 220 public final void removeTasks(List tasks) { 221 Iterator it = tasks.iterator(); 222 while (it.hasNext()) { 223 Task task = (Task) it.next(); 224 removeTask(task); 225 } 226 } 227 228 231 public final void removeTask(Task task) { 232 task.removeTaskListener(this); 233 boolean removed = false; 234 int index; 235 synchronized(tasks) { 236 index = tasks.indexOf(task); 237 removed = tasks.remove(task); 238 listCopy = null; 239 } 240 if (removed) { 241 fireRemoved(null, task, index); } 243 } 244 245 249 public void markChanged() { 250 needSave = true; 259 save(); 260 } 261 262 263 public void save() { 264 needSave = false; 265 } 266 267 276 final void setSilentUpdate(boolean silentUpdate, 277 boolean saveOnFinish) { dontSave = silentUpdate; 279 needSave = true; 280 if (!dontSave && saveOnFinish) { 291 save(); 293 } 294 } 295 296 public void addTaskListener(TaskListener listener) { 297 synchronized (listeners) { 298 listeners.add(listener); 299 } 300 } 301 302 public void removeTaskListener(TaskListener listener) { 303 synchronized (listeners) { 304 listeners.remove(listener); 305 } 306 } 307 308 309 protected void fireAdded(Task task) { 310 synchronized (listeners) { 311 int n = listeners.size(); 312 for (int i = 0; i < n; i++) { 313 TaskListener tl = (TaskListener) listeners.get(i); 314 tl.addedTask(task); 315 } 316 } 317 } 318 319 320 protected void fireStructureChanged(Task task) { 321 TaskListener[] taskListeners; synchronized (listeners) { 323 taskListeners = new TaskListener[listeners.size()]; 324 taskListeners = (TaskListener[]) listeners.toArray(taskListeners); 325 } 326 for (int i = 0; i < taskListeners.length; i++) { 327 taskListeners[i].structureChanged(task); 328 } 329 } 330 331 332 protected void fireRemoved(Task pt, Task task, int index) { 333 TaskListener[] taskListeners; synchronized (listeners) { 335 taskListeners = new TaskListener[listeners.size()]; 336 taskListeners = (TaskListener[]) listeners.toArray(taskListeners); 337 } 338 for (int i = 0; i < taskListeners.length; i++) { 339 taskListeners[i].removedTask(pt, task, index); 340 } 341 342 } 343 344 349 public int size() { 350 synchronized(tasks) { 351 return TLUtils.recursiveCount(tasks.iterator()); 352 } 353 } 354 355 358 public void clear() { 359 synchronized(tasks) { 360 tasks.clear(); 361 listCopy = null; 362 } 363 fireStructureChanged(null); 364 } 365 366 367 public void print() { 368 System.err.println("\nTask List:\n-------------"); synchronized(tasks) { 370 Iterator it = tasks.iterator(); 371 while (it.hasNext()) { 372 Task next = (Task) it.next(); 373 recursivePrint(next, 0); 374 } 375 } 376 377 System.err.println("\n\n"); 378 } 379 380 private void recursivePrint(Task node, int depth) { 381 if (depth > 20) { Thread.dumpStack(); 383 return; 384 } 385 for (int i = 0; i < depth; i++) { 386 System.err.print(" "); 387 } 388 System.err.println(node); 389 if (node.getSubtasks() != null) { 390 List l = node.getSubtasks(); 391 ListIterator it = l.listIterator(); 392 while (it.hasNext()) { 393 Task task = (Task) it.next(); 394 recursivePrint(task, depth + 1); 395 } 396 } 397 } 398 399 401 402 public void selectedTask(Task t) { 403 } 405 406 407 public void warpedTask(Task t) { 408 } 410 411 412 public void addedTask(Task t) { 413 fireAdded(t); 414 } 415 416 417 public void removedTask(Task pt, Task t, int index) { 418 fireRemoved(pt, t, index); 419 } 420 421 422 public void structureChanged(Task t) { 423 fireStructureChanged(t); 424 } 425 426 431 public boolean isEmpty() { 432 return tasks.size() == 0; 433 } 434 } 435 | Popular Tags |