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.lang.reflect.InvocationTargetException;
046: import java.util.ArrayList;
047: import java.util.List;
048: import java.util.Vector;
049: import javax.swing.AbstractAction;
050: import javax.swing.Action;
051: import javax.swing.SwingUtilities;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.modules.viewmodel.TreeModelNode;
054: import org.netbeans.modules.viewmodel.TreeModelRoot;
055: import org.netbeans.modules.viewmodel.TreeTable;
056: import org.netbeans.spi.viewmodel.*;
057: import org.openide.nodes.Node;
058: import org.openide.nodes.NodeListener;
059: import org.openide.util.RequestProcessor;
060:
061: /**
062: *
063: */
064: public class ModelEventTest extends NbTestCase implements NodeListener {
065:
066: BasicTest.CompoundModel cm;
067: Node n;
068: volatile Object event;
069: Vector propEvents = new Vector();
070:
071: public ModelEventTest(String s) {
072: super (s);
073: }
074:
075: protected void setUp() throws Exception {
076: super .setUp();
077: ArrayList l = new ArrayList();
078: cm = new CompoundModel1();
079: l.add(cm);
080: TreeTable tt = (TreeTable) Models.createView(Models
081: .createCompoundModel(l));
082: BasicTest.waitFinished();
083: n = tt.getExplorerManager().getRootContext();
084: n.addNodeListener(this );
085: }
086:
087: public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) {
088: assertNull("Already fired", event);
089: event = ev;
090: }
091:
092: public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) {
093: assertNull("Already fired", event);
094: event = ev;
095: }
096:
097: public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) {
098: assertNull("Already fired", event);
099: event = ev;
100: }
101:
102: public void nodeDestroyed(org.openide.nodes.NodeEvent ev) {
103: assertNull("Already fired", event);
104: event = ev;
105: }
106:
107: public void propertyChange(
108: java.beans.PropertyChangeEvent propertyChangeEvent) {
109: propEvents.add(propertyChangeEvent.getPropertyName());
110: /*
111: System.out.println("propertyChangeEvent = "+propertyChangeEvent);
112: assertNull("Already fired", event);
113: event = propertyChangeEvent;
114: */
115: }
116:
117: public void testDisplayName() {
118: ModelEvent e = new ModelEvent.NodeChanged(this , "Root",
119: ModelEvent.NodeChanged.DISPLAY_NAME_MASK);
120: cm.fire(e);
121: try {
122: SwingUtilities.invokeAndWait(new Runnable() {
123: public void run() {
124: }
125: });
126: } catch (InterruptedException iex) {
127: fail(iex.toString());
128: } catch (InvocationTargetException itex) {
129: fail(itex.toString());
130: }
131: assertTrue("Display Name was not fired", propEvents
132: .contains(Node.PROP_DISPLAY_NAME));
133: //assertNotNull("Was not fired", this.event);
134: }
135:
136: public void testIcon() {
137: ModelEvent e = new ModelEvent.NodeChanged(this , "Root",
138: ModelEvent.NodeChanged.ICON_MASK);
139: cm.fire(e);
140: try {
141: SwingUtilities.invokeAndWait(new Runnable() {
142: public void run() {
143: }
144: });
145: } catch (InterruptedException iex) {
146: fail(iex.toString());
147: } catch (InvocationTargetException itex) {
148: fail(itex.toString());
149: }
150: assertTrue("Icon was not fired", propEvents
151: .contains(Node.PROP_ICON));
152: }
153:
154: public void testShortDescription() {
155: ModelEvent e = new ModelEvent.NodeChanged(this , "Root",
156: ModelEvent.NodeChanged.SHORT_DESCRIPTION_MASK);
157: cm.fire(e);
158: try {
159: SwingUtilities.invokeAndWait(new Runnable() {
160: public void run() {
161: }
162: });
163: } catch (InterruptedException iex) {
164: fail(iex.toString());
165: } catch (InvocationTargetException itex) {
166: fail(itex.toString());
167: }
168: assertTrue("Short Description was not fired", propEvents
169: .contains(Node.PROP_SHORT_DESCRIPTION));
170: }
171:
172: public void testChildren() {
173: n.getChildren().getNodes();
174: /*
175: ModelEvent e = new ModelEvent.NodeChanged(this, "Root", ModelEvent.NodeChanged.CHILDREN_MASK);
176: cm.fire(e);
177: try {
178: SwingUtilities.invokeAndWait (new Runnable () {
179: public void run () {}
180: });
181: } catch (InterruptedException iex) {
182: fail(iex.toString());
183: } catch (InvocationTargetException itex) {
184: fail(itex.toString());
185: }
186: //assertTrue("Short Description was not fired", propEvents.contains(Node.PROP_));
187: assertNotNull("Children were not fired", this.event);
188: */
189: }
190:
191: public final class CompoundModel1 extends BasicTest.CompoundModel {
192:
193: int dn = 0;
194: int ib = 0;
195: int sd = 0;
196: int cc = 0;
197:
198: protected void addCall(String methodName, Object node) {
199: // Ignore multiple calls
200: }
201:
202: // init ....................................................................
203:
204: public String getDisplayName(Object node)
205: throws UnknownTypeException {
206: String dns = super .getDisplayName(node);
207: dns += (dn++);
208: return dns;
209: }
210:
211: public String getIconBase(Object node)
212: throws UnknownTypeException {
213: String ibs = super .getIconBase(node);
214: ibs += (ib++);
215: return ibs;
216: }
217:
218: public String getShortDescription(Object node)
219: throws UnknownTypeException {
220: String sds = super .getShortDescription(node);
221: sds += (sd++);
222: return sds;
223: }
224:
225: /**
226: * Returns number of children for given node.
227: *
228: * @param node the parent node
229: * @throws UnknownTypeException if this TreeModel implementation is not
230: * able to resolve children for given node type
231: *
232: * @return true if node is leaf
233: */
234: public synchronized int getChildrenCount(Object node)
235: throws UnknownTypeException {
236: return super .getChildrenCount(node) + (cc++);
237: }
238:
239: public Object[] getChildren(Object parent, int from, int to)
240: throws UnknownTypeException {
241: System.err.println("CompoundModel1.getChildren(" + parent
242: + ", " + from + ", " + to + ")");
243: //Thread.dumpStack();
244: addCall("getChildren", parent);
245: Object[] ch = new Object[3 + (cc - 1)];
246: if (parent == ROOT) {
247: for (int i = 0; i < ch.length; i++) {
248: // Use Character.valueOf() on 1.5
249: ch[i] = new Character((char) ('a' + i)).toString();
250: }
251: return ch;
252: }
253: if (parent instanceof String) {
254: for (int i = 0; i < ch.length; i++) {
255: // Use Character.valueOf() on 1.5
256: ch[i] = ((String) parent + new Character(
257: (char) ('a' + i)).toString());
258: }
259: return ch;
260: }
261: throw new UnknownTypeException(parent);
262: }
263: }
264: }
|