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