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:
042: package org.netbeans.modules.viewmodel;
043:
044: import java.awt.event.ActionEvent;
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.HashSet;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Set;
051: import java.util.Vector;
052: import javax.swing.AbstractAction;
053: import javax.swing.Action;
054: import org.netbeans.junit.NbTestCase;
055: import org.netbeans.modules.viewmodel.TreeModelNode;
056: import org.netbeans.modules.viewmodel.TreeModelRoot;
057: import org.netbeans.modules.viewmodel.TreeTable;
058: import org.netbeans.spi.viewmodel.*;
059: import org.openide.nodes.Node;
060: import org.openide.util.RequestProcessor;
061:
062: /**
063: * Tests the JPDABreakpointEvent.resume() functionality.
064: *
065: * @author Maros Sandor, Jan Jancura
066: */
067: public class BasicTest extends NbTestCase {
068:
069: private String helpID = "A test help ID"; // NOI18N
070:
071: public BasicTest(String s) {
072: super (s);
073: }
074:
075: public void testBasic() throws Exception {
076: ArrayList l = new ArrayList();
077: CompoundModel cm = new CompoundModel();
078: l.add(cm);
079: Models.CompoundModel mcm = Models
080: .createCompoundModel(l, helpID);
081: TreeTable tt = (TreeTable) Models.createView(mcm);
082: waitFinished();
083: Node n = tt.getExplorerManager().getRootContext();
084: checkNode(n, "");
085: if (cm.exception != null)
086: cm.exception.printStackTrace();
087: assertNull("Threading problem", cm.exception);
088: // TODO: Expansion test does not work - probably written in a bad way...
089: //assertEquals ("nodeExpanded notification number", 3, cm.expandedTest.size ());
090: //assertEquals ("nodeExpanded ", cm.toBeExpandedTest, cm.expandedTest);
091: assertEquals(n.getValue("propertiesHelpID"), helpID);
092: }
093:
094: private void checkNode(Node n, String name) {
095: // init
096: //assertEquals (null, n.getShortDescription ());
097: Node[] ns = n.getChildren().getNodes();
098: waitFinished();
099:
100: ns = n.getChildren().getNodes();
101: if (name.length() < 4) {
102: assertEquals(name, 3, ns.length);
103: checkNode(ns[0], name + "a");
104: checkNode(ns[1], name + "b");
105: checkNode(ns[2], name + "c");
106: } else
107: assertEquals(ns.length, 0);
108:
109: if (name.length() > 0) {
110: //assertEquals (name, n.getName ());
111: n.getDisplayName();
112: String sd = n.getShortDescription();
113: n.getActions(false);
114: waitFinished();
115: assertEquals(name, n.getDisplayName());
116: assertEquals(name + "WWW", sd);
117: assertEquals(1, n.getActions(false).length);
118: }
119: }
120:
121: static void waitFinished() {
122: TreeModelNode.getRequestProcessor().post(new Runnable() {
123: public void run() {
124: }
125: }).waitFinished();
126: }
127:
128: public void testMnemonics() throws Exception {
129: ArrayList l = new ArrayList();
130: CompoundModel cm = new CompoundModel();
131: l.add(cm);
132: TestColumnModel tcm = new TestColumnModel();
133: l.add(tcm);
134: Models.CompoundModel mcm = Models.createCompoundModel(l);
135: TreeTable tt = (TreeTable) Models.createView(mcm);
136: Node.Property[] columns = tt.columns;
137: assertEquals(2, columns.length);
138: assertEquals(new Character('e'), columns[1]
139: .getValue("ColumnMnemonicCharTTV"));
140: }
141:
142: public static class CompoundModel implements TreeModel, NodeModel,
143: NodeActionsProvider, TableModel, TreeExpansionModel {
144:
145: private Vector listeners = new Vector();
146:
147: private Throwable exception;
148:
149: private Map callNumbers = new HashMap();
150:
151: protected synchronized void addCall(String methodName,
152: Object node) {
153: Map m = (Map) callNumbers.get(methodName);
154: if (m == null)
155: callNumbers.put(methodName, m = new HashMap());
156: if (m.containsKey(node)) {
157: Object info = m.get(node);
158: if (info instanceof Exception) {
159: System.err.println("Second call of " + methodName
160: + " method for the same node " + node);
161: System.err.println("First was at:");
162: ((Exception) info).printStackTrace();
163: System.err.println("Second is:");
164: Thread.dumpStack();
165: m.put(node, new Integer(2));
166: } else {
167: int numCalls = ((Integer) info).intValue() + 1;
168: System.err.println(numCalls + ". call of "
169: + methodName + " method for the same node "
170: + node);
171: Thread.dumpStack();
172: m.put(node, new Integer(numCalls));
173: }
174: } else {
175: m.put(node, new Exception());
176: }
177: }
178:
179: void checkThread() {
180: try {
181: assertTrue("The right thread", TreeModelNode
182: .getRequestProcessor()
183: .isRequestProcessorThread());
184: } catch (Throwable t) {
185: exception = t;
186: }
187: /*;
188: Thread t = Thread.currentThread ();
189: if ( t.getClass ().getName ().startsWith
190: (RequestProcessor.class.getName ())
191: ) exception = new Exception ();
192: */
193: }
194:
195: // TreeModel ...............................................................
196:
197: /**
198: * Returns the root node of the tree or null, if the tree is empty.
199: *
200: * @return the root node of the tree or null
201: */
202: public Object getRoot() {
203: addCall("getRoot", null);
204: return ROOT;
205: }
206:
207: /**
208: * Returns children for given parent on given indexes.
209: *
210: * @param parent a parent of returned nodes
211: * @throws UnknownTypeException if this TreeModel implementation is not
212: * able to resolve dchildren for given node type
213: *
214: * @return children for given parent on given indexes
215: */
216: public Object[] getChildren(Object parent, int from, int to)
217: throws UnknownTypeException {
218: addCall("getChildren", parent);
219: if (parent == ROOT)
220: return new Object[] { "a", "b", "c" };
221: if (parent instanceof String)
222: return new Object[] { parent + "a", parent + "b",
223: parent + "c" };
224: throw new UnknownTypeException(parent);
225: }
226:
227: /**
228: * Returns number of children for given node.
229: *
230: * @param node the parent node
231: * @throws UnknownTypeException if this TreeModel implementation is not
232: * able to resolve children for given node type
233: *
234: * @return true if node is leaf
235: */
236: public int getChildrenCount(Object node)
237: throws UnknownTypeException {
238: addCall("getChildrenCount", node);
239: if (node == ROOT)
240: return 3;
241: if (node instanceof String)
242: return 3;
243: throw new UnknownTypeException(node);
244: }
245:
246: /**
247: * Returns true if node is leaf.
248: *
249: * @throws UnknownTypeException if this TreeModel implementation is not
250: * able to resolve dchildren for given node type
251: * @return true if node is leaf
252: */
253: public boolean isLeaf(Object node) throws UnknownTypeException {
254: addCall("isLeaf", node);
255: if (node == ROOT)
256: return false;
257: if (node instanceof String)
258: return ((String) node).length() > 3;
259: throw new UnknownTypeException(node);
260: }
261:
262: // NodeModel ...........................................................
263:
264: /**
265: * Returns display name for given node.
266: *
267: * @throws UnknownTypeException if this NodeModel implementation is not
268: * able to resolve display name for given node type
269: * @return display name for given node
270: */
271: public String getDisplayName(Object node)
272: throws UnknownTypeException {
273: addCall("getDisplayName", node);
274: //checkThread ();
275: if (node instanceof String)
276: return (String) node;
277: throw new UnknownTypeException(node);
278: }
279:
280: /**
281: * Returns tooltip for given node.
282: *
283: * @throws UnknownTypeException if this NodeModel implementation is not
284: * able to resolve tooltip for given node type
285: * @return tooltip for given node
286: */
287: public String getShortDescription(Object node)
288: throws UnknownTypeException {
289: addCall("getShortDescription", node);
290: //checkThread (); Short description is called on AWT! How else we could display a tooltip?
291: if (node == ROOT)
292: return "";
293: if (node instanceof String)
294: return node + "WWW";
295: throw new UnknownTypeException(node);
296: }
297:
298: /**
299: * Returns icon for given node.
300: *
301: * @throws UnknownTypeException if this NodeModel implementation is not
302: * able to resolve icon for given node type
303: * @return icon for given node
304: */
305: public String getIconBase(Object node)
306: throws UnknownTypeException {
307: addCall("getIconBase", node);
308: //checkThread ();
309: if (node instanceof String)
310: return node + "XXX";
311: throw new UnknownTypeException(node);
312: }
313:
314: // NodeActionsProvider .....................................................
315:
316: /**
317: * Performs default action for given node.
318: *
319: * @throws UnknownTypeException if this NodeActionsProvider implementation
320: * is not able to resolve actions for given node type
321: * @return display name for given node
322: */
323: public void performDefaultAction(Object node)
324: throws UnknownTypeException {
325: }
326:
327: /**
328: * Returns set of actions for given node.
329: *
330: * @throws UnknownTypeException if this NodeActionsProvider implementation
331: * is not able to resolve actions for given node type
332: * @return display name for given node
333: */
334: public Action[] getActions(Object node)
335: throws UnknownTypeException {
336: //checkThread ();
337: if (node == ROOT)
338: return new Action[0];
339: if (node instanceof String)
340: return new Action[] { new AbstractAction((String) node) {
341: public void actionPerformed(ActionEvent ev) {
342:
343: }
344: }, };
345: throw new UnknownTypeException(node);
346: }
347:
348: // ColumnsModel ............................................................
349:
350: /**
351: * Returns sorted array of
352: * {@link org.netbeans.spi.viewmodel.ColumnModel}s.
353: *
354: * @return sorted array of ColumnModels
355: */
356: public ColumnModel[] getColumns() {
357: return new ColumnModel[0];
358: }
359:
360: // TableModel ..............................................................
361:
362: public Object getValueAt(Object node, String columnID)
363: throws UnknownTypeException {
364: addCall("getValueAt", node);
365: checkThread();
366: if (node instanceof String) {
367: if (columnID.equals("1"))
368: return node + "1";
369: if (columnID.equals("2"))
370: return node + "2";
371: }
372: throw new UnknownTypeException(node);
373: }
374:
375: public boolean isReadOnly(Object node, String columnID)
376: throws UnknownTypeException {
377: addCall("isReadOnly", node);
378: checkThread();
379: if (node instanceof String) {
380: if (columnID.equals("1"))
381: return true;
382: if (columnID.equals("2"))
383: return true;
384: }
385: throw new UnknownTypeException(node);
386: }
387:
388: public void setValueAt(Object node, String columnID,
389: Object value) throws UnknownTypeException {
390: throw new UnknownTypeException(node);
391: }
392:
393: // TreeExpansionModel ......................................................
394:
395: private Set toBeExpandedTest = new HashSet();
396: private Set expandedTest = new HashSet();
397: {
398: toBeExpandedTest.add("a");
399: toBeExpandedTest.add("ab");
400: toBeExpandedTest.add("abc");
401: }
402:
403: /**
404: * Defines default state (collapsed, expanded) of given node.
405: *
406: * @param node a node
407: * @return default state (collapsed, expanded) of given node
408: */
409: public boolean isExpanded(Object node)
410: throws UnknownTypeException {
411: if (node instanceof String)
412: return toBeExpandedTest.contains(node);
413: throw new UnknownTypeException(node);
414: }
415:
416: /**
417: * Called when given node is expanded.
418: *
419: * @param node a expanded node
420: */
421: public void nodeExpanded(Object node) {
422: if (!toBeExpandedTest.contains(node)) {
423: System.err.println("This node should not be expanded: "
424: + node);
425: Thread.dumpStack();
426: }
427: expandedTest.add(node);
428: }
429:
430: /**
431: * Called when given node is collapsed.
432: *
433: * @param node a collapsed node
434: */
435: public void nodeCollapsed(Object node) {
436: System.err.println("nodeCollapsed " + node);
437: Thread.dumpStack();
438: }
439:
440: // listeners ...............................................................
441:
442: /**
443: * Registers given listener.
444: *
445: * @param l the listener to add
446: */
447: public void addModelListener(ModelListener l) {
448: listeners.add(l);
449: }
450:
451: /**
452: * Unregisters given listener.
453: *
454: * @param l the listener to remove
455: */
456: public void removeModelListener(ModelListener l) {
457: listeners.remove(l);
458: }
459:
460: public void fire() {
461: Vector v = (Vector) listeners.clone();
462: int i, k = v.size();
463: for (i = 0; i < k; i++)
464: ((ModelListener) v.get(i)).modelChanged(null);
465: }
466:
467: public void fire(ModelEvent event) {
468: Vector v = (Vector) listeners.clone();
469: int i, k = v.size();
470: for (i = 0; i < k; i++) {
471: ((ModelListener) v.get(i)).modelChanged(event);
472: }
473: }
474: }
475:
476: private static class TestColumnModel extends ColumnModel {
477: public Class getType() {
478: return String.class;
479: }
480:
481: public String getDisplayName() {
482: return "Test";
483: }
484:
485: public Character getDisplayedMnemonic() {
486: return new Character('e');
487: }
488:
489: public String getID() {
490: return "xx";
491: }
492:
493: }
494: }
|