1 19 20 package org.netbeans.modules.editor.completion; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import javax.swing.JToolTip ; 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 39 40 public final class CompletionResultSetImpl { 41 42 static { 43 try { 45 Class.forName(CompletionResultSet.class.getName(), true, CompletionResultSetImpl.class.getClassLoader()); 46 } catch (ClassNotFoundException ex) {} 47 } 48 49 private static final CompletionSpiPackageAccessor spi 50 = CompletionSpiPackageAccessor.get(); 51 52 private final CompletionImpl completionImpl; 53 54 private final Object 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 title; 65 66 private String waitText; 67 68 private int anchorOffset; 69 70 private List <CompletionItem> items; 71 72 private boolean finished; 73 74 private CompletionDocumentation documentation; 75 76 private JToolTip toolTip; 77 78 private int estimatedItemCount; 79 80 private int estimatedItemWidth; 81 82 CompletionResultSetImpl(CompletionImpl completionImpl, 83 Object 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; this.estimatedItemCount = -1; this.active = true; 94 95 spi.createCompletionResultSet(this); 96 } 97 98 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 114 public CompletionTask getTask() { 115 return task; 116 } 117 118 123 public int getQueryType() { 124 return queryType; 125 } 126 127 131 public synchronized void markInactive() { 132 this.active = false; 133 } 134 135 public synchronized String getTitle() { 136 return title; 137 } 138 139 public synchronized void setTitle(String 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; } 159 160 if (items == null) { 161 int estSize = (estimatedItemCount == -1) ? 10 : estimatedItemCount; 162 items = new ArrayList <CompletionItem>(estSize); 163 } 164 items.add(item); 165 return items.size() < 1000; 166 } 167 168 public boolean addAllItems(Collection <? extends CompletionItem> items) { 169 boolean cont = true; 170 for (Iterator <? extends CompletionItem> it = items.iterator(); it.hasNext();) { 171 cont = addItem(it.next()); 172 } 173 return cont; 174 } 175 176 179 public synchronized List <? 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 getToolTip() { 197 return toolTip; 198 } 199 200 public synchronized void setToolTip(JToolTip 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 ("finish() already called"); } 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 getResultId() { 237 return resultId; 238 } 239 240 private void checkNotFinished() { 241 if (isFinished()) { 242 throw new IllegalStateException ("Result set already finished"); } 244 } 245 246 public synchronized String getWaitText() { 247 return waitText; 248 } 249 250 public synchronized void setWaitText(String waitText) { 251 this.waitText = waitText; 252 } 253 254 } 255 | Popular Tags |