1 19 20 21 package org.netbeans.modules.tasklist.core; 22 23 import org.openide.nodes.AbstractNode; 24 import org.openide.nodes.Children; 25 import org.openide.nodes.Node; 26 import org.openide.util.RequestProcessor; 27 28 import javax.swing.*; 29 import java.util.Collections ; 30 import java.util.Enumeration ; 31 32 38 public class TaskListNode extends AbstractNode { 39 40 47 public TaskListNode(ObservableList tasklist) { 48 super(new TaskListChildren(tasklist)); 49 } 50 51 58 public TaskListNode(ObservableList tasklist, NodeFactory nodeFactory) { 59 super(new TaskListChildren(tasklist)); 60 TaskListChildren list = (TaskListChildren) getChildren(); 61 list.setNodeFactory(nodeFactory); 62 } 63 64 public Action[] getActions(boolean context) { 65 return new Action[0]; 66 } 67 68 69 70 public static interface NodeFactory { 71 72 73 Node createNode(Object task); 74 } 75 76 public void destroy() throws java.io.IOException { 77 Enumeration en = getChildren().nodes(); 79 while (en.hasMoreElements()) { 80 Node next = (Node) en.nextElement(); 81 next.destroy(); 82 } 83 super.destroy(); 84 } 85 86 static class TaskListChildren extends Children.Keys implements TaskListener, Runnable { 87 88 private ObservableList list; 89 private NodeFactory nodeFactory; 90 private static int BATCH_INTERVAL_MS = 59; 91 private volatile RequestProcessor.Task batchSetKeys; 92 private volatile boolean active = false; 93 94 TaskListChildren(ObservableList list) { 95 assert list != null; 96 this.list = list; 97 } 98 99 protected void addNotify() { 100 super.addNotify(); 101 setKeys(list.getTasks()); 102 list.addTaskListener(this); 103 active = true; 104 } 105 106 protected void removeNotify() { 107 active = false; 108 list.removeTaskListener(this); 109 setKeys(Collections.EMPTY_SET); 110 super.removeNotify(); 111 } 112 113 protected Node[] createNodes(Object key) { 114 Task task = (Task) key; 115 Node[] nodes; 116 if (nodeFactory == null) { 117 nodes = task.createNode(); 118 } else { 119 nodes = new Node[] {nodeFactory.createNode(task)}; 120 } 121 return nodes; 122 } 123 124 public void setNodeFactory(NodeFactory nodeFactory) { 125 this.nodeFactory = nodeFactory; 126 } 127 128 private void batchSetKeys() { 131 if (batchSetKeys == null) { 132 batchSetKeys = RequestProcessor.getDefault().post(this, BATCH_INTERVAL_MS); 133 } 134 } 135 136 138 public void selectedTask(Task t) { 139 } 140 141 public void warpedTask(Task t) { 142 } 143 144 public void addedTask(Task t) { 145 batchSetKeys(); 146 } 147 148 public void removedTask(Task pt, Task t, int index) { 149 batchSetKeys(); 150 } 151 152 public void structureChanged(Task t) { 153 batchSetKeys(); 154 } 155 156 public void run() { 158 batchSetKeys = null; 159 if (active) { 160 setKeys(list.getTasks()); 161 } 162 } 163 164 165 } 166 } 167 | Popular Tags |