KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > NodeMemberEvent


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.nodes;
20
21 import java.util.Arrays JavaDoc;
22 import java.util.HashSet JavaDoc;
23
24
25 /** Event describing change in the list of a node's children.
26 *
27 * @author Jaroslav Tulach
28 */

29 public class NodeMemberEvent extends NodeEvent {
30     static final long serialVersionUID = -3973509253579305102L;
31
32     /** is this add event? */
33     private boolean add;
34
35     /** list of changed nodes */
36     private Node[] delta;
37
38     /** list of nodes to find indices in, null if one should use current
39     * node's list
40     */

41     private Node[] from;
42
43     /** list of nodes indexes, can be null if it should be computed lazily */
44     private int[] indices;
45
46     /** Package private constructor to allow construction only
47     * @param n node that should fire change
48     * @param add true if nodes has been added
49     * @param delta array of nodes that have changed
50     * @param from nodes to find indices in
51     */

52     NodeMemberEvent(Node n, boolean add, Node[] delta, Node[] from) {
53         super(n);
54         this.add = add;
55         this.delta = delta;
56         this.from = from;
57     }
58
59     /** Get the type of action.
60     * @return <CODE>true</CODE> if children were added,
61     * <CODE>false</CODE> if removed
62     */

63     public final boolean isAddEvent() {
64         return add;
65     }
66
67     /** Get a list of children that changed.
68     * @return array of nodes that changed
69     */

70     public final Node[] getDelta() {
71         return delta;
72     }
73
74     /** Get an array of indices of the changed nodes.
75     * @return array with the same length as {@link #getDelta}
76     */

77     public synchronized int[] getDeltaIndices() {
78         if (indices != null) {
79             return indices;
80         }
81
82         // compute indices
83
if (from == null) {
84             // use current node subnodes
85
from = getNode().getChildren().getNodes();
86         }
87
88         java.util.List JavaDoc<Node> list = Arrays.asList(delta);
89         HashSet JavaDoc<Node> set = new HashSet JavaDoc<Node>(list);
90
91         indices = new int[delta.length];
92
93         int j = 0;
94         int i = 0;
95
96         while ((i < from.length) && (j < indices.length)) {
97             if (set.contains(from[i])) {
98                 indices[j++] = i;
99             }
100
101             i++;
102         }
103
104         if (j != delta.length) {
105             StringBuilder JavaDoc m = new StringBuilder JavaDoc(1000);
106             m.append("Some of a set of deleted nodes are not present in the original ones.\n"); // NOI18N
107
m.append("See #15478; you may need to check that your Children.Keys keys are safely comparable."); // NOI18N
108
m.append("\ni: ").append(i); // NOI18N
109
m.append("\nj: ").append(j); // NOI18N
110
m.append("\nThis: ").append(this); // NOI18N
111
m.append("\nCurrent state:\n"); // NOI18N
112
m.append(Arrays.asList(from));
113             m.append("\nDelta:\n"); // NOI18N
114
m.append(list);
115             throw new IllegalStateException JavaDoc(m.toString());
116         }
117
118         return indices;
119     }
120
121     /** Human presentable information about the event */
122     public String JavaDoc toString() {
123         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
124         sb.append(getClass().getName());
125         sb.append("[node="); // NOI18N
126
sb.append(getSource());
127         sb.append(", add="); // NOI18N
128
sb.append(isAddEvent());
129
130         Node[] deltaNodes = getDelta();
131         int[] deltaIndices = getDeltaIndices();
132
133         for (int i = 0; i < deltaNodes.length; i++) {
134             sb.append("\n "); // NOI18N
135
sb.append(i);
136             sb.append(" at "); // NOI18N
137
sb.append(deltaIndices[i]);
138             sb.append(" = "); // NOI18N
139
sb.append(deltaNodes[i]);
140         }
141
142         sb.append("\n]"); // NOI18N
143

144         return sb.toString();
145     }
146 }
147
Popular Tags