1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / HTMLCompletionProcessor.java
1 /**********************************************************************
2  Copyright (c) 2000, 2002 IBM Corp. 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 implementation
10  www.phpeclipse.de
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import net.sourceforge.phpdt.core.ICompilationUnit;
19 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
20 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
21 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.TemplateEngine;
22 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24
25 //import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.TextPresentation;
28 import org.eclipse.jface.text.contentassist.ICompletionProposal;
29 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
30 import org.eclipse.jface.text.contentassist.IContextInformation;
31 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
32 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
33 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
34 import org.eclipse.jface.text.templates.TemplateContextType;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.ui.IEditorPart;
37
38 /**
39  * HTML completion processor.
40  */
41 public class HTMLCompletionProcessor implements IContentAssistProcessor {
42
43         /**
44          * Simple content assist tip closer. The tip is valid in a range of 5
45          * characters around its popup location.
46          */
47         protected static class Validator implements IContextInformationValidator,
48                         IContextInformationPresenter {
49
50                 protected int fInstallOffset;
51
52                 /*
53                  * @see IContextInformationValidator#isContextInformationValid(int)
54                  */
55                 public boolean isContextInformationValid(int offset) {
56                         return Math.abs(fInstallOffset - offset) < 5;
57                 }
58
59                 /*
60                  * @see IContextInformationValidator#install(IContextInformation,
61                  *      ITextViewer, int)
62                  */
63                 public void install(IContextInformation info, ITextViewer viewer,
64                                 int offset) {
65                         fInstallOffset = offset;
66                 }
67
68                 /*
69                  * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int,
70                  *      TextPresentation)
71                  */
72                 public boolean updatePresentation(int documentPosition,
73                                 TextPresentation presentation) {
74                         return false;
75                 }
76         };
77
78         private static class ContextInformationWrapper implements
79                         IContextInformation, IContextInformationExtension {
80
81                 private final IContextInformation fContextInformation;
82
83                 private int fPosition;
84
85                 public ContextInformationWrapper(IContextInformation contextInformation) {
86                         fContextInformation = contextInformation;
87                 }
88
89                 /*
90                  * @see IContextInformation#getContextDisplayString()
91                  */
92                 public String getContextDisplayString() {
93                         return fContextInformation.getContextDisplayString();
94                 }
95
96                 /*
97                  * @see IContextInformation#getImage()
98                  */
99                 public Image getImage() {
100                         return fContextInformation.getImage();
101                 }
102
103                 /*
104                  * @see IContextInformation#getInformationDisplayString()
105                  */
106                 public String getInformationDisplayString() {
107                         return fContextInformation.getInformationDisplayString();
108                 }
109
110                 /*
111                  * @see IContextInformationExtension#getContextInformationPosition()
112                  */
113                 public int getContextInformationPosition() {
114                         return fPosition;
115                 }
116
117                 public void setContextInformationPosition(int position) {
118                         fPosition = position;
119                 }
120         };
121
122         protected IContextInformationValidator fValidator = new Validator();
123
124         private TemplateEngine fTemplateEngine;
125
126         private char[] fProposalAutoActivationSet;
127
128         private PHPCompletionProposalComparator fComparator;
129
130         private int fNumberOfComputedResults = 0;
131
132         private IEditorPart fEditor;
133
134         protected IWorkingCopyManager fManager;
135
136         public HTMLCompletionProcessor(IEditorPart editor) {
137                 fEditor = editor;
138                 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
139
140                 TemplateContextType contextType = PHPeclipsePlugin.getDefault()
141                                 .getTemplateContextRegistry().getContextType("html"); //$NON-NLS-1$
142                 if (contextType != null)
143                         fTemplateEngine = new TemplateEngine(contextType);
144
145                 fComparator = new PHPCompletionProposalComparator();
146         }
147
148         /*
149          * (non-Javadoc) Method declared on IContentAssistProcessor
150          */
151         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
152                         int documentOffset) {
153                 int contextInformationPosition = guessContextInformationPosition(
154                                 viewer, documentOffset);
155                 return internalComputeCompletionProposals(viewer, documentOffset,
156                                 contextInformationPosition);
157         }
158
159         private ICompletionProposal[] internalComputeCompletionProposals(
160                         ITextViewer viewer, int offset, int contextOffset) {
161                 //IDocument document = viewer.getDocument();
162                 ICompilationUnit unit = fManager.getWorkingCopy(fEditor
163                                 .getEditorInput());
164
165                 if (fTemplateEngine != null) {
166                         ICompletionProposal[] results;
167                         // try {
168                         fTemplateEngine.reset();
169                         fTemplateEngine.complete(viewer, offset, unit);
170                         // } catch (JavaModelException x) {
171                         // Shell shell= viewer.getTextWidget().getShell();
172                         // ErrorDialog.openError(shell,
173                         // JavaTextMessages.getString("CompletionProcessor.error.accessing.title"),
174                         // JavaTextMessages.getString("CompletionProcessor.error.accessing.message"),
175                         // x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
176                         // }
177
178                         IPHPCompletionProposal[] templateResults = fTemplateEngine
179                                         .getResults();
180
181                         // concatenate arrays
182                         IPHPCompletionProposal[] total;
183                         total = new IPHPCompletionProposal[templateResults.length];
184                         System.arraycopy(templateResults, 0, total, 0,
185                                         templateResults.length);
186                         results = total;
187
188                         fNumberOfComputedResults = (results == null ? 0 : results.length);
189                         /*
190                          * Order here and not in result collector to make sure that the
191                          * order applies to all proposals and not just those of the
192                          * compilation unit.
193                          */
194                         return order(results);
195                 }
196                 return new IPHPCompletionProposal[0];
197         }
198
199         private int guessContextInformationPosition(ITextViewer viewer, int offset) {
200                 int contextPosition = offset;
201                 //IDocument document = viewer.getDocument();
202                 return contextPosition;
203         }
204
205         /*
206          * (non-Javadoc) Method declared on IContentAssistProcessor
207          */
208         // public IContextInformation[] computeContextInformation(ITextViewer
209         // viewer, int documentOffset) {
210         // IContextInformation[] result = new IContextInformation[5];
211         // for (int i = 0; i < result.length; i++)
212         // result[i] = new
213         // ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"),
214         // new Object[] { new Integer(i), new Integer(documentOffset)}),
215         // //$NON-NLS-1$
216         // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"),
217         // new Object[] { new Integer(i), new Integer(documentOffset - 5), new
218         // Integer(documentOffset + 5)})); //$NON-NLS-1$
219         // return result;
220         // }
221         /**
222          * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
223          */
224         public IContextInformation[] computeContextInformation(ITextViewer viewer,
225                         int offset) {
226                 int contextInformationPosition = guessContextInformationPosition(
227                                 viewer, offset);
228                 List result = addContextInformations(viewer, contextInformationPosition);
229                 return (IContextInformation[]) result
230                                 .toArray(new IContextInformation[result.size()]);
231         }
232
233         private List addContextInformations(ITextViewer viewer, int offset) {
234                 ICompletionProposal[] proposals = internalComputeCompletionProposals(
235                                 viewer, offset, -1);
236
237                 List result = new ArrayList();
238                 for (int i = 0; i < proposals.length; i++) {
239                         IContextInformation contextInformation = proposals[i]
240                                         .getContextInformation();
241                         if (contextInformation != null) {
242                                 ContextInformationWrapper wrapper = new ContextInformationWrapper(
243                                                 contextInformation);
244                                 wrapper.setContextInformationPosition(offset);
245                                 result.add(wrapper);
246                         }
247                 }
248                 return result;
249         }
250
251         /**
252          * Order the given proposals.
253          */
254         private ICompletionProposal[] order(ICompletionProposal[] proposals) {
255                 Arrays.sort(proposals, fComparator);
256                 return proposals;
257         }
258
259         /**
260          * Tells this processor to order the proposals alphabetically.
261          * 
262          * @param order
263          *            <code>true</code> if proposals should be ordered.
264          */
265         public void orderProposalsAlphabetically(boolean order) {
266                 fComparator.setOrderAlphabetically(order);
267         }
268
269         /**
270          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
271          */
272         public char[] getCompletionProposalAutoActivationCharacters() {
273                 return fProposalAutoActivationSet;
274         }
275
276         /**
277          * Sets this processor's set of characters triggering the activation of the
278          * completion proposal computation.
279          * 
280          * @param activationSet
281          *            the activation set
282          */
283         public void setCompletionProposalAutoActivationCharacters(
284                         char[] activationSet) {
285                 fProposalAutoActivationSet = activationSet;
286         }
287
288         /*
289          * (non-Javadoc) Method declared on IContentAssistProcessor
290          */
291         public char[] getContextInformationAutoActivationCharacters() {
292                 return new char[] {};
293         }
294
295         /*
296          * (non-Javadoc) Method declared on IContentAssistProcessor
297          */
298         public IContextInformationValidator getContextInformationValidator() {
299                 return fValidator;
300         }
301
302         /*
303          * (non-Javadoc) Method declared on IContentAssistProcessor
304          */
305         public String getErrorMessage() {
306                 return null;
307         }
308 }