KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > completion > CompletionResultSetImpl


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 package org.netbeans.modules.editor.completion;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.JToolTip JavaDoc;
28 import org.netbeans.spi.editor.completion.CompletionDocumentation;
29 import org.netbeans.spi.editor.completion.CompletionItem;
30 import org.netbeans.spi.editor.completion.CompletionProvider;
31 import org.netbeans.spi.editor.completion.CompletionResultSet;
32 import org.netbeans.spi.editor.completion.CompletionTask;
33
34 /**
35  *
36  * @author Miloslav Metelka
37  * @version 1.00
38  */

39
40 public final class CompletionResultSetImpl {
41     
42     static {
43         // Ensure the SPI accessor gets assigned
44
try {
45             Class.forName(CompletionResultSet.class.getName(), true, CompletionResultSetImpl.class.getClassLoader());
46         } catch (ClassNotFoundException JavaDoc ex) {}
47     }
48     
49     private static final CompletionSpiPackageAccessor spi
50             = CompletionSpiPackageAccessor.get();
51
52     private final CompletionImpl completionImpl;
53     
54     private final Object JavaDoc resultId;
55     
56     private final CompletionTask task;
57     
58     private final int queryType;
59     
60     private CompletionResultSet resultSet;
61     
62     private boolean active;
63
64     private String JavaDoc title;
65     
66     private String JavaDoc waitText;
67     
68     private int anchorOffset;
69     
70     private List JavaDoc<CompletionItem> items;
71     
72     private boolean finished;
73     
74     private CompletionDocumentation documentation;
75     
76     private JToolTip JavaDoc toolTip;
77     
78     private int estimatedItemCount;
79     
80     private int estimatedItemWidth;
81     
82     CompletionResultSetImpl(CompletionImpl completionImpl,
83     Object JavaDoc resultId, CompletionTask task, int queryType) {
84         assert (completionImpl != null);
85         assert (resultId != null);
86         assert (task != null);
87         this.completionImpl = completionImpl;
88         this.resultId = resultId;
89         this.task = task;
90         this.queryType = queryType;
91         this.anchorOffset = -1; // not set
92
this.estimatedItemCount = -1; // not estimated
93
this.active = true;
94         
95         spi.createCompletionResultSet(this);
96     }
97     
98     /**
99      * Get the result set instance associated with this implementation.
100      */

101     public synchronized CompletionResultSet getResultSet() {
102         return resultSet;
103     }
104     
105     public synchronized void setResultSet(CompletionResultSet resultSet) {
106         assert (resultSet != null);
107         assert (this.resultSet == null);
108         this.resultSet = resultSet;
109     }
110     
111     /**
112      * Get the task associated with this result.
113      */

114     public CompletionTask getTask() {
115         return task;
116     }
117     
118     /**
119      * Get the query type to which this result set belongs.
120      * The results of other query types will be ignored when
121      * being set into this result set.
122      */

123     public int getQueryType() {
124         return queryType;
125     }
126     
127     /**
128      * Mark that results from this result set should no longer
129      * be taken into account.
130      */

131     public synchronized void markInactive() {
132         this.active = false;
133     }
134
135     public synchronized String JavaDoc getTitle() {
136         return title;
137     }
138
139     public synchronized void setTitle(String JavaDoc title) {
140         checkNotFinished();
141         this.title = title;
142     }
143     
144     public synchronized int getAnchorOffset() {
145         return anchorOffset;
146     }
147     
148     public synchronized void setAnchorOffset(int anchorOffset) {
149         checkNotFinished();
150         this.anchorOffset = anchorOffset;
151     }
152     
153     public synchronized boolean addItem(CompletionItem item) {
154         assert (item != null) : "Added item cannot be null";
155         checkNotFinished();
156         if (!active || (queryType & CompletionProvider.COMPLETION_QUERY_TYPE) == 0) {
157             return false; // signal do not add any further results
158
}
159
160         if (items == null) {
161             int estSize = (estimatedItemCount == -1) ? 10 : estimatedItemCount;
162             items = new ArrayList JavaDoc<CompletionItem>(estSize);
163         }
164         items.add(item);
165         return items.size() < 1000;
166     }
167     
168     public boolean addAllItems(Collection JavaDoc<? extends CompletionItem> items) {
169         boolean cont = true;
170         for (Iterator JavaDoc<? extends CompletionItem> it = items.iterator(); it.hasNext();) {
171             cont = addItem(it.next());
172         }
173         return cont;
174     }
175     
176     /**
177      * @return non-null list of items.
178      */

179     public synchronized List JavaDoc<? extends CompletionItem> getItems() {
180         assert isFinished() : "Adding not finished";
181         return (items != null) ? items : Collections.<CompletionItem>emptyList();
182     }
183     
184     public synchronized void setDocumentation(CompletionDocumentation documentation) {
185         checkNotFinished();
186         if (!active || queryType != CompletionProvider.DOCUMENTATION_QUERY_TYPE) {
187             return;
188         }
189         this.documentation = documentation;
190     }
191     
192     public synchronized CompletionDocumentation getDocumentation() {
193         return documentation;
194     }
195     
196     public synchronized JToolTip JavaDoc getToolTip() {
197         return toolTip;
198     }
199     
200     public synchronized void setToolTip(JToolTip JavaDoc toolTip) {
201         checkNotFinished();
202         if (!active || queryType != CompletionProvider.TOOLTIP_QUERY_TYPE) {
203             return;
204         }
205         this.toolTip = toolTip;
206     }
207     
208     public synchronized boolean isFinished() {
209         return finished;
210     }
211     
212     public void finish() {
213         synchronized (this) {
214             if (finished) {
215                 throw new IllegalStateException JavaDoc("finish() already called"); // NOI18N
216
}
217             finished = true;
218         }
219
220         completionImpl.finishNotify(this);
221     }
222     
223     public int getSortType() {
224         return completionImpl.getSortType();
225     }
226     
227     public synchronized void estimateItems(int estimatedItemCount, int estimatedItemWidth) {
228         this.estimatedItemCount = estimatedItemCount;
229         this.estimatedItemWidth = estimatedItemWidth;
230     }
231     
232     CompletionImpl getCompletionImpl() {
233         return completionImpl;
234     }
235     
236     Object JavaDoc getResultId() {
237         return resultId;
238     }
239     
240     private void checkNotFinished() {
241         if (isFinished()) {
242             throw new IllegalStateException JavaDoc("Result set already finished"); // NOI18N
243
}
244     }
245
246     public synchronized String JavaDoc getWaitText() {
247         return waitText;
248     }
249
250     public synchronized void setWaitText(String JavaDoc waitText) {
251         this.waitText = waitText;
252     }
253
254 }
255
Popular Tags