1 19 package org.openide.explorer.view; 20 21 import org.openide.nodes.*; 22 23 import java.util.*; 24 25 26 31 final class VisualizerChildren extends Object { 32 33 public final VisualizerNode parent; 34 35 36 public final List<VisualizerNode> list = new ArrayList<VisualizerNode>(); 37 38 41 public VisualizerChildren(VisualizerNode parent, Node[] nodes) { 42 this.parent = parent; 43 44 int s = nodes.length; 45 46 for (int i = 0; i < s; i++) { 47 VisualizerNode v = VisualizerNode.getVisualizer(this, nodes[i]); 48 list.add(v); 49 } 50 } 51 52 55 public void added(VisualizerEvent.Added ev) { 56 ListIterator<VisualizerNode> it = list.listIterator(); 57 boolean empty = !it.hasNext(); 58 59 int[] indxs = ev.getArray(); 60 Node[] nodes = ev.getAdded(); 61 62 int current = 0; 63 int inIndxs = 0; 64 65 while (inIndxs < indxs.length) { 66 while (current++ < indxs[inIndxs]) { 67 it.next(); 68 } 69 70 it.add(VisualizerNode.getVisualizer(this, nodes[inIndxs])); 71 inIndxs++; 72 } 73 74 VisualizerNode parent = this.parent; 75 76 while (parent != null) { 77 Object [] listeners = parent.getListenerList(); 78 79 for (int i = listeners.length - 1; i >= 0; i -= 2) { 80 ((NodeModel) listeners[i]).added(ev); 81 } 82 83 parent = (VisualizerNode) parent.getParent(); 84 } 85 86 if (empty) { 87 this.parent.notifyVisualizerChildrenChange(list.size(), this); 89 } 90 } 91 92 private static boolean sameContains(List l, Object elem) { 93 for (Iterator it = l.iterator(); it.hasNext();) { 94 if (it.next() == elem) { 95 return true; 96 } 97 } 98 99 return false; 100 } 101 102 105 public void removed(VisualizerEvent.Removed ev) { 106 List remList = Arrays.asList(ev.getRemovedNodes()); 107 108 Iterator it = list.iterator(); 109 110 VisualizerNode vis; 111 112 int[] indx = new int[remList.size()]; 113 int count = 0; 114 int remSize = 0; 115 116 while (it.hasNext()) { 117 vis = (VisualizerNode) it.next(); 119 120 if (sameContains(remList, vis.node)) { 122 indx[remSize++] = count; 123 124 it.remove(); 126 127 ev.removed.add(vis); 129 } 130 131 count++; 132 } 133 134 ev.setRemovedIndicies(indx); 136 137 VisualizerNode parent = this.parent; 138 139 while (parent != null) { 140 Object [] listeners = parent.getListenerList(); 141 142 for (int i = listeners.length - 1; i >= 0; i -= 2) { 143 ((NodeModel) listeners[i]).removed(ev); 144 } 145 146 parent = (VisualizerNode) parent.getParent(); 147 } 148 149 if (list.isEmpty()) { 150 this.parent.notifyVisualizerChildrenChange(0, this); 152 } 153 } 154 155 160 private int[] reorderByComparator(Comparator<VisualizerNode> c) { 161 VisualizerNode[] old = list.toArray(new VisualizerNode[list.size()]); 162 Arrays.sort(old, c); 163 164 int[] idxs = new int[old.length]; 165 166 for (int i = 0; i < idxs.length; i++) { 167 idxs[i] = list.indexOf(old[i]); 168 } 169 170 list.clear(); 171 list.addAll(Arrays.asList(old)); 172 173 return idxs; 174 } 175 176 179 public void reordered(VisualizerEvent.Reordered ev) { 180 if (ev.getComparator() != null) { 181 ev.array = reorderByComparator(ev.getComparator()); 183 } else { 184 int[] indxs = ev.getArray(); 185 VisualizerNode[] old = list.toArray(new VisualizerNode[list.size()]); 186 VisualizerNode[] arr = new VisualizerNode[old.length]; 187 188 int s = indxs.length; 189 190 try { 191 for (int i = 0; i < s; i++) { 192 VisualizerNode old_i = old[i]; 194 int indxs_i = indxs[i]; 195 196 if (arr[indxs_i] != null) { 197 System.err.println("Writing to this index for the second time: " + indxs_i); System.err.println("Length of indxs array: " + indxs.length); System.err.println("Length of actual array: " + old.length); System.err.println("Indices of reorder event:"); 203 for (int j = 0; i < indxs.length; j++) 204 System.err.println("\t" + indxs[j]); 206 Thread.dumpStack(); 207 208 return; 209 } 210 211 arr[indxs_i] = old_i; 212 } 213 } catch (ArrayIndexOutOfBoundsException e) { 214 e.printStackTrace(); 215 System.err.println("Length of actual array: " + old.length); System.err.println("Indices of reorder event:"); 218 for (int i = 0; i < indxs.length; i++) 219 System.err.println("\t" + indxs[i]); 221 return; 222 } 223 224 assert !Arrays.asList(arr).contains(null) : "Null element in reorderer list " + Arrays.asList(arr) + 225 "; list=" + list + " indxs=" + Arrays.asList(org.openide.util.Utilities.toObjectArray(indxs)); 226 list.clear(); 227 list.addAll(Arrays.asList(arr)); 228 assert !list.contains(null); 229 } 230 231 VisualizerNode parent = this.parent; 232 233 while (parent != null) { 234 Object [] listeners = parent.getListenerList(); 235 236 for (int i = listeners.length - 1; i >= 0; i -= 2) { 237 ((NodeModel) listeners[i]).reordered(ev); 238 } 239 240 parent = (VisualizerNode) parent.getParent(); 241 } 242 } 243 } 244 | Popular Tags |