Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.modules.junit.output; |
41 |
|
42 |
import java.util.BitSet; |
43 |
import org.openide.explorer.view.TreeView; |
44 |
import org.openide.nodes.Children; |
45 |
import org.openide.nodes.Node; |
46 |
import static java.lang.Boolean.FALSE; |
47 |
import static java.lang.Boolean.TRUE; |
48 |
|
49 |
/** |
50 |
* Utility class containing methods for storage and recovery of nodes' |
51 |
* expansion states (collapsed/expanded). |
52 |
* |
53 |
* @author Marian Petras |
54 |
*/ |
55 |
final class NodeStateUtilities { |
56 |
|
57 |
private NodeStateUtilities() { } |
58 |
|
59 |
/** |
60 |
*/ |
61 |
static Object getExpansionState(final Node node, |
62 |
final TreeView treeView) { |
63 |
if (!treeView.isExpanded(node)) { |
64 |
return FALSE; |
65 |
} |
66 |
|
67 |
return getExpansionStates(node.getChildren(), treeView); |
68 |
} |
69 |
|
70 |
/** |
71 |
*/ |
72 |
static void restoreExpansionState(final Node node, |
73 |
final Object stateInfo, |
74 |
final TreeView treeView) { |
75 |
if (stateInfo == FALSE) { |
76 |
//we will not be collapsing nodes expanded by the user |
77 |
} else if (stateInfo != null) { |
78 |
treeView.expandNode(node); |
79 |
if (stateInfo != TRUE) { |
80 |
restoreExpansionStates(node.getChildren(), stateInfo, treeView); |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
/** |
86 |
*/ |
87 |
static Object getExpansionStates(final Children children, |
88 |
final TreeView treeView) { |
89 |
if (children == Children.LEAF) { |
90 |
return FALSE; |
91 |
} |
92 |
|
93 |
return getExpansionStates(children.getNodes(), treeView); |
94 |
} |
95 |
|
96 |
/** |
97 |
*/ |
98 |
static void restoreExpansionStates(final Children children, |
99 |
final Object statesInfo, |
100 |
final TreeView treeView) { |
101 |
if (children == Children.LEAF) { |
102 |
return; |
103 |
} |
104 |
|
105 |
restoreExpansionStates(children.getNodes(true), statesInfo, treeView); |
106 |
} |
107 |
|
108 |
/** |
109 |
*/ |
110 |
static Object getExpansionStates(final Node[] nodes, |
111 |
final TreeView treeView) { |
112 |
if (nodes.length == 0) { |
113 |
return TRUE; |
114 |
} |
115 |
|
116 |
BitSet stateSet = null; |
117 |
Object[] stateArray = null; |
118 |
for (int i = 0; i < nodes.length; i++) { |
119 |
Object expansionState = getExpansionState(nodes[i], treeView); |
120 |
if (stateArray == null) { |
121 |
if (expansionState == TRUE) { |
122 |
if (stateSet == null) { |
123 |
stateSet = new BitSet(nodes.length); |
124 |
} |
125 |
stateSet.set(i); |
126 |
continue; |
127 |
} |
128 |
|
129 |
if (expansionState == FALSE) { |
130 |
//stateSet.clear(i); //it is the default value |
131 |
continue; |
132 |
} |
133 |
|
134 |
/* |
135 |
* Oops! Something else then TRUE or FALSE! |
136 |
* We cannot store such a value in a bitset. |
137 |
* So convert the data collected so far to another format |
138 |
* - to an array of objects: |
139 |
*/ |
140 |
stateArray = new Object[nodes.length]; |
141 |
for (int j = 0; j < i; j++) { |
142 |
stateArray[j] = stateSet.get(j) ? TRUE : FALSE; |
143 |
} |
144 |
stateSet = null; |
145 |
} |
146 |
stateArray[i] = expansionState; |
147 |
} |
148 |
assert (stateSet == null) || (stateArray == null); //at most one is used |
149 |
|
150 |
if ((stateSet == null) && (stateArray == null)) { |
151 |
/* no node expanded */ |
152 |
return TRUE; |
153 |
} |
154 |
|
155 |
return (stateSet != null) ? stateSet : stateArray; |
156 |
} |
157 |
|
158 |
/** |
159 |
*/ |
160 |
static void restoreExpansionStates(final Node[] nodes, |
161 |
final Object statesInfo, |
162 |
final TreeView treeView) { |
163 |
if (statesInfo == null) { |
164 |
return; |
165 |
} |
166 |
|
167 |
final Class infoClass = statesInfo.getClass(); |
168 |
if (infoClass == Boolean.class) { |
169 |
assert ((Boolean) statesInfo).booleanValue(); //always TRUE |
170 |
//nothing to do (nothing to expand) |
171 |
} else if (infoClass == BitSet.class) { |
172 |
BitSet stateSet = (BitSet) statesInfo; |
173 |
int length = Math.min(stateSet.size(), nodes.length); |
174 |
for (int i = stateSet.nextSetBit(0); |
175 |
(i < length) && (i != -1); |
176 |
i = stateSet.nextSetBit(i + 1)) { |
177 |
treeView.expandNode(nodes[i]); |
178 |
} |
179 |
} else { |
180 |
assert infoClass.isArray(); |
181 |
Object[] states = (Object[]) statesInfo; |
182 |
int length = Math.min(nodes.length, states.length); |
183 |
for (int i = 0; i < length; i++) { |
184 |
restoreExpansionState(nodes[i], states[i], treeView); |
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
} |