1 11 package org.eclipse.jdt.internal.ui.text; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.LinkedHashSet ; 17 import java.util.LinkedList ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import org.eclipse.swt.graphics.Image; 22 23 import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor; 24 import org.eclipse.jface.contentassist.IContentAssistSubjectControl; 25 import org.eclipse.jface.contentassist.ISubjectControlContextInformationValidator; 26 27 import org.eclipse.jface.text.Assert; 28 import org.eclipse.jface.text.ITextViewer; 29 import org.eclipse.jface.text.contentassist.ICompletionProposal; 30 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 31 import org.eclipse.jface.text.contentassist.IContextInformation; 32 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 33 34 35 41 public class CompoundContentAssistProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor { 42 43 private static class WrappedContextInformation implements IContextInformation { 44 private IContextInformation fInfo; 45 private IContentAssistProcessor fProcessor; 46 47 WrappedContextInformation(IContextInformation info, IContentAssistProcessor processor) { 48 fInfo= info; 49 fProcessor= processor; 50 } 51 54 public boolean equals(Object obj) { 55 return fInfo.equals(obj); 56 } 57 60 public String getContextDisplayString() { 61 return fInfo.getContextDisplayString(); 62 } 63 66 public Image getImage() { 67 return fInfo.getImage(); 68 } 69 72 public String getInformationDisplayString() { 73 return fInfo.getInformationDisplayString(); 74 } 75 78 public int hashCode() { 79 return fInfo.hashCode(); 80 } 81 84 public String toString() { 85 return fInfo.toString(); 86 } 87 88 IContentAssistProcessor getProcessor() { 89 return fProcessor; 90 } 91 } 92 93 private static class CompoundContentAssistValidator implements IContextInformationValidator { 94 private List fValidators= new ArrayList (); 95 96 void add(IContextInformationValidator validator) { 97 fValidators.add(validator); 98 } 99 100 103 public void install(IContextInformation info, ITextViewer viewer, int documentPosition) { 104 IContextInformationValidator validator= getValidator(info); 105 if (validator != null) 106 validator.install(info, viewer, documentPosition); 107 } 108 109 IContextInformationValidator getValidator(IContextInformation info) { 110 if (info instanceof WrappedContextInformation) { 111 WrappedContextInformation wrap= (WrappedContextInformation) info; 112 return wrap.getProcessor().getContextInformationValidator(); 113 } 114 115 return null; 116 } 117 118 121 public boolean isContextInformationValid(int documentPosition) { 122 boolean isValid= false; 123 for (Iterator it= fValidators.iterator(); it.hasNext();) { 124 IContextInformationValidator v= (IContextInformationValidator) it.next(); 125 isValid |= v.isContextInformationValid(documentPosition); 126 } 127 return isValid; 128 } 129 130 } 131 132 private static class CompoundContentAssistValidatorEx extends CompoundContentAssistValidator implements ISubjectControlContextInformationValidator { 133 134 137 public void install(IContextInformation info, IContentAssistSubjectControl contentAssistSubjectControl, int documentPosition) { 138 IContextInformationValidator validator= getValidator(info); 139 if (validator instanceof ISubjectControlContextInformationValidator) 140 ((ISubjectControlContextInformationValidator) validator).install(info, contentAssistSubjectControl, documentPosition); 141 } 142 143 } 144 145 private final Set fProcessors= new LinkedHashSet (); 146 147 150 public CompoundContentAssistProcessor() { 151 } 152 153 158 public CompoundContentAssistProcessor(IContentAssistProcessor processor) { 159 add(processor); 160 } 161 162 167 public void add(IContentAssistProcessor processor) { 168 Assert.isNotNull(processor); 169 fProcessors.add(processor); 170 } 171 172 177 public void remove(IContentAssistProcessor processor) { 178 fProcessors.remove(processor); 179 } 180 181 186 public CompoundContentAssistProcessor(IContentAssistProcessor[] processors) { 187 for (int i= 0; i < processors.length; i++) { 188 add(processors[i]); 189 } 190 } 191 192 195 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 196 List ret= new LinkedList (); 197 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 198 IContentAssistProcessor p= (IContentAssistProcessor) it.next(); 199 ICompletionProposal[] proposals= p.computeCompletionProposals(viewer, documentOffset); 200 if (proposals != null) 201 ret.addAll(Arrays.asList(proposals)); 202 } 203 return (ICompletionProposal[]) ret.toArray(new ICompletionProposal[ret.size()]); 204 } 205 206 214 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 215 List ret= new LinkedList (); 216 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 217 IContentAssistProcessor p= (IContentAssistProcessor) it.next(); 218 IContextInformation[] informations= p.computeContextInformation(viewer, documentOffset); 219 if (informations != null) 220 for (int i= 0; i < informations.length; i++) 221 ret.add(new WrappedContextInformation(informations[i], p)); 222 } 223 return (IContextInformation[]) ret.toArray(new IContextInformation[ret.size()]); 224 } 225 226 229 public char[] getCompletionProposalAutoActivationCharacters() { 230 Set ret= new LinkedHashSet (); 231 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 232 IContentAssistProcessor p= (IContentAssistProcessor) it.next(); 233 char[] chars= p.getCompletionProposalAutoActivationCharacters(); 234 if (chars != null) 235 for (int i = 0; i < chars.length; i++) 236 ret.add(new Character (chars[i])); 237 } 238 239 char[] chars= new char[ret.size()]; 240 int i= 0; 241 for (Iterator it = ret.iterator(); it.hasNext(); i++) { 242 Character ch = (Character ) it.next(); 243 chars[i]= ch.charValue(); 244 } 245 return chars; 246 } 247 248 251 public char[] getContextInformationAutoActivationCharacters() { 252 Set ret= new LinkedHashSet (); 253 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 254 IContentAssistProcessor p= (IContentAssistProcessor) it.next(); 255 char[] chars= p.getContextInformationAutoActivationCharacters(); 256 if (chars != null) 257 for (int i = 0; i < chars.length; i++) 258 ret.add(new Character (chars[i])); 259 } 260 261 char[] chars= new char[ret.size()]; 262 int i= 0; 263 for (Iterator it = ret.iterator(); it.hasNext(); i++) { 264 Character ch = (Character ) it.next(); 265 chars[i]= ch.charValue(); 266 } 267 return chars; 268 } 269 270 277 public String getErrorMessage() { 278 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 279 IContentAssistProcessor p= (IContentAssistProcessor) it.next(); 280 String err= p.getErrorMessage(); 281 if (err != null) 282 return err; 283 } 284 return null; 285 } 286 287 296 public IContextInformationValidator getContextInformationValidator() { 297 boolean hasValidator= false; 298 boolean hasExtension= false; 299 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 300 IContentAssistProcessor p= (IContentAssistProcessor) it.next(); 301 IContextInformationValidator v= p.getContextInformationValidator(); 302 if (v instanceof ISubjectControlContextInformationValidator) { 303 hasExtension= true; 304 break; 305 } else if (v != null) { 306 hasValidator= true; 307 } 308 } 309 310 CompoundContentAssistValidator validator= null; 311 if (hasExtension) 312 validator= new CompoundContentAssistValidatorEx(); 313 else if (hasValidator) 314 validator= new CompoundContentAssistValidator(); 315 316 if (validator != null) 317 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 318 IContentAssistProcessor p= (IContentAssistProcessor) it.next(); 319 IContextInformationValidator v= p.getContextInformationValidator(); 320 if (v != null) 321 validator.add(v); 322 } 323 324 return validator; 325 } 326 327 330 public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 331 List ret= new LinkedList (); 332 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 333 Object o= it.next(); 334 if (o instanceof ISubjectControlContentAssistProcessor) { 335 ISubjectControlContentAssistProcessor p= (ISubjectControlContentAssistProcessor) o; 336 ICompletionProposal[] proposals= p.computeCompletionProposals(contentAssistSubjectControl, documentOffset); 337 if (proposals != null) 338 ret.addAll(Arrays.asList(proposals)); 339 } 340 } 341 342 return (ICompletionProposal[]) ret.toArray(new ICompletionProposal[ret.size()]); 343 } 344 345 353 public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 354 List ret= new LinkedList (); 355 for (Iterator it= fProcessors.iterator(); it.hasNext();) { 356 Object o= it.next(); 357 if (o instanceof ISubjectControlContentAssistProcessor) { 358 ISubjectControlContentAssistProcessor p= (ISubjectControlContentAssistProcessor) o; 359 IContextInformation[] informations= p.computeContextInformation(contentAssistSubjectControl, documentOffset); 360 if (informations != null) 361 for (int i= 0; i < informations.length; i++) 362 ret.add(new WrappedContextInformation(informations[i], p)); 363 } 364 } 365 return (IContextInformation[]) ret.toArray(new IContextInformation[ret.size()]); 366 } 367 368 } 369 | Popular Tags |