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-2007 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.test.xml.schema.core.lib.util;
043:
044: import java.awt.Component;
045: import java.awt.Container;
046: import javax.swing.JComponent;
047: import org.netbeans.jellytools.TopComponentOperator;
048: import org.netbeans.jellytools.nodes.Node;
049: import org.netbeans.jemmy.ComponentChooser;
050: import org.netbeans.jemmy.EventTool;
051: import org.netbeans.jemmy.JemmyProperties;
052: import org.netbeans.jemmy.TimeoutExpiredException;
053: import org.netbeans.jemmy.Timeouts;
054: import org.netbeans.jemmy.operators.ContainerOperator;
055: import org.netbeans.jemmy.operators.JComponentOperator;
056: import org.netbeans.test.xml.schema.core.lib.SchemaMultiView;
057:
058: /**
059: *
060: * @author ca@netbeans.org
061: */
062: public class Helpers {
063:
064: public static final String WAIT_COMPONENT_TIMEOUT = "ComponentOperator.WaitComponentTimeout";
065: public static final int NO_EVENT_TIMEOUT = 500;
066: public static EventTool et = new EventTool();
067:
068: public static void closeTopComponentIfOpened(String strName) {
069: for (int i = 0; i < 5; i++) {
070: JComponent theOneToClose = TopComponentOperator
071: .findTopComponent(strName, 0);
072: if (theOneToClose != null) {
073: new TopComponentOperator(theOneToClose).close();
074: break;
075: } else {
076: pause(200);
077: }
078: }
079: }
080:
081: public static void recurseColumns(int column, Node node,
082: SchemaMultiView opView, final int maxColumnIndex) {
083: node.select();
084:
085: System.out.println("path: " + node.getPath());
086: /*
087: if (column < maxColumnIndex) {
088: JListOperator opList = opView.getColumnListOperator(column + 1);
089: Node nextColumnNode = opTree.getRootNode();
090: if (nextColumnNode != null) {
091: recurseColumns(column + 1, nextColumnNode, opView, maxColumnIndex);
092: }
093: }
094:
095: for (int i = 0; i < node.getChildren().length; i++) {
096: Node childNode = new Node(node, i);
097: recurseColumns(column, childNode, opView, maxColumnIndex);
098: }
099: */
100: }
101:
102: public static void pause(int milliseconds) {
103: System.out.println("Paused for " + milliseconds);
104: try {
105: Thread.currentThread().sleep(milliseconds);
106: } catch (Exception e) {
107:
108: }
109: }
110:
111: public static void recurseComponent(int level, Component component) {
112:
113: JemmyProperties.getCurrentOutput().print("*");
114: System.out.print("*");
115:
116: for (int j = 0; j < level; j++) {
117: JemmyProperties.getCurrentOutput().print("|");
118: System.out.print("|");
119: }
120:
121: System.out.println("Name: " + component.getName() + ", Class: "
122: + component.getClass());
123: JemmyProperties.getCurrentOutput().printLine(
124: "Name: " + component.getName() + ", Class: "
125: + component.getClass());
126:
127: // System.out.println("Component: " + component);
128:
129: if (component instanceof Container) {
130: Component[] comps = ((Container) component).getComponents();
131: for (int i = 0; i < comps.length; i++) {
132: recurseComponent(level + 1, comps[i]);
133: }
134: }
135: }
136:
137: public static JComponentOperator getComponent(
138: ContainerOperator opContainer,
139: final String strComponentClass) {
140: return new JComponentOperator(opContainer,
141: new ComponentChooser() {
142: public boolean checkComponent(
143: java.awt.Component comp) {
144: return comp.getClass().toString().equals(
145: "class " + strComponentClass);
146: }
147:
148: public String getDescription() {
149: return strComponentClass;
150: }
151: });
152: }
153:
154: public static void waitNoEvent() {
155: waitNoEvent(NO_EVENT_TIMEOUT);
156: }
157:
158: public static void waitNoEvent(int milliseconds) {
159: et.waitNoEvent(milliseconds);
160: }
161:
162: public static JComponentOperator getComponentOperator(
163: ContainerOperator opContainer,
164: final String strComponentClass, final int index) {
165: return getComponentOperator(opContainer, strComponentClass,
166: index, 2000);
167: }
168:
169: public static JComponentOperator getComponentOperator(
170: ContainerOperator opContainer,
171: final String strComponentClass, final int index,
172: final int timeout) {
173:
174: Timeouts times = JemmyProperties.getCurrentTimeouts();
175: long to = times.setTimeout(Helpers.WAIT_COMPONENT_TIMEOUT,
176: timeout);
177:
178: JComponentOperator opJComponent = null;
179:
180: try {
181: opJComponent = new JComponentOperator(opContainer,
182: new ComponentChooser() {
183:
184: public boolean checkComponent(
185: java.awt.Component comp) {
186: return comp.getClass().toString().equals(
187: "class " + strComponentClass);
188: }
189:
190: public String getDescription() {
191: return strComponentClass;
192: }
193: }, index);
194: } catch (TimeoutExpiredException e) {
195: } finally {
196: times.setTimeout(Helpers.WAIT_COMPONENT_TIMEOUT, to);
197: }
198:
199: return opJComponent;
200: }
201:
202: public static void writeJemmyLog(String str) {
203: JemmyProperties.getCurrentOutput().printLine(str);
204: }
205:
206: public static String getUnqualifiedName(String qualifiedName) {
207: return qualifiedName
208: .substring(qualifiedName.lastIndexOf(":") + 1);
209: }
210:
211: public static String getFullTestName(String strName) {
212: return strName.replaceAll(" |:|,|#", "_");
213: }
214: }
|