1 19 20 package org.netbeans.modules.search; 21 22 import java.awt.EventQueue ; 23 import java.lang.ref.Reference ; 24 import java.lang.reflect.Method ; 25 import org.openide.ErrorManager; 26 import org.openide.nodes.Node; 27 import org.openidex.search.SearchGroup; 28 import org.openidex.search.SearchType; 29 30 34 public class PrintDetailsTask implements Runnable { 35 36 37 private static final int BUFFER_SIZE = 8; 38 39 private final Object [] objects; 40 41 private final SearchGroup searchGroup; 42 43 private final Node[] buffer = new Node[BUFFER_SIZE]; 44 45 private int bufPos = 0; 46 47 private SearchDisplayer displayer; 48 49 private volatile boolean interrupted = false; 50 51 52 53 public PrintDetailsTask(final Object [] matchingObjects, 54 final SearchGroup searchGroup) { 55 this.objects = matchingObjects; 56 this.searchGroup = searchGroup; 57 } 58 59 60 public void run() { 61 displayer = new SearchDisplayer(); 62 callDisplayerFromAWT("prepareOutput"); 64 final SearchType[] searchTypes = searchGroup.getSearchTypes(); 65 66 int freeBufSpace = 0; 67 for (int i = 0; i < objects.length; i++) { 68 69 70 Node[] allDetails = null; 71 for (int j = 0; j < searchTypes.length; j++) { 72 Node[] details = searchTypes[j].getDetails(objects[i]); 73 if (details == null || details.length == 0) { 74 continue; 75 } 76 if (allDetails == null) { 77 allDetails = details; 78 } else { 79 allDetails = concatNodeArrays(allDetails, details); 80 } 81 } 82 if (allDetails == null) { 83 continue; 84 } 85 86 87 freeBufSpace = addToBuffer(allDetails, 0); 88 while (freeBufSpace < 0) { 89 printBuffer(); 90 91 int remainderIndex = allDetails.length + freeBufSpace; 92 freeBufSpace = addToBuffer(allDetails, remainderIndex); 93 } 94 if (freeBufSpace == 0) { 95 printBuffer(); 96 } 97 98 if (interrupted) { 99 break; 100 } 101 } 102 if ((freeBufSpace != 0) && !interrupted) { 103 int smallBufSize = BUFFER_SIZE - freeBufSpace; 104 Node[] smallBuffer = new Node[smallBufSize]; 105 System.arraycopy(buffer, 0, smallBuffer, 0, smallBufSize); 106 displayer.displayNodes(smallBuffer); 107 } 108 109 113 callDisplayerFromAWT("finishDisplaying"); 114 } 115 116 121 void stop() { 122 interrupted = true; 123 } 124 125 127 public Reference getOutputWriterRef() { 128 return displayer.getOutputWriterRef(); 129 } 130 131 144 private int addToBuffer(Node[] detailNodes, int firstIndex) { 145 assert firstIndex >=0 && firstIndex <= detailNodes.length; 146 147 int nodesToAddCount = detailNodes.length - firstIndex; 148 int newBufPos = bufPos + nodesToAddCount; 149 int remainingSpace = BUFFER_SIZE - newBufPos; 150 if (remainingSpace <= 0) { 151 nodesToAddCount += remainingSpace; 152 newBufPos = 0; 153 } 154 System.arraycopy(detailNodes, firstIndex, buffer, bufPos, nodesToAddCount); 155 bufPos = newBufPos; 156 return remainingSpace; 157 } 158 159 161 private void printBuffer() { 162 displayer.displayNodes(buffer); 163 } 164 165 167 private Node[] concatNodeArrays(Node[] arrA, Node[] arrB) { 168 Node[] result = new Node[arrA.length + arrB.length]; 169 170 System.arraycopy(arrA, 0, 171 result, 0, 172 arrA.length); 173 System.arraycopy(arrB, 0, 174 result, arrA.length, 175 arrB.length); 176 return result; 177 } 178 179 181 private void callDisplayerFromAWT(final String methodName) { 182 try { 183 final Method method = SearchDisplayer.class 184 .getDeclaredMethod(methodName, new Class [0]); 185 Runnable runnable = new Runnable () { 186 public void run() { 187 try { 188 method.invoke(displayer, (Object []) null); 189 } catch (Exception ex) { 190 ErrorManager.getDefault().notify(ex); 191 } 192 } 193 }; 194 if (EventQueue.isDispatchThread()) { 195 runnable.run(); 196 } else { 197 EventQueue.invokeAndWait(runnable); 198 } 199 } catch (Exception ex) { 200 ErrorManager.getDefault().notify(ex); 201 } 202 } 203 204 } 205 | Popular Tags |