KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > template > preferences > TemplateVariableProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.template.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.contentassist.ICompletionProposal;
21 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
24 import org.eclipse.jface.text.templates.TemplateContextType;
25 import org.eclipse.jface.text.templates.TemplateVariableResolver;
26
27
28
29 public class TemplateVariableProcessor implements IContentAssistProcessor {
30
31     private static Comparator JavaDoc fgTemplateVariableProposalComparator= new Comparator JavaDoc() {
32         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
33             TemplateVariableProposal proposal0= (TemplateVariableProposal) arg0;
34             TemplateVariableProposal proposal1= (TemplateVariableProposal) arg1;
35
36             return proposal0.getDisplayString().compareTo(proposal1.getDisplayString());
37         }
38
39         public boolean equals(Object JavaDoc arg0) {
40             return false;
41         }
42     };
43
44
45     /** the context type */
46     private TemplateContextType fContextType;
47
48     /**
49      * Sets the context type.
50      */

51     public void setContextType(TemplateContextType contextType) {
52         fContextType= contextType;
53     }
54
55     /**
56      * Gets the context type.
57      */

58     public TemplateContextType getContextType() {
59         return fContextType;
60     }
61
62     /*
63      * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
64      */

65     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
66
67         if (fContextType == null)
68             return null;
69
70         List JavaDoc proposals= new ArrayList JavaDoc();
71
72         String JavaDoc text= viewer.getDocument().get();
73         int start= getStart(text, documentOffset);
74         int end= documentOffset;
75
76         String JavaDoc string= text.substring(start, end);
77         int colon= string.indexOf(':');
78         boolean includeBrace= true;
79         int offset= start;
80         String JavaDoc prefix= string;
81         if (colon != -1) {
82             includeBrace= false;
83             offset= start + colon + 1;
84             prefix= string.substring(colon + 1);
85         } else {
86             int escape= string.indexOf("${"); //$NON-NLS-1$
87
if (escape != -1) {
88                 offset= start + escape + 2;
89                 includeBrace= false;
90                 prefix= string.substring(escape + 2);
91             }
92         }
93         if (prefix.equals("$")) //$NON-NLS-1$
94
prefix= ""; //$NON-NLS-1$
95

96         int length= end - offset;
97
98         for (Iterator JavaDoc iterator= fContextType.resolvers(); iterator.hasNext(); ) {
99             TemplateVariableResolver variable= (TemplateVariableResolver) iterator.next();
100
101             if (variable.getType().startsWith(prefix))
102                 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer, includeBrace));
103         }
104
105         Collections.sort(proposals, fgTemplateVariableProposalComparator);
106         return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
107     }
108
109     /* Guesses the start position of the completion */
110     private int getStart(String JavaDoc string, int end) {
111         int start= end;
112
113         if (start >= 1 && string.charAt(start - 1) == '$')
114             return start - 1;
115
116         while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
117             start--;
118         
119         if (start >= 1 && string.charAt(start - 1) == ':') {
120             start--;
121             while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
122                 start--;
123         }
124
125         if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$')
126             return start - 2;
127
128         return end;
129     }
130
131     /*
132      * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
133      */

134     public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
135         return null;
136     }
137
138     /*
139      * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
140      */

141     public char[] getCompletionProposalAutoActivationCharacters() {
142         return new char[] {'$'};
143     }
144
145     /*
146      * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
147      */

148     public char[] getContextInformationAutoActivationCharacters() {
149         return null;
150     }
151
152     /*
153      * @see IContentAssistProcessor#getErrorMessage()
154      */

155     public String JavaDoc getErrorMessage() {
156         return null;
157     }
158
159     /*
160      * @see IContentAssistProcessor#getContextInformationValidator()
161      */

162     public IContextInformationValidator getContextInformationValidator() {
163         return null;
164     }
165
166 }
167
168
Popular Tags