1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / contentassist / TemplateEngine.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.template.contentassist;
12
13 import java.util.ArrayList;
14
15 import net.sourceforge.phpdt.core.ICompilationUnit;
16 //incastrix
17 //import net.sourceforge.phpdt.internal.corext.Assert;
18 import org.eclipse.core.runtime.Assert;
19 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContext;
20 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
21 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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.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 import org.eclipse.swt.graphics.Point;
33
34 public class TemplateEngine {
35
36         private static final String $_LINE_SELECTION = "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
37
38         private static final String $_WORD_SELECTION = "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
39
40         /** The context type. */
41         private TemplateContextType fContextType;
42
43         /** The result proposals. */
44         private ArrayList fProposals = new ArrayList();
45
46         /**
47          * Creates the template engine for a particular context type. See
48          * <code>TemplateContext</code> for supported context types.
49          */
50         public TemplateEngine(TemplateContextType contextType) {
51                 Assert.isNotNull(contextType);
52                 fContextType = contextType;
53         }
54
55         /**
56          * Empties the collector.
57          */
58         public void reset() {
59                 fProposals.clear();
60         }
61
62         /**
63          * Returns the array of matching templates.
64          */
65         public TemplateProposal[] getResults() {
66                 return (TemplateProposal[]) fProposals
67                                 .toArray(new TemplateProposal[fProposals.size()]);
68         }
69
70         /**
71          * Inspects the context of the compilation unit around
72          * <code>completionPosition</code> and feeds the collector with proposals.
73          * 
74          * @param viewer
75          *            the text viewer
76          * @param completionPosition
77          *            the context position in the document of the text viewer
78          * @param compilationUnit
79          *            the compilation unit (may be <code>null</code>)
80          */
81         public void complete(ITextViewer viewer, int completionPosition,
82                         ICompilationUnit compilationUnit) {
83                 IDocument document = viewer.getDocument();
84
85                 if (!(fContextType instanceof CompilationUnitContextType))
86                         return;
87
88                 Point selection = viewer.getSelectedRange();
89
90                 // remember selected text
91                 String selectedText = null;
92                 if (selection.y != 0) {
93                         try {
94                                 selectedText = document.get(selection.x, selection.y);
95                         } catch (BadLocationException e) {
96                         }
97                 }
98
99                 CompilationUnitContext context = ((CompilationUnitContextType) fContextType)
100                                 .createContext(document, completionPosition, selection.y,
101                                                 compilationUnit);
102                 context.setVariable("selection", selectedText); //$NON-NLS-1$
103                 int start = context.getStart();
104                 int end = context.getEnd();
105                 IRegion region = new Region(start, end - start);
106
107                 Template[] templates = PHPeclipsePlugin.getDefault().getTemplateStore()
108                                 .getTemplates();
109
110                 if (selection.y == 0) {
111                         for (int i = 0; i != templates.length; i++)
112                                 if (context.canEvaluate(templates[i]))
113                                         fProposals.add(new TemplateProposal(templates[i], context,
114                                                         region, PHPUiImages
115                                                                         .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
116
117                 } else {
118
119                         if (context.getKey().length() == 0)
120                                 context.setForceEvaluation(true);
121
122                         boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
123
124                         for (int i = 0; i != templates.length; i++) {
125                                 Template template = templates[i];
126                                 if (context.canEvaluate(template)
127                                                 && template.getContextTypeId().equals(
128                                                                 context.getContextType().getId())
129                                                 && (!multipleLinesSelected
130                                                                 && template.getPattern().indexOf(
131                                                                                 $_WORD_SELECTION) != -1 || (multipleLinesSelected && template
132                                                                 .getPattern().indexOf($_LINE_SELECTION) != -1))) {
133                                         fProposals.add(new TemplateProposal(templates[i], context,
134                                                         region, PHPUiImages
135                                                                         .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
136                                 }
137                         }
138                 }
139         }
140
141         /**
142          * Returns <code>true</code> if one line is completely selected or if
143          * multiple lines are selected. Being completely selected means that all
144          * characters except the new line characters are 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
164                                         || (s.x == line.getOffset() && s.y == line.getLength());
165
166                 } catch (BadLocationException x) {
167                         return false;
168                 }
169         }
170 }