]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/TemplateEngine.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / template / contentassist / TemplateEngine.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.text.template.contentassist;
12
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.Iterator;
16import java.util.Map;
17import java.util.Map.Entry;
18
19import org.eclipse.swt.graphics.Image;
20import org.eclipse.swt.graphics.Point;
21
22import org.eclipse.core.runtime.Assert;
23
24import org.eclipse.jface.text.BadLocationException;
25import org.eclipse.jface.text.IDocument;
26import org.eclipse.jface.text.IRegion;
27import org.eclipse.jface.text.ITextViewer;
28import org.eclipse.jface.text.Position;
29import org.eclipse.jface.text.Region;
30import org.eclipse.jface.text.templates.GlobalTemplateVariables;
31import org.eclipse.jface.text.templates.Template;
32import org.eclipse.jface.text.templates.TemplateContextType;
33
34import org.eclipse.jdt.core.ICompilationUnit;
35
36import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext;
37import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType;
38import org.eclipse.jdt.internal.corext.template.java.SWTContextType;
39
40import org.eclipse.jdt.internal.ui.JavaPlugin;
41import org.eclipse.jdt.internal.ui.JavaPluginImages;
42
43
44public class TemplateEngine {
45
46 private static final String $_LINE_SELECTION= "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
47 private static final String $_WORD_SELECTION= "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
48
49 /** The context type. */
50 private TemplateContextType fContextType;
51 /** The result proposals. */
52 private ArrayList<TemplateProposal> fProposals= new ArrayList<TemplateProposal>();
53 /** Positions created on the key documents to remove in reset. */
54 private final Map<IDocument, Position> fPositions= new HashMap<IDocument, Position>();
55
56 /**
57 * Creates the template engine for the given <code>contextType</code>.
58 * <p>
59 * The <code>JavaPlugin.getDefault().getTemplateContextRegistry()</code>
60 * defines the supported context types.</p>
61 *
62 * @param contextType the context type
63 */
64 public TemplateEngine(TemplateContextType contextType) {
65 Assert.isNotNull(contextType);
66 fContextType= contextType;
67 }
68
69 /**
70 * Empties the collector.
71 */
72 public void reset() {
73 fProposals.clear();
74 for (Iterator<Entry<IDocument, Position>> it= fPositions.entrySet().iterator(); it.hasNext();) {
75 Entry<IDocument, Position> entry= it.next();
76 IDocument doc= entry.getKey();
77 Position position= entry.getValue();
78 doc.removePosition(position);
79 }
80 fPositions.clear();
81 }
82
83 /**
84 * Returns the array of matching templates.
85 *
86 * @return the template proposals
87 */
88 public TemplateProposal[] getResults() {
89 return fProposals.toArray(new TemplateProposal[fProposals.size()]);
90 }
91
92 /**
93 * Inspects the context of the compilation unit around <code>completionPosition</code>
94 * and feeds the collector with proposals.
95 * @param viewer the text viewer
96 * @param completionPosition the context position in the document of the text viewer
97 * @param compilationUnit the compilation unit (may be <code>null</code>)
98 */
99 public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
100 IDocument document= viewer.getDocument();
101
102 if (!(fContextType instanceof CompilationUnitContextType))
103 return;
104
105 Point selection= viewer.getSelectedRange();
106 Position position= new Position(completionPosition, selection.y);
107
108 // remember selected text
109 String selectedText= null;
110 if (selection.y != 0) {
111 try {
112 selectedText= document.get(selection.x, selection.y);
113 document.addPosition(position);
114 fPositions.put(document, position);
115 } catch (BadLocationException e) {}
116 }
117
118 CompilationUnitContext context= ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
119 context.setVariable("selection", selectedText); //$NON-NLS-1$
120 int start= context.getStart();
121 int end= context.getEnd();
122 IRegion region= new Region(start, end - start);
123
124 Template[] templates= JavaPlugin.getDefault().getTemplateStore().getTemplates();
125
126 if (selection.y == 0) {
127 for (int i= 0; i != templates.length; i++) {
128 Template template= templates[i];
129 if (context.canEvaluate(template)) {
130 fProposals.add(new TemplateProposal(template, context, region, getImage()));
131 }
132 }
133 } else {
134
135 context.generated_551997803931588642();
136
137 boolean multipleLinesSelected= areMultipleLinesSelected(viewer);
138
139 for (int i= 0; i != templates.length; i++) {
140 Template template= templates[i];
141 if (context.canEvaluate(template) &&
142 (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1)))
143 {
144 fProposals.add(new TemplateProposal(templates[i], context, region, getImage()));
145 }
146 }
147 }
148 }
149
150 private Image getImage() {
151 if (fContextType instanceof SWTContextType)
152 return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_SWT_TEMPLATE);
153 else
154 return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE);
155 }
156
157 /**
158 * Returns <code>true</code> if one line is completely selected or if multiple lines are selected.
159 * Being completely selected means that all characters except the new line characters are
160 * selected.
161 *
162 * @param viewer the text viewer
163 * @return <code>true</code> if one or multiple lines are selected
164 * @since 2.1
165 */
166 private boolean areMultipleLinesSelected(ITextViewer viewer) {
167 if (viewer == null)
168 return false;
169
170 Point s= viewer.getSelectedRange();
171 if (s.y == 0)
172 return false;
173
174 try {
175
176 IDocument document= viewer.getDocument();
177 int startLine= document.getLineOfOffset(s.x);
178 int endLine= document.getLineOfOffset(s.x + s.y);
179 IRegion line= document.getLineInformation(startLine);
180 return startLine != endLine || (s.x == line.getOffset() && s.y == line.getLength());
181
182 } catch (BadLocationException x) {
183 return false;
184 }
185 }
186}