KickJava   Java API By Example, From Geeks To Geeks.

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


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 2004 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.search;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
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 /**
31  *
32  * @author Marian Petras
33  */

34 public class PrintDetailsTask implements Runnable JavaDoc {
35
36     /** */
37     private static final int BUFFER_SIZE = 8;
38     /** */
39     private final Object JavaDoc[] objects;
40     /** */
41     private final SearchGroup searchGroup;
42     /** */
43     private final Node[] buffer = new Node[BUFFER_SIZE];
44     /** position of the first free item in the buffer */
45     private int bufPos = 0;
46     /** */
47     private SearchDisplayer displayer;
48     /** */
49     private volatile boolean interrupted = false;
50     
51     
52     /** Creates a new instance of PrintDetailsTask */
53     public PrintDetailsTask(final Object JavaDoc[] 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"); //NOI18N
63

64         final SearchType[] searchTypes = searchGroup.getSearchTypes();
65         
66         int freeBufSpace = 0;
67         for (int i = 0; i < objects.length; i++) {
68
69             /* Collect details about the found node: */
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             /* Print the collected details: */
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         /*
110          * We must call this even if this task is interrupted. We must close
111          * the output window.
112          */

113         callDisplayerFromAWT("finishDisplaying");
114     }
115
116     /**
117      * Stops this search task.
118      *
119      * @see #stop(boolean)
120      */

121     void stop() {
122         interrupted = true;
123     }
124     
125     /**
126      */

127     public Reference JavaDoc getOutputWriterRef() {
128         return displayer.getOutputWriterRef();
129     }
130
131     /**
132      * Adds some or all of the given nodes to the buffer.
133      * Nodes at position lesser than the given start index are ignored.
134      * If the nodes to be added do not fit all into the buffer, the remaining
135      * nodes are ignored.
136      *
137      * @param detailNodes array containing nodes to be added
138      * @param firstIndex index of the first node to be added to the buffer
139      * @return positive number expressing number of free items in the buffer;
140      * negative number expressing number of remaining nodes that
141      * did not fit into the buffer;
142      * or <code>0</code> if the nodes exactly filled the buffer
143      */

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     /**
160      */

161     private void printBuffer() {
162         displayer.displayNodes(buffer);
163     }
164
165     /**
166      */

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     /**
180      */

181     private void callDisplayerFromAWT(final String JavaDoc methodName) {
182         try {
183             final Method JavaDoc method = SearchDisplayer.class
184                                   .getDeclaredMethod(methodName, new Class JavaDoc[0]);
185             Runnable JavaDoc runnable = new Runnable JavaDoc() {
186                 public void run() {
187                     try {
188                         method.invoke(displayer, (Object JavaDoc[]) null);
189                     } catch (Exception JavaDoc 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 JavaDoc ex) {
200             ErrorManager.getDefault().notify(ex);
201         }
202     }
203     
204 }
205
Popular Tags