KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > VisualizerChildren


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.explorer.view;
20
21 import org.openide.nodes.*;
22
23 import java.util.*;
24
25
26 /** List of Visualizers. This is holded by parent visualizer by a
27 * weak reference,
28 *
29 * @author Jaroslav Tulach
30 */

31 final class VisualizerChildren extends Object JavaDoc {
32     /** parent visualizer */
33     public final VisualizerNode parent;
34
35     /** list of all objects here (VisualizerNode) */
36 public final List<VisualizerNode> list = new ArrayList<VisualizerNode>();
37
38     /** Creates new VisualizerChildren.
39     * Can be called only from EventQueue.
40     */

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     /** Notification of children addded event. Modifies the list of nodes
53     * and fires info to all listeners.
54     */

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 JavaDoc[] 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             // change of state
88
this.parent.notifyVisualizerChildrenChange(list.size(), this);
89         }
90     }
91
92     private static boolean sameContains(List l, Object JavaDoc 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     /** Notification that children has been removed. Modifies the list of nodes
103     * and fires info to all listeners.
104     */

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             // take visualizer node
118
vis = (VisualizerNode) it.next();
119
120             // check if it will removed
121
if (sameContains(remList, vis.node)) {
122                 indx[remSize++] = count;
123
124                 // remove this VisualizerNode from children
125
it.remove();
126
127                 // bugfix #36389, add the removed node to VisualizerEvent
128
ev.removed.add(vis);
129             }
130
131             count++;
132         }
133
134         // notify event about changed indexes
135
ev.setRemovedIndicies(indx);
136
137         VisualizerNode parent = this.parent;
138
139         while (parent != null) {
140             Object JavaDoc[] 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             // now is empty
151
this.parent.notifyVisualizerChildrenChange(0, this);
152         }
153     }
154
155     /**
156      * Issue 37802, sort the actual list of children with the comparator,
157      * rather than expecting it to match the current children of the node,
158      * which may be in an inconsistent state.
159      */

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     /** Notification that children has been reordered. Modifies the list of nodes
177     * and fires info to all listeners.
178     */

179     public void reordered(VisualizerEvent.Reordered ev) {
180         if (ev.getComparator() != null) {
181             //#37802
182
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                     // arr[indxs[i]] = old[i];
193
VisualizerNode old_i = old[i];
194                     int indxs_i = indxs[i];
195
196                     if (arr[indxs_i] != null) {
197                         // this is bad <-- we are rewriting some old value --> there will remain some null somewhere
198
System.err.println("Writing to this index for the second time: " + indxs_i); // NOI18N
199
System.err.println("Length of indxs array: " + indxs.length); // NOI18N
200
System.err.println("Length of actual array: " + old.length); // NOI18N
201
System.err.println("Indices of reorder event:"); // NOI18N
202

203                         for (int j = 0; i < indxs.length; j++)
204                             System.err.println("\t" + indxs[j]); // NOI18N
205

206                         Thread.dumpStack();
207
208                         return;
209                     }
210
211                     arr[indxs_i] = old_i;
212                 }
213             } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
214                 e.printStackTrace();
215                 System.err.println("Length of actual array: " + old.length); // NOI18N
216
System.err.println("Indices of reorder event:"); // NOI18N
217

218                 for (int i = 0; i < indxs.length; i++)
219                     System.err.println("\t" + indxs[i]); // NOI18N
220

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 JavaDoc[] 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