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): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * 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: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy.drivers.trees;
048:
049: import java.awt.Point;
050:
051: import java.awt.event.InputEvent;
052: import java.awt.event.KeyEvent;
053:
054: import javax.swing.text.JTextComponent;
055:
056: import javax.swing.tree.TreeModel;
057: import javax.swing.tree.TreePath;
058:
059: import org.netbeans.jemmy.JemmyException;
060: import org.netbeans.jemmy.QueueTool;
061: import org.netbeans.jemmy.Timeout;
062: import org.netbeans.jemmy.Waitable;
063: import org.netbeans.jemmy.Waiter;
064:
065: import org.netbeans.jemmy.drivers.DriverManager;
066: import org.netbeans.jemmy.drivers.LightSupportiveDriver;
067: import org.netbeans.jemmy.drivers.MouseDriver;
068: import org.netbeans.jemmy.drivers.PathChooser;
069: import org.netbeans.jemmy.drivers.TextDriver;
070: import org.netbeans.jemmy.drivers.TreeDriver;
071:
072: import org.netbeans.jemmy.operators.ComponentOperator;
073: import org.netbeans.jemmy.operators.JTextComponentOperator;
074: import org.netbeans.jemmy.operators.JTreeOperator;
075: import org.netbeans.jemmy.operators.Operator;
076:
077: /**
078: * TreeDriver for javax.swing.JTree component type.
079: * Uses mouse operations.
080: *
081: * @author Alexandre Iline(alexandre.iline@sun.com)
082: */
083: public class JTreeMouseDriver extends LightSupportiveDriver implements
084: TreeDriver {
085: QueueTool queueTool;
086:
087: /**
088: * Constructs a JTreeMouseDriver.
089: */
090: public JTreeMouseDriver() {
091: super (
092: new String[] { "org.netbeans.jemmy.operators.JTreeOperator" });
093: queueTool = new QueueTool();
094: }
095:
096: public void selectItem(ComponentOperator oper, int index) {
097: selectItems(oper, new int[] { index });
098: }
099:
100: public void selectItems(final ComponentOperator oper, int[] indices) {
101: ((JTreeOperator) oper).clearSelection();
102: checkSupported(oper);
103: final MouseDriver mdriver = DriverManager.getMouseDriver(oper);
104: final JTreeOperator toper = (JTreeOperator) oper;
105: final Timeout clickTime = oper.getTimeouts().create(
106: "ComponentOperator.MouseClickTimeout");
107: QueueTool qt = new QueueTool();
108: for (int i = 0; i < indices.length; i++) {
109: final int index = i;
110: if (!queueTool.isDispatchThread()) {
111: toper.scrollToRow(indices[i]);
112: }
113: final Point p = toper.getPointToClick(indices[index]);
114: queueTool.invokeSmoothly(new QueueTool.QueueAction(
115: "Path selecting") {
116: public Object launch() {
117: mdriver.clickMouse(oper, p.x, p.y, 1, Operator
118: .getDefaultMouseButton(), (index == 0) ? 0
119: : InputEvent.CTRL_MASK, clickTime);
120: return (null);
121: }
122: });
123: }
124: //1.5 workaround
125: if (System.getProperty("java.specification.version").compareTo(
126: "1.4") > 0) {
127: if (!QueueTool.isDispatchThread()) {
128: queueTool.setOutput(oper.getOutput()
129: .createErrorOutput());
130: queueTool.waitEmpty(10);
131: queueTool.waitEmpty(10);
132: queueTool.waitEmpty(10);
133: }
134: }
135: //end of 1.5 workaround
136: }
137:
138: public void expandItem(ComponentOperator oper, final int index) {
139: checkSupported(oper);
140: final JTreeOperator toper = (JTreeOperator) oper;
141: final MouseDriver mdriver = DriverManager.getMouseDriver(oper);
142: if (!toper.isExpanded(index)) {
143: queueTool.invokeSmoothly(new QueueTool.QueueAction(
144: "Path selecting") {
145: public Object launch() {
146: Point p = toper.getPointToClick(index);
147: mdriver
148: .clickMouse(
149: toper,
150: p.x,
151: p.y,
152: 2,
153: Operator.getDefaultMouseButton(),
154: 0,
155: toper
156: .getTimeouts()
157: .create(
158: "ComponentOperator.MouseClickTimeout"));
159: return (null);
160: }
161: });
162: }
163: }
164:
165: public void collapseItem(ComponentOperator oper, final int index) {
166: checkSupported(oper);
167: final JTreeOperator toper = (JTreeOperator) oper;
168: final MouseDriver mdriver = DriverManager.getMouseDriver(oper);
169: if (toper.isExpanded(index)) {
170: queueTool.invokeSmoothly(new QueueTool.QueueAction(
171: "Path selecting") {
172: public Object launch() {
173: Point p = toper.getPointToClick(index);
174: mdriver
175: .clickMouse(
176: toper,
177: p.x,
178: p.y,
179: 2,
180: Operator.getDefaultMouseButton(),
181: 0,
182: toper
183: .getTimeouts()
184: .create(
185: "ComponentOperator.MouseClickTimeout"));
186: return (null);
187: }
188: });
189: }
190: }
191:
192: public void editItem(ComponentOperator oper, int index,
193: Object newValue, Timeout waitEditorTime) {
194: JTextComponentOperator textoper = startEditingAndReturnEditor(
195: oper, index, waitEditorTime);
196: final TextDriver text = DriverManager
197: .getTextDriver(JTextComponentOperator.class);
198: text.clearText(textoper);
199: text.typeText(textoper, newValue.toString(), 0);
200: DriverManager.getKeyDriver(oper).pushKey(
201: textoper,
202: KeyEvent.VK_ENTER,
203: 0,
204: oper.getTimeouts().create(
205: "ComponentOperator.PushKeyTimeout"));
206: }
207:
208: public void startEditing(ComponentOperator oper, int index,
209: Timeout waitEditorTime) {
210: startEditingAndReturnEditor(oper, index, waitEditorTime);
211: }
212:
213: private JTextComponentOperator startEditingAndReturnEditor(
214: ComponentOperator oper, final int index,
215: Timeout waitEditorTime) {
216: checkSupported(oper);
217: final JTreeOperator toper = (JTreeOperator) oper;
218: final MouseDriver mdriver = DriverManager.getMouseDriver(oper);
219: queueTool.invokeSmoothly(new QueueTool.QueueAction(
220: "Path selecting") {
221: public Object launch() {
222: Point p = toper.getPointToClick(index);
223: mdriver.clickMouse(toper, p.x, p.y, 1, Operator
224: .getDefaultMouseButton(), 0, toper
225: .getTimeouts().create(
226: "ComponentOperator.MouseClickTimeout"));
227: return (null);
228: }
229: });
230: oper.getTimeouts().sleep("JTreeOperator.BeforeEditTimeout");
231: queueTool.invokeSmoothly(new QueueTool.QueueAction(
232: "Path selecting") {
233: public Object launch() {
234: Point p = toper.getPointToClick(index);
235: mdriver.clickMouse(toper, p.x, p.y, 1, Operator
236: .getDefaultMouseButton(), 0, toper
237: .getTimeouts().create(
238: "ComponentOperator.MouseClickTimeout"));
239: return (null);
240: }
241: });
242: toper.getTimeouts().setTimeout(
243: "ComponentOperator.WaitComponentTimeout",
244: waitEditorTime.getValue());
245: return (new JTextComponentOperator(
246: (JTextComponent) toper
247: .waitSubComponent(new JTextComponentOperator.JTextComponentFinder())));
248: }
249: }
|