KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > CompoundContentAssistProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedHashSet JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
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 /**
36  * A processor that aggregates the proposals of multiple other processors. When proposals are requested,
37  * the contained processors are queried in the order they were added to the compound object.
38  *
39  * @since 3.0
40  */

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         /*
52          * @see java.lang.Object#equals(java.lang.Object)
53          */

54         public boolean equals(Object JavaDoc obj) {
55             return fInfo.equals(obj);
56         }
57         /*
58          * @see org.eclipse.jface.text.contentassist.IContextInformation#getContextDisplayString()
59          */

60         public String JavaDoc getContextDisplayString() {
61             return fInfo.getContextDisplayString();
62         }
63         /*
64          * @see org.eclipse.jface.text.contentassist.IContextInformation#getImage()
65          */

66         public Image getImage() {
67             return fInfo.getImage();
68         }
69         /*
70          * @see org.eclipse.jface.text.contentassist.IContextInformation#getInformationDisplayString()
71          */

72         public String JavaDoc getInformationDisplayString() {
73             return fInfo.getInformationDisplayString();
74         }
75         /*
76          * @see java.lang.Object#hashCode()
77          */

78         public int hashCode() {
79             return fInfo.hashCode();
80         }
81         /*
82          * @see java.lang.Object#toString()
83          */

84         public String JavaDoc 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 JavaDoc fValidators= new ArrayList JavaDoc();
95
96         void add(IContextInformationValidator validator) {
97             fValidators.add(validator);
98         }
99
100         /*
101          * @see org.eclipse.jface.text.contentassist.IContextInformationValidator#install(org.eclipse.jface.text.contentassist.IContextInformation, org.eclipse.jface.text.ITextViewer, int)
102          */

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         /*
119          * @see org.eclipse.jface.text.contentassist.IContextInformationValidator#isContextInformationValid(int)
120          */

121         public boolean isContextInformationValid(int documentPosition) {
122             boolean isValid= false;
123             for (Iterator JavaDoc 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         /*
135          * @see ISubjectControlContextInformationValidator#install(IContextInformation, IContentAssistSubjectControl, int)
136          */

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 JavaDoc fProcessors= new LinkedHashSet JavaDoc();
146
147     /**
148      * Creates a new instance.
149      */

150     public CompoundContentAssistProcessor() {
151     }
152
153     /**
154      * Creates a new instance with one child processor.
155      *
156      * @param processor the processor to add
157      */

158     public CompoundContentAssistProcessor(IContentAssistProcessor processor) {
159         add(processor);
160     }
161
162     /**
163      * Adds a processor to this compound processor.
164      *
165      * @param processor the processor to add
166      */

167     public void add(IContentAssistProcessor processor) {
168         Assert.isNotNull(processor);
169         fProcessors.add(processor);
170     }
171
172     /**
173      * Removes a processor from this compound processor.
174      *
175      * @param processor the processor to remove
176      */

177     public void remove(IContentAssistProcessor processor) {
178         fProcessors.remove(processor);
179     }
180
181     /**
182      * Creates a new instance and adds all specified processors.
183      *
184      * @param processors
185      */

186     public CompoundContentAssistProcessor(IContentAssistProcessor[] processors) {
187         for (int i= 0; i < processors.length; i++) {
188             add(processors[i]);
189         }
190     }
191
192     /*
193      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
194      */

195     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
196         List JavaDoc ret= new LinkedList JavaDoc();
197         for (Iterator JavaDoc 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     /**
207      * {@inheritDoc}
208      * <p>
209      * The returned objects are wrapper objects around the real information containers.
210      * </p>
211      *
212      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
213      */

214     public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
215         List JavaDoc ret= new LinkedList JavaDoc();
216         for (Iterator JavaDoc 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     /*
227      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
228      */

229     public char[] getCompletionProposalAutoActivationCharacters() {
230         Set JavaDoc ret= new LinkedHashSet JavaDoc();
231         for (Iterator JavaDoc 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 JavaDoc(chars[i]));
237         }
238
239         char[] chars= new char[ret.size()];
240         int i= 0;
241         for (Iterator JavaDoc it = ret.iterator(); it.hasNext(); i++) {
242             Character JavaDoc ch = (Character JavaDoc) it.next();
243             chars[i]= ch.charValue();
244         }
245         return chars;
246     }
247
248     /*
249      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
250      */

251     public char[] getContextInformationAutoActivationCharacters() {
252         Set JavaDoc ret= new LinkedHashSet JavaDoc();
253         for (Iterator JavaDoc 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 JavaDoc(chars[i]));
259         }
260
261         char[] chars= new char[ret.size()];
262         int i= 0;
263         for (Iterator JavaDoc it = ret.iterator(); it.hasNext(); i++) {
264             Character JavaDoc ch = (Character JavaDoc) it.next();
265             chars[i]= ch.charValue();
266         }
267         return chars;
268     }
269
270     /**
271      * Returns the first non- <code>null</code> error message of any contained
272      * processor, or <code>null</code> if no processor has an error message.
273      *
274      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
275      * @return {@inheritDoc}
276      */

277     public String JavaDoc getErrorMessage() {
278         for (Iterator JavaDoc it= fProcessors.iterator(); it.hasNext();) {
279             IContentAssistProcessor p= (IContentAssistProcessor) it.next();
280             String JavaDoc err= p.getErrorMessage();
281             if (err != null)
282                 return err;
283         }
284         return null;
285     }
286
287     /**
288      * {@inheritDoc}
289      * <p>
290      * The returned validator is a wrapper around the validators provided by the
291      * child processors.
292      * </p>
293      *
294      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
295      */

296     public IContextInformationValidator getContextInformationValidator() {
297         boolean hasValidator= false;
298         boolean hasExtension= false;
299         for (Iterator JavaDoc 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 JavaDoc 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     /*
328      * @see ISubjectControlContentAssistProcessor#computeCompletionProposals(IContentAssistSubjectControl, int)
329      */

330     public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) {
331         List JavaDoc ret= new LinkedList JavaDoc();
332         for (Iterator JavaDoc it= fProcessors.iterator(); it.hasNext();) {
333             Object JavaDoc 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     /**
346      * {@inheritDoc}
347      * <p>
348      * The returned objects are wrapper objects around the real information containers.
349      * </p>
350      *
351      * @see org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.contentassist.IContentAssistSubject, int)
352      */

353     public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) {
354         List JavaDoc ret= new LinkedList JavaDoc();
355         for (Iterator JavaDoc it= fProcessors.iterator(); it.hasNext();) {
356             Object JavaDoc 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