001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor.templates;
011:
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.Comparator;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import org.eclipse.jface.text.ITextViewer;
019: import org.eclipse.jface.text.contentassist.ICompletionProposal;
020: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
021: import org.eclipse.jface.text.contentassist.IContextInformation;
022: import org.eclipse.jface.text.contentassist.IContextInformationValidator;
023: import org.eclipse.jface.text.templates.TemplateContextType;
024: import org.eclipse.jface.text.templates.TemplateVariableResolver;
025:
026: /**
027: * A content assist processor for template variables.
028: * <p>
029: * This class should not be used by clients and may become package visible in
030: * the future.
031: * </p>
032: *
033: * @since 3.0
034: */
035: final class TemplateVariableProcessor implements
036: IContentAssistProcessor {
037:
038: private static Comparator fgTemplateVariableProposalComparator = new Comparator() {
039: public int compare(Object arg0, Object arg1) {
040: TemplateVariableProposal proposal0 = (TemplateVariableProposal) arg0;
041: TemplateVariableProposal proposal1 = (TemplateVariableProposal) arg1;
042:
043: return proposal0.getDisplayString().compareTo(
044: proposal1.getDisplayString());
045: }
046:
047: /*
048: * @see java.lang.Object#equals(java.lang.Object)
049: */
050: public boolean equals(Object arg0) {
051: return false;
052: }
053:
054: /*
055: * Returns Object#hashCode.
056: * @see java.lang.Object#hashCode()
057: */
058: public int hashCode() {
059: return super .hashCode();
060: }
061: };
062:
063: /** the context type */
064: private TemplateContextType fContextType;
065:
066: /**
067: * Sets the context type.
068: *
069: * @param contextType the context type for this processor
070: */
071: public void setContextType(TemplateContextType contextType) {
072: fContextType = contextType;
073: }
074:
075: /**
076: * Returns the context type.
077: *
078: * @return the context type
079: */
080: public TemplateContextType getContextType() {
081: return fContextType;
082: }
083:
084: /*
085: * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
086: */
087: public ICompletionProposal[] computeCompletionProposals(
088: ITextViewer viewer, int documentOffset) {
089:
090: if (fContextType == null)
091: return null;
092:
093: List proposals = new ArrayList();
094:
095: String text = viewer.getDocument().get();
096: int start = getStart(text, documentOffset);
097: int end = documentOffset;
098:
099: String string = text.substring(start, end);
100: String prefix = (string.length() >= 2) ? string.substring(2)
101: : null;
102:
103: int offset = start;
104: int length = end - start;
105:
106: for (Iterator iterator = fContextType.resolvers(); iterator
107: .hasNext();) {
108: TemplateVariableResolver variable = (TemplateVariableResolver) iterator
109: .next();
110:
111: if (prefix == null || variable.getType().startsWith(prefix))
112: proposals.add(new TemplateVariableProposal(variable,
113: offset, length, viewer));
114: }
115:
116: Collections.sort(proposals,
117: fgTemplateVariableProposalComparator);
118: return (ICompletionProposal[]) proposals
119: .toArray(new ICompletionProposal[proposals.size()]);
120: }
121:
122: /* Guesses the start position of the completion */
123: private int getStart(String string, int end) {
124: int start = end;
125:
126: if (start >= 1 && string.charAt(start - 1) == '$')
127: return start - 1;
128:
129: while ((start != 0)
130: && Character.isUnicodeIdentifierPart(string
131: .charAt(start - 1)))
132: start--;
133:
134: if (start >= 2 && string.charAt(start - 1) == '{'
135: && string.charAt(start - 2) == '$')
136: return start - 2;
137:
138: return end;
139: }
140:
141: /*
142: * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
143: */
144: public IContextInformation[] computeContextInformation(
145: ITextViewer viewer, int documentOffset) {
146: return null;
147: }
148:
149: /*
150: * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
151: */
152: public char[] getCompletionProposalAutoActivationCharacters() {
153: return new char[] { '$' };
154: }
155:
156: /*
157: * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
158: */
159: public char[] getContextInformationAutoActivationCharacters() {
160: return null;
161: }
162:
163: /*
164: * @see IContentAssistProcessor#getErrorMessage()
165: */
166: public String getErrorMessage() {
167: return null;
168: }
169:
170: /*
171: * @see IContentAssistProcessor#getContextInformationValidator()
172: */
173: public IContextInformationValidator getContextInformationValidator() {
174: return null;
175: }
176:
177: }
|