001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.nodes;
042:
043: import java.util.Arrays;
044: import java.util.HashSet;
045:
046: /** Event describing change in the list of a node's children.
047: *
048: * @author Jaroslav Tulach
049: */
050: public class NodeMemberEvent extends NodeEvent {
051: static final long serialVersionUID = -3973509253579305102L;
052:
053: /** is this add event? */
054: private boolean add;
055:
056: /** list of changed nodes */
057: private Node[] delta;
058:
059: /** list of nodes to find indices in, null if one should use current
060: * node's list
061: */
062: private Node[] from;
063:
064: /** list of nodes indexes, can be null if it should be computed lazily */
065: private int[] indices;
066:
067: /** Package private constructor to allow construction only
068: * @param n node that should fire change
069: * @param add true if nodes has been added
070: * @param delta array of nodes that have changed
071: * @param from nodes to find indices in
072: */
073: NodeMemberEvent(Node n, boolean add, Node[] delta, Node[] from) {
074: super (n);
075: this .add = add;
076: this .delta = delta;
077: this .from = from;
078: }
079:
080: /** Get the type of action.
081: * @return <CODE>true</CODE> if children were added,
082: * <CODE>false</CODE> if removed
083: */
084: public final boolean isAddEvent() {
085: return add;
086: }
087:
088: /** Get a list of children that changed.
089: * @return array of nodes that changed
090: */
091: public final Node[] getDelta() {
092: return delta;
093: }
094:
095: /** Get an array of indices of the changed nodes.
096: * @return array with the same length as {@link #getDelta}
097: */
098: public synchronized int[] getDeltaIndices() {
099: if (indices != null) {
100: return indices;
101: }
102:
103: // compute indices
104: if (from == null) {
105: // use current node subnodes
106: from = getNode().getChildren().getNodes();
107: }
108:
109: java.util.List<Node> list = Arrays.asList(delta);
110: HashSet<Node> set = new HashSet<Node>(list);
111:
112: indices = new int[delta.length];
113:
114: int j = 0;
115: int i = 0;
116:
117: while ((i < from.length) && (j < indices.length)) {
118: if (set.contains(from[i])) {
119: indices[j++] = i;
120: }
121:
122: i++;
123: }
124:
125: if (j != delta.length) {
126: StringBuilder m = new StringBuilder(1000);
127: m
128: .append("Some of a set of deleted nodes are not present in the original ones.\n"); // NOI18N
129: m
130: .append("See #15478; you may need to check that your Children.Keys keys are safely comparable."); // NOI18N
131: m.append("\ni: ").append(i); // NOI18N
132: m.append("\nj: ").append(j); // NOI18N
133: m.append("\nThis: ").append(this ); // NOI18N
134: m.append("\nCurrent state:\n"); // NOI18N
135: m.append(Arrays.asList(from));
136: m.append("\nDelta:\n"); // NOI18N
137: m.append(list);
138: throw new IllegalStateException(m.toString());
139: }
140:
141: return indices;
142: }
143:
144: /** Human presentable information about the event */
145: public String toString() {
146: StringBuilder sb = new StringBuilder();
147: sb.append(getClass().getName());
148: sb.append("[node="); // NOI18N
149: sb.append(getSource());
150: sb.append(", add="); // NOI18N
151: sb.append(isAddEvent());
152:
153: Node[] deltaNodes = getDelta();
154: int[] deltaIndices = getDeltaIndices();
155:
156: for (int i = 0; i < deltaNodes.length; i++) {
157: sb.append("\n "); // NOI18N
158: sb.append(i);
159: sb.append(" at "); // NOI18N
160: sb.append(deltaIndices[i]);
161: sb.append(" = "); // NOI18N
162: sb.append(deltaNodes[i]);
163: }
164:
165: sb.append("\n]"); // NOI18N
166:
167: return sb.toString();
168: }
169: }
|