]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/TemplateEngine.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / template / contentassist / TemplateEngine.java
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  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.template.contentassist;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.Map;
17 import java.util.Map.Entry;
18
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.Point;
21
22 import org.eclipse.core.runtime.Assert;
23
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITextViewer;
28 import org.eclipse.jface.text.Position;
29 import org.eclipse.jface.text.Region;
30 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
31 import org.eclipse.jface.text.templates.Template;
32 import org.eclipse.jface.text.templates.TemplateContextType;
33
34 import org.eclipse.jdt.core.ICompilationUnit;
35
36 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext;
37 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType;
38 import org.eclipse.jdt.internal.corext.template.java.SWTContextType;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.JavaPluginImages;
42
43
44 public 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 }