1 19 20 21 package org.netbeans.modules.search; 22 23 import java.awt.EventQueue ; 24 25 import java.io.IOException ; 26 import java.lang.ref.Reference ; 27 import java.lang.ref.WeakReference ; 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 44 public final class SearchDisplayer { 45 46 47 public static final String ATTR_OUTPUT_LINE = "output line"; 49 private OutputWriter ow = null; 50 51 private Reference owRef = null; 52 53 54 SearchDisplayer() { 55 } 56 57 59 void prepareOutput() { 60 String tabName = NbBundle.getMessage(ResultView.class, 61 "TITLE_SEARCH_RESULTS"); InputOutput searchIO = IOProvider.getDefault().getIO(tabName, false); 63 ow = searchIO.getOut(); 64 owRef = new WeakReference (ow); 65 66 searchIO.select(); 67 } 68 69 71 static void clearOldOutput(final Reference outputWriterRef) { 72 if (outputWriterRef != null) { 73 OutputWriter oldWriter = (OutputWriter) outputWriterRef.get(); 74 if (oldWriter != null) { 75 try { 76 oldWriter.reset(); 77 } catch (IOException ex) { 78 ErrorManager.getDefault().notify(ex); 79 } 80 } 81 } 82 } 83 84 89 void displayNodes(final Node[] nodes) { 90 91 92 final String [] outputLines = new String [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 o = node.getValue(ATTR_OUTPUT_LINE); 98 outputLines[i] = o instanceof String ? (String ) o 99 : node.getShortDescription(); 100 listeners[i] = node instanceof OutputListener ? (OutputListener)node 101 : null; 102 } 103 104 105 try { 106 EventQueue.invokeAndWait(new Runnable () { 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 ex) { 118 ErrorManager.getDefault() 119 .notify(ErrorManager.EXCEPTION, ex); 120 } 121 } 122 }); 123 } catch (Exception ex) { 124 ErrorManager.getDefault().notify(ex); 125 } 126 } 127 128 130 void finishDisplaying() { 131 ow.flush(); 132 ow.close(); 133 ow = null; 134 } 135 136 138 Reference getOutputWriterRef() { 139 return owRef; 140 } 141 142 } 143 | Popular Tags |