KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > ProgressIndicator


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.modules.junit;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import org.openide.ErrorManager;
25 import org.openide.awt.StatusDisplayer;
26 import org.openide.util.NbBundle;
27
28 /** Thread-safe wrapper around JUnitProgress - panel showing progress info
29  * and allowing the user to cancel running task. Used in actions creating
30  * or executing tests.
31  *
32  * @author Tomas Pavek
33  * @author Ondrej Rypacek
34  * @author Marian Petras
35  */

36 class ProgressIndicator {
37
38     /**
39      * initial message to be used when GUI is created.
40      * It is only used if setMessage(...) is called sooner than show().
41      */

42     private String JavaDoc initialMessage;
43     private JUnitProgress progressPanel;
44     /** <code>true</code> if GUI (dialog) creation has passed or is scheduled */
45     private boolean guiCreationScheduled;
46
47     synchronized boolean isCanceled() {
48         return progressPanel != null ? progressPanel.isCanceled() : false;
49     }
50
51     void displayStatusText(String JavaDoc statusText) {
52         StatusDisplayer.getDefault().setStatusText(statusText);
53     }
54
55     /**
56      * Sets a message to be displayed in the progress GUI.
57      * If the GUI already exists (or is scheduled to be created), this method
58      * will cause the message in the GUI to be changed to the given text.
59      * If the GUI neither exists nor is scheduled, this method just remembers
60      * the message so that it will used when the GUI is created.
61      */

62     synchronized void setMessage(final String JavaDoc msg, final boolean displayStatus) {
63         if (guiCreationScheduled) {
64             EventQueue.invokeLater(new Runnable JavaDoc() {
65                 public void run() {
66                     progressPanel.setMessage(msg, displayStatus);
67                 }
68             });
69         } else {
70             /*
71              * Set an initial message to be used when GUI is about to be
72              * created:
73              */

74             initialMessage = msg;
75         }
76     }
77
78     synchronized void show() {
79         if (!guiCreationScheduled) {
80             sendToAwtQueue("createAndShowDialog"); //NOI18N
81
guiCreationScheduled = true;
82         } else {
83             sendToAwtQueue("showDialog"); //NOI18N
84
}
85     }
86
87     synchronized void hide() {
88         if (guiCreationScheduled) {
89             sendToAwtQueue("hideDialog"); //NOI18N
90
}
91         StatusDisplayer.getDefault().setStatusText(""); //NOI18N
92
}
93
94     /**
95      */

96     synchronized void createAndShowDialog() {
97         String JavaDoc msg = NbBundle.getMessage(ProgressIndicator.class,
98                                "LBL_generator_progress_title"); //NOI18N
99
progressPanel = new JUnitProgress(msg);
100         
101         if (initialMessage != null) {
102             progressPanel.setMessage(initialMessage);
103             initialMessage = null;
104         }
105         
106         showDialog();
107     }
108
109     /**
110      */

111     synchronized void showDialog() {
112         progressPanel.showMe(true);
113     }
114
115     /**
116      */

117     synchronized void hideDialog() {
118         progressPanel.hideMe();
119     }
120
121     /**
122      */

123     private void sendToAwtQueue(String JavaDoc methodName) {
124         final Method JavaDoc method;
125         try {
126             method = getClass().getDeclaredMethod(methodName, new Class JavaDoc[0]);
127         } catch (Exception JavaDoc ex) {
128             ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
129             return;
130         }
131         EventQueue.invokeLater(new Runnable JavaDoc() {
132             public void run() {
133                 try {
134                     method.invoke(ProgressIndicator.this, (Object JavaDoc[]) null);
135                 } catch (Exception JavaDoc ex) {
136                     ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
137                 }
138             }
139         });
140     }
141
142 }
143
Popular Tags