KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import javax.accessibility.AccessibleContext JavaDoc;
27 import javax.swing.JScrollPane JavaDoc;
28 import javax.swing.JTextPane JavaDoc;
29 import javax.swing.Timer JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.swing.text.Style JavaDoc;
33 import javax.swing.text.StyleConstants JavaDoc;
34 import javax.swing.text.StyleContext JavaDoc;
35 import javax.swing.text.StyledDocument JavaDoc;
36 import org.openide.ErrorManager;
37 import org.openide.util.NbBundle;
38
39 /**
40  *
41  * @author Marian Petras
42  */

43 final class ResultPanelOutput extends JScrollPane JavaDoc
44                               implements ActionListener JavaDoc {
45     
46     private static final boolean LOG = false;
47     
48     static final Color JavaDoc selectedFg;
49     static final Color JavaDoc unselectedFg;
50     static final Color JavaDoc selectedErr;
51     static final Color JavaDoc unselectedErr;
52     
53     private static final int UPDATE_DELAY = 300; //milliseconds
54

55     static {
56         
57         /*
58          * The property names and colour value constants were copied from class
59          * org.netbeans.core.output2.WrappedTextView.
60          */

61         
62         Color JavaDoc color;
63         
64         color = UIManager.getColor("nb.output.foreground.selected"); //NOI18N
65
if (color == null) {
66             color = UIManager.getColor("textText"); //NOI18N
67
if (color == null) {
68                 color = Color.BLACK;
69             }
70         }
71         selectedFg = color;
72         
73         color = UIManager.getColor("nb.output.foreground"); //NOI18N
74
if (color == null) {
75             color = selectedFg;
76         }
77         unselectedFg = color;
78
79         color = UIManager.getColor("nb.output.err.foreground.selected");//NOI18N
80
if (color == null) {
81             color = new Color JavaDoc(164, 0, 0);
82         }
83         selectedErr = color;
84         
85         color = UIManager.getColor("nb.output.err.foreground"); //NOI18N
86
if (color == null) {
87             color = selectedErr;
88         }
89         unselectedErr = color;
90     }
91     
92     /** */
93     private final Style JavaDoc outputStyle, errOutputStyle;
94     //private final Style headingStyle;
95

96     /** */
97     private final JTextPane JavaDoc textPane;
98     /** */
99     private final StyledDocument JavaDoc doc;
100     /** */
101     private final ResultDisplayHandler displayHandler;
102     
103     /** */
104     boolean newLinePending = false;
105     
106     private Timer JavaDoc timer = null;
107     
108     /*
109      * accessed from multiple threads but accessed only from blocks
110      * synchronized with the ResultDisplayHandler's output queue lock
111      */

112     private volatile boolean timerRunning = false;
113     
114     /**
115      * Creates a new instance of ResultPanelOutput
116      */

117     ResultPanelOutput(ResultDisplayHandler displayHandler) {
118         super();
119         if (LOG) {
120             System.out.println("ResultPanelOutput.<init>");
121         }
122         
123         textPane = new JTextPane JavaDoc();
124         doc = textPane.getStyledDocument();
125         textPane.setEditable(false);
126         setViewportView(textPane);
127         
128         AccessibleContext JavaDoc accessibleContext = textPane.getAccessibleContext();
129         accessibleContext.setAccessibleName(
130                 NbBundle.getMessage(getClass(), "ACSN_OutputTextPane"));//NOI18N
131
accessibleContext.setAccessibleDescription(
132                 NbBundle.getMessage(getClass(), "ACSD_OutputTextPane"));//NOI18N
133

134         Style JavaDoc defaultStyle = StyleContext.getDefaultStyleContext()
135                              .getStyle(StyleContext.DEFAULT_STYLE);
136         
137         outputStyle = doc.addStyle("output", defaultStyle); //NOI18N
138
StyleConstants.setFontFamily(outputStyle, "Monospaced"); //NOI18N
139
StyleConstants.setForeground(outputStyle, unselectedFg);
140         
141         errOutputStyle = doc.addStyle("error", outputStyle);
142         StyleConstants.setForeground(errOutputStyle, unselectedErr);
143         
144         //headingStyle = doc.addStyle("heading", outputStyle); //NOI18N
145
//StyleConstants.setUnderline(headingStyle, true);
146

147         this.displayHandler = displayHandler;
148     }
149     
150     /**
151      */

152     public void addNotify() {
153         super.addNotify();
154         
155         final Object JavaDoc[] pendingOutput;
156         
157         if (LOG) {
158             System.out.println("ResultPanelOutput.addNotify()");
159         }
160         
161         /*
162          * We must make the following block synchronized using the output queue
163          * lock to prevent a scenario that some new output would be delivered to
164          * the display handler and the output listener would not be set yet.
165          */

166         synchronized (displayHandler.getOutputQueueLock()) {
167             pendingOutput = displayHandler.consumeOutput();
168             if (pendingOutput.length == 0) {
169                 displayHandler.setOutputListener(this);
170             }
171         }
172         
173         if (pendingOutput.length != 0) {
174             displayOutput(pendingOutput);
175             startTimer();
176         }
177     }
178     
179     /**
180      */

181     void outputAvailable() {
182
183         /* Called from the AntLogger's thread */
184
185         if (LOG) {
186             System.out.println("ResultOutputPanel.outputAvailable() - called by the AntLogger");
187         }
188         //synchronized (displayHandler.getOutputQueueLock()):
189
final Object JavaDoc[] pendingOutput = displayHandler.consumeOutput();
190         assert pendingOutput.length != 0;
191         new OutputDisplayer(pendingOutput).run();
192         displayHandler.setOutputListener(null);
193         if (!timerRunning) {
194             startTimer();
195         }
196     }
197
198     final class OutputDisplayer implements Runnable JavaDoc {
199         private final Object JavaDoc[] output;
200         OutputDisplayer(Object JavaDoc[] output) {
201             this.output = output;
202         }
203         public void run() {
204             if (!EventQueue.isDispatchThread()) {
205                 EventQueue.invokeLater(this);
206                 return;
207             }
208             displayOutput(output);
209         }
210     }
211     
212     /**
213      * This method is called by a Swing timer (in the dispatch thread).
214      */

215     public void actionPerformed(ActionEvent JavaDoc e) {
216         
217         /* Called by the Swing timer (in the EventDispatch thread) */
218         
219         assert EventQueue.isDispatchThread();
220         
221         if (LOG) {
222             System.out.println("ResultOutputPanel.actionPerformed(...) - called by the timer");
223         }
224         final Object JavaDoc[] pendingOutput = displayHandler.consumeOutput();
225         if (pendingOutput.length != 0) {
226             displayOutput(pendingOutput);
227         } else {
228             synchronized (displayHandler.getOutputQueueLock()) {
229                 stopTimer();
230                 displayHandler.setOutputListener(this);
231             }
232         }
233     }
234     
235     /**
236      */

237     private void startTimer() {
238         if (LOG) {
239             System.out.println("ResultPanelOutput.startTimer()");
240         }
241         if (timer == null) {
242             timer = new Timer JavaDoc(UPDATE_DELAY, this);
243         }
244         timerRunning = true;
245         timer.start();
246     }
247     
248     /**
249      */

250     private void stopTimer() {
251         if (LOG) {
252             System.out.println("ResultPanelOutput.stopTimer()");
253         }
254         if (timer != null) {
255             timer.stop();
256             timerRunning = false;
257         }
258     }
259     
260     /**
261      */

262     void displayOutput(final Object JavaDoc[] output) {
263         assert EventQueue.isDispatchThread();
264         
265         if (LOG) {
266             System.out.println("ResultPanelOutput.displayOutput(...):");
267             for (int i = 0; output[i] != null; i++) {
268                 System.out.println(" " + output[i]);
269             }
270         }
271         Object JavaDoc o;
272         int index = 0;
273         while ((o = output[index++]) != null) {
274             boolean errOutput = false;
275             if (o == Boolean.TRUE) {
276                 o = output[index++];
277                 errOutput = true;
278             }
279             displayOutputLine(o.toString(), errOutput);
280         }
281     }
282     
283     /**
284      */

285     private void displayOutputLine(final String JavaDoc text, final boolean error) {
286         final Style JavaDoc textStyle = error ? errOutputStyle : outputStyle;
287         
288         try {
289             if (newLinePending) {
290                 doc.insertString(doc.getLength(), "\n", outputStyle); //NOI18N
291
newLinePending = false;
292             }
293             doc.insertString(doc.getLength(), text, textStyle);
294             newLinePending = true;
295         } catch (BadLocationException JavaDoc ex) {
296             ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
297         }
298     }
299     
300     //<editor-fold defaultstate="collapsed" desc="displayReport(Report)">
301
/* *
302      * /
303     private void displayReport(final Report report) {
304         if (report == null) {
305             clear();
306             return;
307         }
308         
309         try {
310             doc.insertString(
311                     0,
312                     NbBundle.getMessage(getClass(), "MSG_StdOutput"), //NOI18N
313                     headingStyle);
314             doc.insertString(
315                     doc.getLength(),
316                     "\n", //NOI18N
317                     headingStyle);
318             if ((report.outputStd != null) && (report.outputStd.length != 0)) {
319                 displayText(report.outputStd);
320             }
321             doc.insertString(
322                     doc.getLength(),
323                     "\n\n", //NOI18N
324                     outputStyle);
325             doc.insertString(
326                     doc.getLength(),
327                     NbBundle.getMessage(getClass(), "MSG_ErrOutput"), //NOI18N
328                     headingStyle);
329             if ((report.outputErr != null) && (report.outputErr.length != 0)) {
330                 doc.insertString(
331                         doc.getLength(),
332                         "\n", //NOI18N
333                         headingStyle);
334                 displayText(report.outputErr);
335             }
336         } catch (BadLocationException ex) {
337             ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
338         }
339     }
340     */

341     //</editor-fold>
342

343     /**
344      */

345     private void clear() {
346         assert EventQueue.isDispatchThread();
347         
348         try {
349             doc.remove(0, doc.getLength());
350         } catch (BadLocationException JavaDoc ex) {
351             ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
352         }
353     }
354     
355     //<editor-fold defaultstate="collapsed" desc="displayText(String[])">
356
/* *
357      * /
358     private void displayText(final String[] lines) throws BadLocationException {
359         final int limit = lines.length - 1;
360         for (int i = 0; i < limit; i++) {
361             doc.insertString(doc.getLength(),
362                              lines[i] + '\n',
363                              outputStyle);
364         }
365         doc.insertString(doc.getLength(),
366                          lines[limit],
367                          outputStyle);
368     }
369     */

370     //</editor-fold>
371

372     //<editor-fold defaultstate="collapsed" desc="display(DisplayContents)">
373
/* *
374      * /
375     private void display(ResultDisplayHandler.DisplayContents display) {
376         assert EventQueue.isDispatchThread();
377         
378         Report report = display.getReport();
379         String msg = display.getMessage();
380         if (report != null) {
381             displayReport(report);
382         } else {
383             clear();
384         }
385     }
386     //</editor-fold>
387
388     /* *
389      * /
390     //<editor-fold defaultstate="collapsed" desc="updateDisplay()">
391     private void updateDisplay() {
392         ResultDisplayHandler.DisplayContents display
393                                                 = displayHandler.getDisplay();
394         if (display != null) {
395             display(display);
396         }
397     }
398     */

399     //</editor-fold>
400

401     //<editor-fold defaultstate="collapsed" desc="stateChanged(ChangeEvent)">
402
/* *
403      * /
404     public void stateChanged(ChangeEvent e) {
405         updateDisplay();
406     }
407      */

408     //</editor-fold>
409

410     /**
411      */

412     public boolean requestFocusInWindow() {
413         return textPane.requestFocusInWindow();
414     }
415
416 }
417
Popular Tags