KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > lib > ProgressOperator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.j2ee.lib;
21
22 import java.lang.reflect.Method JavaDoc;
23 import javax.swing.JDialog JavaDoc;
24 import org.netbeans.jemmy.JemmyException;
25 import org.netbeans.jemmy.Waitable;
26 import org.netbeans.jemmy.Waiter;
27 import org.netbeans.jemmy.operators.Operator;
28
29 /**
30  * Handle Progress bars at the main window of NetBeans.
31  *
32  * @author Jiri.Skrivanek@sun.com
33  */

34 public class ProgressOperator {
35
36     /** Wait process started.
37      */

38     public static void waitStarted(final String JavaDoc name, long timeout) {
39         try {
40             Waiter waiter = new Waiter(new Waitable() {
41                 public Object JavaDoc actionProduced(Object JavaDoc anObject) {
42                     return processInProgress(name) ? Boolean.TRUE : null;
43                 }
44                 public String JavaDoc getDescription() {
45                     return("Wait process "+name+" is started.");
46                 }
47             });
48             waiter.getTimeouts().setTimeout("Waiter.WaitingTime", timeout);
49             waiter.waitAction(null);
50         } catch (InterruptedException JavaDoc e) {
51             throw new JemmyException("Interrupted.", e);
52         }
53         
54     }
55     
56     /** Wait process with given name finished.
57      */

58     public static void waitFinished(final String JavaDoc name, long timeout) {
59         try {
60             Waiter waiter = new Waiter(new Waitable() {
61                 public Object JavaDoc actionProduced(Object JavaDoc anObject) {
62                     return processInProgress(name) ? null : Boolean.TRUE;
63                 }
64                 public String JavaDoc getDescription() {
65                     return("Wait process "+name+" is finished.");
66                 }
67             });
68             waiter.getTimeouts().setTimeout("Waiter.WaitingTime", timeout);
69             waiter.waitAction(null);
70         } catch (InterruptedException JavaDoc e) {
71             throw new JemmyException("Interrupted.", e);
72         }
73         
74     }
75     
76     /** Wait all processes finished.
77      */

78     public static void waitFinished(long timeout) {
79         waitFinished("", timeout); // NOI18N
80
}
81     
82     private static boolean processInProgress(String JavaDoc name) {
83         try {
84             Class JavaDoc clazz = Class.forName("org.netbeans.progress.module.Controller");
85             Method JavaDoc getDefaultMethod = clazz.getDeclaredMethod("getDefault", (Class JavaDoc[])null);
86             getDefaultMethod.setAccessible(true);
87             Object JavaDoc controllerInstance = getDefaultMethod.invoke(null, (Object JavaDoc[])null);
88             
89             Method JavaDoc getModelMethod = clazz.getDeclaredMethod("getModel", (Class JavaDoc[])null);
90             getModelMethod.setAccessible(true);
91             Object JavaDoc taskModelInstance = getModelMethod.invoke(controllerInstance, (Object JavaDoc[])null);
92             
93             //Method getSizeMethod = taskModelInstance.getClass().getDeclaredMethod("getSize", (Class[])null);
94
//Object size = getSizeMethod.invoke(taskModelInstance, (Object[])null);
95
//System.out.println("SIZE="+((Integer)size));
96

97             Method JavaDoc getHandlesMethod = taskModelInstance.getClass().getDeclaredMethod("getHandles", (Class JavaDoc[])null);
98             Object JavaDoc[] handles = (Object JavaDoc[])getHandlesMethod.invoke(taskModelInstance, (Object JavaDoc[])null);
99             
100             for(int i=0;i<handles.length;i++) {
101                 Method JavaDoc getDisplayNameMethod = handles[i].getClass().getDeclaredMethod("getDisplayName", (Class JavaDoc[])null);
102                 String JavaDoc displayName = (String JavaDoc)getDisplayNameMethod.invoke(handles[i], (Object JavaDoc[])null);
103                 //System.out.println("DISPLAY_NAME="+displayName);
104
if(Operator.getDefaultStringComparator().equals(displayName, name)) {
105                     return true;
106                 }
107             }
108             return false;
109             
110             //Method addListDataListenerMethod = taskModelInstance.getClass().getDeclaredMethod("addListDataListener", ListDataListener.class);
111
//addListDataListenerMethod.invoke(taskModelInstance, new TestProgressBar());
112

113             
114         } catch (Exception JavaDoc e) {
115             throw new JemmyException("Reflection operation failed.", e);
116         }
117     }
118 }
Popular Tags