1 19 package org.openide.explorer.view; 20 21 import org.openide.explorer.*; 22 import org.openide.explorer.ExplorerManager.Provider; 23 import org.openide.nodes.Node; 24 import org.openide.nodes.Node.Property; 25 26 import java.awt.*; 27 28 import java.beans.*; 29 import java.util.logging.Logger ; 30 31 import javax.swing.*; 32 import javax.swing.tree.*; 33 34 35 71 public class ContextTreeView extends TreeView { 72 73 static final long serialVersionUID = -8282594827988436813L; 74 75 static final Logger LOG = Logger.getLogger(ContextTreeView.class.getName()); 76 77 79 public ContextTreeView() { 80 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 81 } 82 83 85 protected boolean selectionAccept(Node[] nodes) { 86 if (nodes.length == 0) { 87 return true; 88 } 89 90 Node parent = nodes[0].getParentNode(); 91 92 for (int i = 1; i < nodes.length; i++) { 93 if (nodes[i].getParentNode() != parent) { 94 return false; 95 } 96 } 97 98 return true; 99 } 100 101 104 protected void selectionChanged(Node[] nodes, ExplorerManager man) 105 throws PropertyVetoException { 106 if (nodes.length > 0) { 107 man.setExploredContext(nodes[0]); 108 } 109 110 man.setSelectedNodes(nodes); 111 } 112 113 116 protected void showPath(TreePath path) { 117 tree.makeVisible(path); 118 119 Rectangle rect = tree.getPathBounds(path); 120 121 if (rect != null) { 122 rect.width += rect.x; 123 rect.x = 0; 124 tree.scrollRectToVisible(rect); 125 } 126 127 tree.setSelectionPath(path); 128 } 129 130 134 protected void showSelection(TreePath[] paths) { 135 if (paths.length == 0) { 136 tree.setSelectionPaths(new TreePath[0]); 137 } else { 138 tree.setSelectionPath(paths[0].getParentPath()); 139 } 140 } 141 142 146 protected boolean useExploredContextMenu() { 147 return true; 148 } 149 150 152 protected NodeTreeModel createModel() { 153 return new NodeContextModel(); 154 } 155 156 158 static final class NodeContextModel extends NodeTreeModel { 159 private int[] newIndices; 163 private Object [] newChildren; 164 165 public java.lang.Object getChild(java.lang.Object parent, int index) { 166 int superCnt = super.getChildCount(parent); 167 int myCnt = 0; 168 169 for (int i = 0; i < superCnt; i++) { 170 Object origChild = super.getChild(parent, i); 171 Node n = Visualizer.findNode(origChild); 172 173 if (!n.isLeaf()) { 174 if (myCnt++ == index) { 175 return origChild; 176 } 177 } 178 } 179 180 return null; 181 } 182 183 public int getChildCount(java.lang.Object parent) { 184 int superCnt = super.getChildCount(parent); 185 int myCnt = 0; 186 187 for (int i = 0; i < superCnt; i++) { 188 Node n = Visualizer.findNode(super.getChild(parent, i)); 189 190 if (!n.isLeaf()) { 191 myCnt++; 192 } 193 } 194 195 return myCnt; 196 } 197 198 public int getIndexOfChild(java.lang.Object parent, java.lang.Object child) { 199 int superCnt = super.getChildCount(parent); 200 int myCnt = 0; 201 202 for (int i = 0; i < superCnt; i++) { 203 Object origChild = super.getChild(parent, i); 204 205 if (child.equals(origChild)) { 206 return myCnt; 207 } 208 209 Node n = Visualizer.findNode(origChild); 210 211 if (!n.isLeaf()) { 212 myCnt++; 213 } 214 } 215 216 return -1; 217 } 218 219 public boolean isLeaf(java.lang.Object node) { 220 return false; 221 } 222 223 226 private boolean filterEvent(Object [] path, int[] childIndices, Object [] children) { 227 assert (childIndices != null) && (children != null) : " ch: " + children + " indices: " + childIndices; assert children.length == childIndices.length : "They should be the same: " + children.length + " == " + 229 childIndices.length; assert newChildren == null : "Children should be cleared: " + newChildren; assert newIndices == null : "indices should be cleared: " + newIndices; assert path.length > 0 : "Path has to be greater than zero " + path.length; 234 VisualizerNode parent = (VisualizerNode) path[path.length - 1]; 235 236 int[] filter = new int[childIndices.length]; 237 int accepted = 0; 238 239 for (int i = 0; i < childIndices.length; i++) { 240 VisualizerNode n = (VisualizerNode) children[i]; 241 242 if (!n.isLeaf()) { 243 filter[accepted++] = i; 244 } 245 } 246 247 if (accepted == 0) { 248 return false; 249 } 250 251 newIndices = new int[accepted]; 252 newChildren = new Object [accepted]; 253 254 for (int i = 0; i < accepted; i++) { 255 newChildren[i] = children[filter[i]]; 256 newIndices[i] = getIndexOfChild(parent, newChildren[i]); 257 } 258 259 return true; 260 } 261 262 265 private boolean removalEvent(Object [] path, int[] childIndices, Object [] children) { 266 assert (childIndices != null) && (children != null) : " ch: " + children + " indices: " + childIndices; assert children.length == childIndices.length : "They should be the same: " + children.length + " == " + 268 childIndices.length; assert newChildren == null : "Children should be cleared: " + newChildren; assert newIndices == null : "indices should be cleared: " + newIndices; assert path.length > 0 : "Path has to be greater than zero " + path.length; 273 VisualizerNode parent = (VisualizerNode) path[path.length - 1]; 274 275 int[] filter = new int[childIndices.length]; 276 int accepted = 0; 277 278 for (int i = 0; i < childIndices.length; i++) { 279 VisualizerNode n = (VisualizerNode) children[i]; 280 281 if (!n.isLeaf()) { 282 filter[accepted++] = i; 283 } 284 } 285 286 if (accepted == 0) { 287 return false; 288 } 289 290 newIndices = new int[accepted]; 291 newChildren = new Object [accepted]; 292 293 int size = getChildCount(parent); 294 int index = 0; 295 int myPos = 0; 296 int actualI = 0; 297 int i = 0; 298 299 for (int pos = 0; pos < accepted;) { 300 if (childIndices[index] <= i) { 301 VisualizerNode n = (VisualizerNode) children[index]; 302 303 if (!n.isLeaf()) { 304 newIndices[pos] = myPos++; 305 newChildren[pos] = n; 306 pos++; 307 } 308 309 index++; 310 } else { 311 VisualizerNode n = (VisualizerNode) getChild(parent, actualI++); 312 313 if ((n != null) && !n.isLeaf()) { 314 myPos++; 315 } 316 } 317 318 i++; 319 } 320 321 return true; 322 } 323 324 332 protected void fireTreeNodesRemoved(Object source, Object [] path, int[] childIndices, Object [] children) { 333 LOG.fine("fireTreeNodesRemoved"); if (!removalEvent(path, childIndices, children)) { 335 LOG.fine("fireTreeNodesRemoved - exit"); return; 337 } 338 339 super.fireTreeNodesRemoved(source, path, newIndices, newChildren); 340 newIndices = null; 341 newChildren = null; 342 LOG.fine("fireTreeNodesRemoved - end"); } 344 345 protected void fireTreeNodesInserted(Object source, Object [] path, int[] childIndices, Object [] children) { 346 LOG.fine("fireTreeNodesInserted"); if (!filterEvent(path, childIndices, children)) { 348 LOG.fine("fireTreeNodesInserted - exit"); return; 350 } 351 352 super.fireTreeNodesInserted(source, path, newIndices, newChildren); 353 newIndices = null; 354 newChildren = null; 355 LOG.fine("fireTreeNodesInserted - end"); } 357 358 protected void fireTreeNodesChanged(Object source, Object [] path, int[] childIndices, Object [] children) { 359 LOG.fine("fireTreeNodesChanged"); if (!filterEvent(path, childIndices, children)) { 361 LOG.fine("fireTreeNodesChanged - exit"); return; 363 } 364 365 super.fireTreeNodesChanged(source, path, newIndices, newChildren); 366 newIndices = null; 367 newChildren = null; 368 LOG.fine("fireTreeNodesChanged - end"); } 370 } 371 } 373 | Popular Tags |