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