KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > template > contentassist > TemplateEngine


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.contentassist;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Map.Entry;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.swt.graphics.Point;
22
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.IRegion;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.Position;
28 import org.eclipse.jface.text.Region;
29 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
30 import org.eclipse.jface.text.templates.Template;
31 import org.eclipse.jface.text.templates.TemplateContextType;
32
33 import org.eclipse.jdt.core.ICompilationUnit;
34
35 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext;
36 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.JavaPluginImages;
40
41 public class TemplateEngine {
42
43     private static final String JavaDoc $_LINE_SELECTION= "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
44
private static final String JavaDoc $_WORD_SELECTION= "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
45

46     /** The context type. */
47     private TemplateContextType fContextType;
48     /** The result proposals. */
49     private ArrayList JavaDoc fProposals= new ArrayList JavaDoc();
50     /** Positions created on the key documents to remove in reset. */
51     private final Map JavaDoc fPositions= new HashMap JavaDoc();
52
53     /**
54      * Creates the template engine for a particular context type.
55      * See <code>TemplateContext</code> for supported context types.
56      */

57     public TemplateEngine(TemplateContextType contextType) {
58         Assert.isNotNull(contextType);
59         fContextType= contextType;
60     }
61
62     /**
63      * Empties the collector.
64      */

65     public void reset() {
66         fProposals.clear();
67         for (Iterator JavaDoc it= fPositions.entrySet().iterator(); it.hasNext();) {
68             Entry entry= (Entry) it.next();
69             IDocument doc= (IDocument) entry.getKey();
70             Position position= (Position) entry.getValue();
71             doc.removePosition(position);
72         }
73         fPositions.clear();
74     }
75
76     /**
77      * Returns the array of matching templates.
78      */

79     public TemplateProposal[] getResults() {
80         return (TemplateProposal[]) fProposals.toArray(new TemplateProposal[fProposals.size()]);
81     }
82
83     /**
84      * Inspects the context of the compilation unit around <code>completionPosition</code>
85      * and feeds the collector with proposals.
86      * @param viewer the text viewer
87      * @param completionPosition the context position in the document of the text viewer
88      * @param compilationUnit the compilation unit (may be <code>null</code>)
89      */

90     public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
91         IDocument document= viewer.getDocument();
92
93         if (!(fContextType instanceof CompilationUnitContextType))
94             return;
95
96         Point selection= viewer.getSelectedRange();
97         Position position= new Position(completionPosition, selection.y);
98
99         // remember selected text
100
String JavaDoc selectedText= null;
101         if (selection.y != 0) {
102             try {
103                 selectedText= document.get(selection.x, selection.y);
104                 document.addPosition(position);
105                 fPositions.put(document, position);
106             } catch (BadLocationException e) {}
107         }
108
109         CompilationUnitContext context= ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
110         context.setVariable("selection", selectedText); //$NON-NLS-1$
111
int start= context.getStart();
112         int end= context.getEnd();
113         IRegion region= new Region(start, end - start);
114
115         Template[] templates= JavaPlugin.getDefault().getTemplateStore().getTemplates();
116
117         if (selection.y == 0) {
118             for (int i= 0; i != templates.length; i++)
119                 if (context.canEvaluate(templates[i]))
120                     fProposals.add(new TemplateProposal(templates[i], context, region, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE)));
121
122         } else {
123
124             if (context.getKey().length() == 0)
125                 context.setForceEvaluation(true);
126
127             boolean multipleLinesSelected= areMultipleLinesSelected(viewer);
128
129             for (int i= 0; i != templates.length; i++) {
130                 Template template= templates[i];
131                 if (context.canEvaluate(template) &&
132                     template.getContextTypeId().equals(context.getContextType().getId()) &&
133                     (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1)))
134                 {
135                     fProposals.add(new TemplateProposal(templates[i], context, region, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE)));
136                 }
137             }
138         }
139     }
140
141     /**
142      * Returns <code>true</code> if one line is completely selected or if multiple lines are selected.
143      * Being completely selected means that all characters except the new line characters are
144      * selected.
145      *
146      * @return <code>true</code> if one or multiple lines are selected
147      * @since 2.1
148      */

149     private boolean areMultipleLinesSelected(ITextViewer viewer) {
150         if (viewer == null)
151             return false;
152
153         Point s= viewer.getSelectedRange();
154         if (s.y == 0)
155             return false;
156
157         try {
158
159             IDocument document= viewer.getDocument();
160             int startLine= document.getLineOfOffset(s.x);
161             int endLine= document.getLineOfOffset(s.x + s.y);
162             IRegion line= document.getLineInformation(startLine);
163             return startLine != endLine || (s.x == line.getOffset() && s.y == line.getLength());
164
165         } catch (BadLocationException x) {
166             return false;
167         }
168     }
169 }
170
Popular Tags