KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > output > ResultDisplayHandler


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.output;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.swing.JSplitPane JavaDoc;
31 import org.openide.ErrorManager;
32
33 /**
34  *
35  * @author Marian Petras
36  */

37 final class ResultDisplayHandler {
38     
39     /** */
40     private static final String JavaDoc ID_TREE = "tree"; //NOI18N
41
/** */
42     private static final String JavaDoc ID_OUTPUT = "output"; //NOI18N
43

44     /** */
45     private ResultPanelTree treePanel;
46     /** */
47     private ResultPanelOutput outputListener;
48     /** */
49     private Component JavaDoc displayComp;
50     
51     
52     /** Creates a new instance of ResultDisplayHandler */
53     ResultDisplayHandler() {
54     }
55     
56     /**
57      */

58     Component JavaDoc getDisplayComponent() {
59         if (displayComp == null) {
60             displayComp = createDisplayComp();
61         }
62         return displayComp;
63     }
64     
65     /**
66      */

67     private Component JavaDoc createDisplayComp() {
68         Component JavaDoc left = new StatisticsPanel(this);
69         Component JavaDoc right = new ResultPanelOutput(this);
70         JSplitPane JavaDoc splitPane
71                 = new JSplitPane JavaDoc(JSplitPane.HORIZONTAL_SPLIT, left, right);
72         return splitPane;
73     }
74     
75     /**
76      */

77     void displayShown() {
78         //
79
//PENDING
80
//
81
}
82     
83     /**
84      */

85     void displayHidden() {
86         //
87
//PENDING
88
//
89
}
90     
91     //------------------ DISPLAYING OUTPUT ----------------------//
92

93     static final Object JavaDoc[] EMPTY_QUEUE = new Object JavaDoc[0];
94     private final Object JavaDoc queueLock = new Object JavaDoc();
95     private volatile Object JavaDoc[] outputQueue;
96     private volatile int outputQueueSize = 0;
97     private int outputQueueAvailSpace;
98     
99     /**
100      */

101     Object JavaDoc getOutputQueueLock() {
102         return queueLock;
103     }
104     
105     /**
106      */

107     void setOutputListener(ResultPanelOutput outputPanel) {
108         synchronized (queueLock) {
109             this.outputListener = outputPanel;
110         }
111     }
112     
113     /**
114      */

115     void displayOutput(final String JavaDoc text, final boolean error) {
116
117         /* Called from the AntLogger's thread */
118
119         synchronized (queueLock) {
120             if (outputQueue == null) {
121                 outputQueue = new Object JavaDoc[40];
122                 outputQueueAvailSpace = outputQueue.length - 1;
123                 outputQueueSize = 0;
124             }
125             final int itemSpace = error ? 2 : 1;
126             if ((outputQueueAvailSpace -= itemSpace) < 0) {
127                 int newCapacity = (outputQueue.length < 640)
128                                   ? outputQueue.length * 2
129                                   : (outputQueue.length * 3) / 2;
130                 Object JavaDoc[] oldQueue = outputQueue;
131                 outputQueue = new Object JavaDoc[newCapacity];
132                 System.arraycopy(oldQueue, 0, outputQueue, 0, outputQueueSize);
133                 
134                 outputQueueAvailSpace += outputQueue.length - oldQueue.length;
135             }
136             if (error) {
137                 outputQueue[outputQueueSize++] = Boolean.TRUE;
138             }
139             outputQueue[outputQueueSize++] = text;
140             
141             if (outputListener != null) {
142                 outputListener.outputAvailable();
143             }
144         }
145     }
146     
147     /**
148      */

149     Object JavaDoc[] consumeOutput() {
150         synchronized (queueLock) {
151             if (outputQueueSize == 0) {
152                 return EMPTY_QUEUE;
153             }
154             Object JavaDoc[] passedQueue = outputQueue;
155             outputQueue = null;
156             outputQueueSize = 0;
157             return passedQueue;
158         }
159     }
160     
161     //-----------------------------------------------------------//
162
//------------------- DISPLAYING TREE -----------------------//
163

164     static final String JavaDoc ANONYMOUS_SUITE = new String JavaDoc();
165     /**
166      * name of the currently running suite - to be passed to the
167      * {@link #treePanel} once it is initialized
168      */

169     private String JavaDoc runningSuite;
170     private List JavaDoc<Report> reports;
171     private String JavaDoc message;
172     
173     /**
174      *
175      * @param suiteName name of the running suite; or {@code null} in the case
176      * of anonymous suite
177      */

178     void displaySuiteRunning(String JavaDoc suiteName) {
179         
180         /* Called from the AntLogger's thread */
181         
182         assert runningSuite == null;
183         
184         suiteName = (suiteName != null) ? suiteName : ANONYMOUS_SUITE;
185         
186         synchronized (this) {
187             if (treePanel == null) {
188                 runningSuite = suiteName;
189                 return;
190             }
191         }
192         
193         displayInDispatchThread("displaySuiteRunning", suiteName); //NOI18N
194
}
195
196     /**
197      */

198     void displayReport(final Report report) {
199         
200         /* Called from the AntLogger's thread */
201         
202         synchronized (this) {
203             if (treePanel == null) {
204                 if (reports == null) {
205                     reports = new ArrayList JavaDoc<Report>(10);
206                 }
207                 reports.add(report);
208                 runningSuite = null;
209                 return;
210             }
211         }
212         
213         displayInDispatchThread("displayReport", report); //NOI18N
214

215         assert runningSuite == null;
216     }
217     
218     /**
219      */

220     void displayMessage(final String JavaDoc msg) {
221
222         /* Called from the AntLogger's thread */
223
224         synchronized (this) {
225             if (treePanel == null) {
226                 message = msg;
227                 return;
228             }
229         }
230         
231         displayInDispatchThread("displayMsg", msg); //NOI18N
232
}
233     
234     /** */
235     private Map JavaDoc<String JavaDoc,Method JavaDoc> methodsMap;
236     
237     /**
238      * Calls a given display-method of class {@code ResutlPanelTree}
239      * in the AWT event queue thread.
240      *
241      * @param methodName name of the {@code ResultPanelTree} method
242      * @param param argument to be passed to the method
243      */

244     private void displayInDispatchThread(final String JavaDoc methodName,
245                                          final Object JavaDoc param) {
246         assert methodName != null;
247         assert treePanel != null;
248         
249         final Method JavaDoc method = prepareMethod(methodName);
250         if (method == null) {
251             return;
252         }
253         
254         EventQueue.invokeLater(new Runnable JavaDoc() {
255             public void run() {
256                 try {
257                     method.invoke(treePanel, new Object JavaDoc[] {param});
258                 } catch (InvocationTargetException JavaDoc ex) {
259                     ErrorManager.getDefault().notify(ex.getTargetException());
260                 } catch (Exception JavaDoc ex) {
261                     ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
262                 }
263             }
264         });
265     }
266     
267     /**
268      */

269     private Method JavaDoc prepareMethod(final String JavaDoc methodName) {
270         Method JavaDoc method;
271         
272         if (methodsMap == null) {
273             methodsMap = new HashMap JavaDoc<String JavaDoc,Method JavaDoc>(4);
274             method = null;
275         } else {
276             method = methodsMap.get(methodName);
277         }
278         
279         if ((method == null) && !methodsMap.containsKey(methodName)) {
280             final Class JavaDoc paramType;
281             if (methodName.equals("displayReport")) { //NOI18N
282
paramType = Report.class;
283             } else {
284                 assert methodName.equals("displayMsg") //NOI18N
285
|| methodName.equals("displaySuiteRunning"); //NOI18N
286
paramType = String JavaDoc.class;
287             }
288             try {
289                 method = ResultPanelTree.class
290                          .getDeclaredMethod(methodName, new Class JavaDoc[] {paramType});
291             } catch (Exception JavaDoc ex) {
292                 method = null;
293                 ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
294             }
295             methodsMap.put(methodName, method);
296         }
297         
298         return method;
299     }
300     
301     
302     /**
303      */

304     void setTreePanel(final ResultPanelTree treePanel) {
305         assert EventQueue.isDispatchThread();
306         
307         /* Called from the EventDispatch thread */
308         
309         synchronized (this) {
310             if (this.treePanel != null) {
311                 return;
312             }
313
314             this.treePanel = treePanel;
315         }
316         
317         if (message != null) {
318             treePanel.displayMsg(message);
319             message = null;
320         }
321         if (reports != null) {
322             treePanel.displayReports(reports);
323             reports = null;
324         }
325         if (runningSuite != null) {
326             treePanel.displaySuiteRunning(runningSuite != ANONYMOUS_SUITE
327                                           ? runningSuite
328                                           : null);
329             runningSuite = null;
330         }
331     }
332
333 }
334
Popular Tags