001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.search;
043:
044: import java.awt.EventQueue;
045:
046: import java.io.IOException;
047: import java.lang.ref.Reference;
048: import java.lang.ref.WeakReference;
049: import org.openide.ErrorManager;
050:
051: import org.openide.nodes.Node;
052: import org.openide.util.NbBundle;
053: import org.openide.windows.IOProvider;
054: import org.openide.windows.InputOutput;
055: import org.openide.windows.OutputListener;
056: import org.openide.windows.OutputWriter;
057:
058: /**
059: * Presents search results in output window.
060: *
061: * @author Petr Kuzel
062: * @author Marian Petras
063: */
064: public final class SearchDisplayer {
065:
066: /** name of attribute "text to display in the Output Window" */
067: public static final String ATTR_OUTPUT_LINE = "output line"; //NOI18N
068: /** writer to that tab */
069: private OutputWriter ow = null;
070: /** */
071: private Reference<OutputWriter> owRef = null;
072:
073: /** Creates new SearchDisplayer */
074: SearchDisplayer() {
075: }
076:
077: /**
078: */
079: void prepareOutput() {
080: String tabName = NbBundle.getMessage(ResultView.class,
081: "TITLE_SEARCH_RESULTS"); //NOI18N
082: InputOutput searchIO = IOProvider.getDefault().getIO(tabName,
083: false);
084: ow = searchIO.getOut();
085: owRef = new WeakReference<OutputWriter>(ow);
086:
087: searchIO.select();
088: }
089:
090: /**
091: */
092: static void clearOldOutput(
093: final Reference<OutputWriter> outputWriterRef) {
094: if (outputWriterRef != null) {
095: OutputWriter oldWriter = outputWriterRef.get();
096: if (oldWriter != null) {
097: try {
098: oldWriter.reset();
099: } catch (IOException ex) {
100: ErrorManager.getDefault().notify(ex);
101: }
102: }
103: }
104: }
105:
106: /**
107: * Displays the given nodes.
108: *
109: * @param nodes nodes to display
110: */
111: void displayNodes(final Node[] nodes) {
112:
113: /* Prepare the output lines: */
114: final String[] outputLines = new String[nodes.length];
115: final OutputListener[] listeners = new OutputListener[nodes.length];
116:
117: for (int i = 0; i < nodes.length; i++) {
118: final Node node = nodes[i];
119: final Object o = node.getValue(ATTR_OUTPUT_LINE);
120: outputLines[i] = o instanceof String ? (String) o : node
121: .getShortDescription();
122: listeners[i] = node instanceof OutputListener ? (OutputListener) node
123: : null;
124: }
125:
126: /* Print the output lines: */
127: try {
128: EventQueue.invokeAndWait(new Runnable() {
129: public void run() {
130: try {
131: for (int i = 0; i < outputLines.length; i++) {
132: OutputListener listener = listeners[i];
133: if (listener != null) {
134: ow.println(outputLines[i], listener);
135: } else {
136: ow.println(outputLines[i]);
137: }
138: }
139: } catch (Exception ex) {
140: ErrorManager.getDefault().notify(
141: ErrorManager.EXCEPTION, ex);
142: }
143: }
144: });
145: } catch (Exception ex) {
146: ErrorManager.getDefault().notify(ex);
147: }
148: }
149:
150: /**
151: */
152: void finishDisplaying() {
153: ow.flush();
154: ow.close();
155: ow = null;
156: }
157:
158: /**
159: */
160: Reference<OutputWriter> getOutputWriterRef() {
161: return owRef;
162: }
163:
164: }
|