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 gui;
043:
044: import java.lang.reflect.Method;
045: import javax.swing.JDialog;
046: import org.netbeans.jemmy.JemmyException;
047: import org.netbeans.jemmy.Waitable;
048: import org.netbeans.jemmy.Waiter;
049: import org.netbeans.jemmy.operators.Operator;
050:
051: /**
052: * Handle Progress bars at the main window of NetBeans.
053: *
054: * @author Jiri.Skrivanek@sun.com
055: */
056: public class ProgressSupport {
057:
058: /** Wait process started.
059: */
060: public static void waitStarted(final String name, long timeout) {
061: try {
062: Waiter waiter = new Waiter(new Waitable() {
063: public Object actionProduced(Object anObject) {
064: return processInProgress(name) ? Boolean.TRUE
065: : null;
066: }
067:
068: public String getDescription() {
069: return ("Wait process " + name + " is started.");
070: }
071: });
072: waiter.getTimeouts().setTimeout("Waiter.WaitingTime",
073: timeout);
074: waiter.waitAction(null);
075: } catch (InterruptedException e) {
076: throw new JemmyException("Interrupted.", e);
077: }
078:
079: }
080:
081: /** Wait process with given name finished.
082: */
083: public static void waitFinished(final String name, long timeout) {
084: try {
085: Waiter waiter = new Waiter(new Waitable() {
086: public Object actionProduced(Object anObject) {
087: return processInProgress(name) ? null
088: : Boolean.TRUE;
089: }
090:
091: public String getDescription() {
092: return ("Wait process " + name + " is finished.");
093: }
094: });
095: waiter.getTimeouts().setTimeout("Waiter.WaitingTime",
096: timeout);
097: waiter.waitAction(null);
098: } catch (InterruptedException e) {
099: throw new JemmyException("Interrupted.", e);
100: }
101:
102: }
103:
104: /** Wait all processes finished.
105: */
106: public static void waitFinished(long timeout) {
107: waitFinished("", timeout); // NOI18N
108: }
109:
110: private static boolean processInProgress(String name) {
111: try {
112: Class clazz = Class
113: .forName("org.netbeans.progress.module.Controller");
114: Method getDefaultMethod = clazz.getDeclaredMethod(
115: "getDefault", (Class[]) null);
116: getDefaultMethod.setAccessible(true);
117: Object controllerInstance = getDefaultMethod.invoke(null,
118: (Object[]) null);
119:
120: Method getModelMethod = clazz.getDeclaredMethod("getModel",
121: (Class[]) null);
122: getModelMethod.setAccessible(true);
123: Object taskModelInstance = getModelMethod.invoke(
124: controllerInstance, (Object[]) null);
125:
126: //Method getSizeMethod = taskModelInstance.getClass().getDeclaredMethod("getSize", (Class[])null);
127: //Object size = getSizeMethod.invoke(taskModelInstance, (Object[])null);
128: //System.out.println("SIZE="+((Integer)size));
129:
130: Method getHandlesMethod = taskModelInstance.getClass()
131: .getDeclaredMethod("getHandles", (Class[]) null);
132: Object[] handles = (Object[]) getHandlesMethod.invoke(
133: taskModelInstance, (Object[]) null);
134:
135: for (int i = 0; i < handles.length; i++) {
136: Method getDisplayNameMethod = handles[i].getClass()
137: .getDeclaredMethod("getDisplayName",
138: (Class[]) null);
139: String displayName = (String) getDisplayNameMethod
140: .invoke(handles[i], (Object[]) null);
141: //System.out.println("DISPLAY_NAME="+displayName);
142: if (Operator.getDefaultStringComparator().equals(
143: displayName, name)) {
144: return true;
145: }
146: }
147: return false;
148:
149: //Method addListDataListenerMethod = taskModelInstance.getClass().getDeclaredMethod("addListDataListener", ListDataListener.class);
150: //addListDataListenerMethod.invoke(taskModelInstance, new TestProgressBar());
151:
152: } catch (Exception e) {
153: throw new JemmyException("Reflection operation failed.", e);
154: }
155: }
156: }
|