Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2009 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 2009 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.modules.viewmodel; |
41 |
|
42 |
import java.util.ArrayList; |
43 |
import java.util.Arrays; |
44 |
import org.netbeans.junit.NbTestCase; |
45 |
import org.netbeans.spi.viewmodel.Model; |
46 |
import org.netbeans.spi.viewmodel.ModelEvent; |
47 |
import org.netbeans.spi.viewmodel.ModelListener; |
48 |
import org.netbeans.spi.viewmodel.Models; |
49 |
import org.netbeans.spi.viewmodel.NodeModel; |
50 |
import org.netbeans.spi.viewmodel.ReorderableTreeModel; |
51 |
import org.netbeans.spi.viewmodel.TreeModel; |
52 |
import org.netbeans.spi.viewmodel.UnknownTypeException; |
53 |
import org.openide.nodes.Index; |
54 |
import org.openide.nodes.Node; |
55 |
import org.openide.util.RequestProcessor; |
56 |
|
57 |
/** |
58 |
* Tests that reorderable model produces node with Index implementation |
59 |
* |
60 |
* @author Martin Entlicher |
61 |
*/ |
62 |
public class ReorderableTreeModelTest extends NbTestCase { |
63 |
|
64 |
private Node root; |
65 |
|
66 |
public ReorderableTreeModelTest (String s) { |
67 |
super (s); |
68 |
} |
69 |
|
70 |
private void setUpModel() { |
71 |
Model m = new ReorderableModelImpl(); |
72 |
|
73 |
ArrayList l = new ArrayList (); |
74 |
l.add(m); |
75 |
Models.CompoundModel mcm = Models.createCompoundModel(l); |
76 |
OutlineTable tt = BasicTest.createView(mcm); |
77 |
|
78 |
RequestProcessor rp = tt.currentTreeModelRoot.getRootNode().getRequestProcessor(); |
79 |
BasicTest.waitFinished (rp); |
80 |
|
81 |
root = tt.getExplorerManager ().getRootContext (); |
82 |
} |
83 |
|
84 |
public void testChildrenReorder() { |
85 |
setUpModel(); |
86 |
|
87 |
Index indexImpl = root.getLookup().lookup(Index.class); |
88 |
assertNull("Root must not provide Index, root is not reordeable!", indexImpl); |
89 |
|
90 |
Node[] ch1 = root.getChildren().getNodes(); |
91 |
assertEquals(2, ch1.length); |
92 |
|
93 |
indexImpl = ch1[1].getLookup().lookup(Index.class); |
94 |
assertNull(ch1[1]+ " must not provide Index, it is not reordeable!", indexImpl); |
95 |
indexImpl = ch1[0].getLookup().lookup(Index.class); |
96 |
assertNotNull(ch1[0]+ " must provide Index, it is reordeable!", indexImpl); |
97 |
|
98 |
indexImpl.reorder(new int[] { 2, 0, 1, 4, 3 }); |
99 |
// => "3", "1", "2", "5", "4" |
100 |
String[] reorderedNames = new String[] { "2", "3", "1", "5", "4" }; |
101 |
Node[] reorderedNodes = ch1[0].getChildren().getNodes(); |
102 |
assertEquals(reorderedNames.length, reorderedNodes.length); |
103 |
String nodes = "Nodes = "+Arrays.toString(reorderedNodes); |
104 |
for (int i = 0; i < reorderedNodes.length; i++) { |
105 |
assertEquals(nodes, reorderedNames[i], reorderedNodes[i].getDisplayName()); |
106 |
} |
107 |
} |
108 |
|
109 |
private static final class ReorderableModelImpl implements ReorderableTreeModel, NodeModel { |
110 |
|
111 |
private static final String CAN_REORDER = "canReorder"; |
112 |
private static final String CAN_NOT_REORDER = "canNotReorder"; |
113 |
|
114 |
private final String[] childrenReordered = new String[] { "1", "2", "3", "4", "5" }; |
115 |
|
116 |
public boolean canReorder(Object parent) throws UnknownTypeException { |
117 |
return /*ROOT == parent ||*/ CAN_REORDER.equals(parent); |
118 |
} |
119 |
|
120 |
public void reorder(Object parent, int[] perm) throws UnknownTypeException { |
121 |
//System.err.println("reorder("+parent+", "+Arrays.toString(perm)+")"); |
122 |
//Thread.dumpStack(); |
123 |
if (!(/*ROOT == parent ||*/ CAN_REORDER.equals(parent))) { |
124 |
throw new IllegalStateException("reorder called on "+parent); |
125 |
} |
126 |
if (perm.length != childrenReordered.length) { |
127 |
throw new IllegalArgumentException("Permutation of length "+perm.length+", but have "+childrenReordered.length+" children."); |
128 |
} |
129 |
checkPermutation(perm); |
130 |
String[] ch = childrenReordered.clone(); |
131 |
for (int i = 0; i < ch.length; i++) { |
132 |
//System.err.println("ch["+perm[i]+"] = "+ch[i]); |
133 |
childrenReordered[perm[i]] = ch[i]; |
134 |
} |
135 |
} |
136 |
|
137 |
private static void checkPermutation(int[] permutation) throws IllegalArgumentException { |
138 |
int max = permutation.length; |
139 |
int[] check = new int[max]; |
140 |
for (int i = 0; i < max; i++) { |
141 |
int p = permutation[i]; |
142 |
if (p >= max) { |
143 |
throw new IllegalArgumentException("Permutation "+Arrays.toString(permutation)+" is not a valid permutation, it contains element "+p+", which is bigger than the length of the permutation."); |
144 |
} |
145 |
if (p < 0) { |
146 |
throw new IllegalArgumentException("Permutation "+Arrays.toString(permutation)+" is not a valid permutation, it contains element "+p+", which is negative."); |
147 |
} |
148 |
if (check[p] != 0) { |
149 |
throw new IllegalArgumentException("Permutation "+Arrays.toString(permutation)+" is not a valid permutation, it contains element "+p+" twice or more times."); |
150 |
} |
151 |
check[p] = 1; |
152 |
} |
153 |
} |
154 |
|
155 |
public Object getRoot() { |
156 |
return ROOT; |
157 |
} |
158 |
|
159 |
public Object[] getChildren(Object parent, int from, int to) throws UnknownTypeException { |
160 |
if (ROOT == parent) { |
161 |
return new String[] { CAN_REORDER, CAN_NOT_REORDER }; |
162 |
} else if (CAN_REORDER.equals(parent)) { |
163 |
return childrenReordered; |
164 |
} else if (CAN_NOT_REORDER.equals(parent)) { |
165 |
return new String[] { "a", "b", "c", "d", "e" }; |
166 |
} else { |
167 |
return new Object[] {}; |
168 |
} |
169 |
} |
170 |
|
171 |
public boolean isLeaf(Object node) throws UnknownTypeException { |
172 |
return ((String) node).length() == 1; |
173 |
} |
174 |
|
175 |
public int getChildrenCount(Object node) throws UnknownTypeException { |
176 |
return Integer.MAX_VALUE; |
177 |
} |
178 |
|
179 |
public void addModelListener(ModelListener l) { |
180 |
} |
181 |
|
182 |
public void removeModelListener(ModelListener l) { |
183 |
} |
184 |
|
185 |
public String getDisplayName(Object node) throws UnknownTypeException { |
186 |
return (String) node; |
187 |
} |
188 |
|
189 |
public String getIconBase(Object node) throws UnknownTypeException { |
190 |
return null; |
191 |
} |
192 |
|
193 |
public String getShortDescription(Object node) throws UnknownTypeException { |
194 |
return node+" of "+getClass().getSimpleName(); |
195 |
} |
196 |
|
197 |
} |
198 |
|
199 |
} |