1 19 20 package org.netbeans.modules.tasklist.core; 21 22 23 import java.awt.Image ; 24 import java.awt.datatransfer.Transferable ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.io.IOException ; 28 import java.util.*; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import org.netbeans.modules.tasklist.core.filter.FilterAction; 32 import org.openide.actions.CopyAction; 33 import org.openide.actions.CutAction; 34 import org.openide.actions.DeleteAction; 35 import org.openide.actions.PasteAction; 36 import org.openide.actions.PropertiesAction; 37 import org.openide.loaders.InstanceSupport; 38 39 import org.openide.nodes.*; 40 import org.openide.util.HelpCtx; 41 import org.openide.util.actions.SystemAction; 42 import org.openide.util.datatransfer.ExTransferable; 43 44 public class TaskNode extends AbstractNode { 45 private static final Logger LOGGER = TLUtils.getLogger(TaskNode.class); 46 47 static { 48 LOGGER.setLevel(Level.FINE); 49 } 50 51 protected final Task item; 52 private Monitor monitor; 53 54 57 public TaskNode(Task item, Children children) { 58 super(children); 59 this.item = item; 60 init(); 61 } 62 63 66 public TaskNode(Task item) { 67 this(item, Children.LEAF); 68 } 69 70 73 public Node.Cookie getCookie(Class type) { 74 if (type == Task.class) { 75 return item; 76 } else { 77 return super.getCookie(type); 78 } 79 } 80 81 82 private void init() { 83 setName(item.getSummary()); 84 85 monitor = new Monitor(); 86 item.addTaskListener(monitor); 87 item.addPropertyChangeListener(monitor); 88 DisposalListener dl = new DisposalListener(); 89 addNodeListener(dl); updateDisplayStuff(); 91 getCookieSet().add(new InstanceSupport.Instance(item)); 92 93 } 96 97 private class DisposalListener extends NodeAdapter { 98 public DisposalListener () {} 99 100 public void nodeDestroyed(NodeEvent ev) { 101 if (ev.getNode() == TaskNode.this) { 102 item.removeTaskListener(monitor); 103 item.removePropertyChangeListener(monitor); 104 } 105 } 106 } 107 108 public TaskChildren getTaskChildren() { 109 return (TaskChildren) getChildren(); 110 } 111 112 public Node cloneNode () { 121 TaskNode clon = new TaskNode(this.item); 122 if (!clon.isLeaf()) 123 clon.setChildren((TaskChildren)getTaskChildren().clone()); 124 return clon; 125 } 126 127 protected TaskChildren createChildren() { 128 return new TaskChildren(this.item); 129 } 130 131 protected final void updateDisplayStuff() { 132 setDisplayName(item.getSummary()); 133 updateIcon(); 134 } 135 136 protected void updateIcon() { 137 setIconBase((item.getAction() != null) ? 140 "org/netbeans/modules/tasklist/core/lightbulb" : "org/netbeans/modules/tasklist/core/task"); } 143 144 public Image getIcon(int type) { 145 if (item.getIcon() != null) { 146 return item.getIcon(); 147 } else { 148 return super.getIcon(type); 149 } 150 } 151 152 public Image getOpenedIcon(int type) { 153 if (item.getIcon() != null) { 154 return item.getIcon(); 155 } else { 156 return super.getOpenedIcon(type); 157 } 158 } 159 160 public HelpCtx getHelpCtx() { 161 return new HelpCtx(TaskNode.class); 162 } 163 164 protected SystemAction[] createActions() { 165 171 return new SystemAction[] { 172 null, 173 SystemAction.get(FilterAction.class), 174 null, 175 SystemAction.get(ExpandAllAction.class), 176 null, 177 SystemAction.get(CutAction.class), 178 SystemAction.get(CopyAction.class), 179 SystemAction.get(PasteAction.class), 180 null, 181 SystemAction.get(DeleteAction.class), 182 null, 183 SystemAction.get(PropertiesAction.class), 184 }; 185 } 186 187 public void destroy() throws IOException { 188 item.removePropertyChangeListener(monitor); 189 item.removeTaskListener(monitor); 190 191 196 Enumeration en = getChildren().nodes(); 198 while (en.hasMoreElements()) { 199 Node next = (Node) en.nextElement(); 200 next.destroy(); 201 } 202 super.destroy(); 203 } 204 205 public boolean canDestroy() { 206 return true; 207 } 208 209 211 protected Sheet createSheet() { 212 Sheet s = Sheet.createDefault(); 213 Sheet.Set ss = s.get(Sheet.PROPERTIES); 214 ss.put(new SuggestionNodeProperty(item, TaskProperties.PROP_SUMMARY)); 215 return s; 216 217 228 } 229 230 public boolean canRename() { 231 return true; 232 } 233 234 public void setName(String nue) { 235 super.setName(nue); 236 if (!nue.equals(item.getSummary())) { 237 item.setSummary(nue); 238 } 239 } 240 241 protected void createPasteTypes(Transferable t, List s) { 242 } 243 244 public boolean canCopy () { 246 return true; 247 } 248 public boolean canCut () { 249 return true; 250 } 251 252 public Transferable clipboardCopy() throws IOException { 253 LOGGER.fine("entering"); 254 return new ExTransferable.Single(TaskTransfer.TODO_FLAVOR) { 255 protected Object getData() { 256 return item.clone(); 257 } 258 }; 259 } 260 261 public Transferable clipboardCut() throws IOException { 262 destroy(); 263 return clipboardCopy(); 264 } 265 266 277 278 283 284 327 328 331 public static Node find(Node root, Task target) { 332 Task item = getTask(root); 333 if (item == target) { 334 return root; 336 } 337 338 347 Task p = target; 349 LinkedList ancestry = new LinkedList(); 350 while ((p != null) && (p != item)) { 351 ancestry.addFirst(p); 352 p = p.getParent(); 353 } 354 355 Node n = root; 356 ListIterator it = ancestry.listIterator(); 357 while (it.hasNext()) { 358 Task parent = (Task)it.next(); 359 org.openide.nodes.Children c = n.getChildren(); 361 Node[] nc = c.getNodes(); 362 for (int i = 0; i < nc.length; i++) { 363 n = nc[i]; 364 if (getTask(n) == parent) { 365 break; 366 } 367 } 368 } 369 if (getTask(n) == target) { 370 return n; 371 } else { 372 return null; 373 } 374 } 375 376 378 public static Task getTask(Node n) { 379 if (n == null) { 380 return null; 381 } 382 return (Task) n.getCookie(Task.class); 383 } 384 385 387 public static TaskNode getTaskNode(Node n) { 388 if (n == null) { 389 return null; 390 } 391 if (n instanceof TaskNode) { 392 return (TaskNode)n; 393 } else if (n instanceof FilterTaskNode) { 394 n = ((FilterTaskNode)n).getOriginal(); 395 if (n instanceof TaskNode) { 396 return (TaskNode)n; 397 } 398 } 399 return null; 400 } 401 402 404 private class Monitor implements TaskListener, PropertyChangeListener { 405 public Monitor () {} 406 407 public void selectedTask(Task t) { 408 } 410 411 public void warpedTask(Task t) { 412 } 414 415 public void addedTask(Task t) { 416 if (t.getParent().getKey() == item.getKey()) { 417 Children c = getChildren(); 419 if (c == Children.LEAF) { 420 assert item.hasSubtasks(); 421 setChildren(createChildren()); 423 } 424 } 425 } 426 427 public void removedTask(Task pt, Task t, int index) { 428 } 430 431 public void structureChanged(Task t) { 432 if (t == null) return; 433 if (t.getKey() == item.getKey()) { 434 Children c = getChildren(); 436 if ((c == Children.LEAF) && (item.hasSubtasks())) { 437 setChildren(createChildren()); 439 } 440 } 441 } 442 443 public void propertyChange(PropertyChangeEvent evt) { 444 updateDisplayStuff(); 446 firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue()); 447 } 448 449 } 450 } 451 452 | Popular Tags |