KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > templates > TemplateVariableProcessor


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.ui.texteditor.templates;
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 /**
30  * A content assist processor for template variables.
31  * <p>
32  * This class should not be used by clients and may become package visible in
33  * the future.
34  * </p>
35  *
36  * @since 3.0
37  */

38 final class TemplateVariableProcessor implements IContentAssistProcessor {
39
40     private static Comparator JavaDoc fgTemplateVariableProposalComparator= new Comparator JavaDoc() {
41         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
42             TemplateVariableProposal proposal0= (TemplateVariableProposal) arg0;
43             TemplateVariableProposal proposal1= (TemplateVariableProposal) arg1;
44
45             return proposal0.getDisplayString().compareTo(proposal1.getDisplayString());
46         }
47
48         /*
49          * @see java.lang.Object#equals(java.lang.Object)
50          */

51         public boolean equals(Object JavaDoc arg0) {
52             return false;
53         }
54
55         /*
56          * Returns Object#hashCode.
57          * @see java.lang.Object#hashCode()
58          */

59         public int hashCode() {
60             return super.hashCode();
61         }
62     };
63
64
65     /** the context type */
66     private TemplateContextType fContextType;
67
68     /**
69      * Sets the context type.
70      *
71      * @param contextType the context type for this processor
72      */

73     public void setContextType(TemplateContextType contextType) {
74         fContextType= contextType;
75     }
76
77     /**
78      * Returns the context type.
79      *
80      * @return the context type
81      */

82     public TemplateContextType getContextType() {
83         return fContextType;
84     }
85
86     /*
87      * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
88      */

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

138     public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
139         return null;
140     }
141
142     /*
143      * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
144      */

145     public char[] getCompletionProposalAutoActivationCharacters() {
146         return new char[] {'$'};
147     }
148
149     /*
150      * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
151      */

152     public char[] getContextInformationAutoActivationCharacters() {
153         return null;
154     }
155
156     /*
157      * @see IContentAssistProcessor#getErrorMessage()
158      */

159     public String JavaDoc getErrorMessage() {
160         return null;
161     }
162
163     /*
164      * @see IContentAssistProcessor#getContextInformationValidator()
165      */

166     public IContextInformationValidator getContextInformationValidator() {
167         return null;
168     }
169
170 }
171
172
Popular Tags