KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > SearchDisplayer


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
21 package org.netbeans.modules.search;
22
23 import java.awt.EventQueue JavaDoc;
24
25 import java.io.IOException JavaDoc;
26 import java.lang.ref.Reference JavaDoc;
27 import java.lang.ref.WeakReference JavaDoc;
28 import org.openide.ErrorManager;
29
30 import org.openide.nodes.Node;
31 import org.openide.util.NbBundle;
32 import org.openide.windows.IOProvider;
33 import org.openide.windows.InputOutput;
34 import org.openide.windows.OutputListener;
35 import org.openide.windows.OutputWriter;
36
37
38 /**
39  * Presents search results in output window.
40  *
41  * @author Petr Kuzel
42  * @author Marian Petras
43  */

44 public final class SearchDisplayer {
45
46     /** name of attribute "text to display in the Output Window" */
47     public static final String JavaDoc ATTR_OUTPUT_LINE = "output line"; //NOI18N
48
/** writer to that tab */
49     private OutputWriter ow = null;
50     /** */
51     private Reference JavaDoc owRef = null;
52
53     /** Creates new SearchDisplayer */
54     SearchDisplayer() {
55     }
56
57     /**
58      */

59     void prepareOutput() {
60         String JavaDoc tabName = NbBundle.getMessage(ResultView.class,
61                                              "TITLE_SEARCH_RESULTS"); //NOI18N
62
InputOutput searchIO = IOProvider.getDefault().getIO(tabName, false);
63         ow = searchIO.getOut();
64         owRef = new WeakReference JavaDoc(ow);
65         
66         searchIO.select();
67     }
68     
69     /**
70      */

71     static void clearOldOutput(final Reference JavaDoc outputWriterRef) {
72         if (outputWriterRef != null) {
73             OutputWriter oldWriter = (OutputWriter) outputWriterRef.get();
74             if (oldWriter != null) {
75                 try {
76                     oldWriter.reset();
77                 } catch (IOException JavaDoc ex) {
78                     ErrorManager.getDefault().notify(ex);
79                 }
80             }
81         }
82     }
83     
84     /**
85      * Displays the given nodes.
86      *
87      * @param nodes nodes to display
88      */

89     void displayNodes(final Node[] nodes) {
90
91         /* Prepare the output lines: */
92         final String JavaDoc[] outputLines = new String JavaDoc[nodes.length];
93         final OutputListener[] listeners = new OutputListener[nodes.length];
94
95         for (int i = 0; i < nodes.length; i++) {
96             final Node node = nodes[i];
97             final Object JavaDoc o = node.getValue(ATTR_OUTPUT_LINE);
98             outputLines[i] = o instanceof String JavaDoc ? (String JavaDoc) o
99                                                  : node.getShortDescription();
100             listeners[i] = node instanceof OutputListener ? (OutputListener)node
101                                                           : null;
102         }
103
104         /* Print the output lines: */
105         try {
106             EventQueue.invokeAndWait(new Runnable JavaDoc() {
107                 public void run() {
108                     try {
109                         for (int i = 0; i < outputLines.length; i++) {
110                             OutputListener listener = listeners[i];
111                             if (listener != null) {
112                                 ow.println(outputLines[i], listener);
113                             } else {
114                                 ow.println(outputLines[i]);
115                             }
116                         }
117                     } catch (Exception JavaDoc ex) {
118                         ErrorManager.getDefault()
119                         .notify(ErrorManager.EXCEPTION, ex);
120                     }
121                 }
122             });
123         } catch (Exception JavaDoc ex) {
124             ErrorManager.getDefault().notify(ex);
125         }
126     }
127     
128     /**
129      */

130     void finishDisplaying() {
131         ow.flush();
132         ow.close();
133         ow = null;
134     }
135     
136     /**
137      */

138     Reference JavaDoc getOutputWriterRef() {
139         return owRef;
140     }
141     
142 }
143
Popular Tags