1 19 20 package org.netbeans.modules.tasklist.usertasks.model; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Set ; 26 import javax.swing.event.ChangeEvent ; 27 import javax.swing.event.ChangeListener ; 28 import javax.swing.event.EventListenerList ; 29 import org.netbeans.modules.tasklist.core.util.ObjectListEvent; 30 import org.netbeans.modules.tasklist.core.util.ObjectListListener; 31 import org.netbeans.modules.tasklist.usertasks.DueTasksNotifier; 32 33 40 public class UserTaskList { 41 44 public static interface UserTaskProcessor { 45 50 public void process(UserTask ut); 51 } 52 53 private UserTaskObjectList tasks; 54 55 59 public Object userObject; 60 61 private DueTasksNotifier dueTasksNotifier; 62 63 64 private EventListenerList listeners = new EventListenerList (); 65 66 69 public UserTaskList() { 70 tasks = new UserTaskObjectList(this); 71 tasks.addListener(new ObjectListListener() { 72 public void listChanged(ObjectListEvent ev) { 73 UserTaskList.this.fireChange(); 74 } 75 }); 76 } 77 78 83 public String [] getOwners() { 84 Iterator it = this.getSubtasks().iterator(); 85 Set <String > cat = new java.util.HashSet <String >(); 86 while (it.hasNext()) { 87 UserTask ut = (UserTask) it.next(); 88 findOwners(ut, cat); 89 } 90 return cat.toArray(new String [cat.size()]); 91 } 92 93 100 private static void findOwners(UserTask task, Set <String > cat) { 101 if (task.getOwner().length() != 0) 102 cat.add(task.getOwner()); 103 104 Iterator it = task.getSubtasks().iterator(); 105 while (it.hasNext()) { 106 findOwners((UserTask) it.next(), cat); 107 } 108 } 109 110 115 public String [] getCategories() { 116 Iterator it = this.getSubtasks().iterator(); 117 Set <String > cat = new java.util.HashSet <String >(); 118 while (it.hasNext()) { 119 UserTask ut = (UserTask) it.next(); 120 findCategories(ut, cat); 121 } 122 return cat.toArray(new String [cat.size()]); 123 } 124 125 132 private static void findCategories(UserTask task, Set <String > cat) { 133 if (task.getCategory().length() != 0) 134 cat.add(task.getCategory()); 135 136 Iterator it = task.getSubtasks().iterator(); 137 while (it.hasNext()) { 138 findCategories((UserTask) it.next(), cat); 139 } 140 } 141 142 public UserTask findItem(Iterator tasks, String uid) { 144 while (tasks.hasNext()) { 145 UserTask task = (UserTask)tasks.next(); 146 if (task.getUID().equals(uid)) { 147 return task; 148 } 149 if (!task.getSubtasks().isEmpty()) { 150 UserTask f = findItem(task.getSubtasks().iterator(), uid); 151 if (f != null) { 152 return f; 153 } 154 } 155 } 156 return null; 157 } 158 159 165 public static void processDepthFirst(UserTaskProcessor p, UserTaskObjectList list) { 166 for (int i = 0; i < list.size(); i++) { 167 UserTask ut = list.getUserTask(i); 168 processDepthFirst(p, ut.getSubtasks()); 169 p.process(ut); 170 } 171 } 172 173 178 public final UserTaskObjectList getSubtasks() { 179 return tasks; 180 } 181 182 185 public void destroy() { 186 Iterator it = getSubtasks().iterator(); 187 while (it.hasNext()) { 188 UserTask ut = (UserTask) it.next(); 189 ut.destroy(); 190 } 191 } 192 193 198 public List getAllSubtasks() { 199 List <UserTask> ret = new ArrayList <UserTask>(); 200 collectAllSubtasks(ret, getSubtasks()); 201 return ret; 202 } 203 204 210 private void collectAllSubtasks(List <UserTask> ret, UserTaskObjectList tasks) { 211 for (int i = 0; i < tasks.size(); i++) { 212 ret.add(tasks.getUserTask(i)); 213 collectAllSubtasks(ret, tasks.getUserTask(i).getSubtasks()); 214 } 215 } 216 217 223 public void addChangeListener(ChangeListener l) { 224 listeners.add(ChangeListener .class, l); 225 } 226 227 232 public void removeChangeListener(ChangeListener l) { 233 listeners.remove(ChangeListener .class, l); 234 } 235 236 239 void fireChange() { 240 Object [] list = listeners.getListenerList(); 242 243 ChangeEvent changeEvent = null; 246 for (int i = list.length - 2; i >= 0; i -= 2) { 247 if (list[i] == ChangeListener .class) { 248 if (changeEvent == null) 250 changeEvent = new ChangeEvent (this); 251 ((ChangeListener ) list[i+1]).stateChanged(changeEvent); 252 } 253 } 254 } 255 256 286 287 } 289 | Popular Tags |