Changes:
[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   
127   /* (non-Javadoc)
128    * Method declared on IContentAssistProcessor
129    */
130   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
131     int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
132     return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
133   }
134
135   private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
136     IDocument document = viewer.getDocument();
137     
138     if (fTemplateEngine != null) {
139       ICompletionProposal[] results;
140       //      try {
141       fTemplateEngine.reset();
142       fTemplateEngine.complete(viewer, offset); //, unit);
143       //      } catch (JavaModelException x) {
144       //        Shell shell= viewer.getTextWidget().getShell();
145       //        ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
146       //      }       
147
148       IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
149
150       // concatenate arrays
151       IPHPCompletionProposal[] total;
152       total = new IPHPCompletionProposal[templateResults.length];
153       System.arraycopy(templateResults, 0, total, 0, templateResults.length);
154       results = total;
155
156       fNumberOfComputedResults = (results == null ? 0 : results.length);
157       /*
158        * Order here and not in result collector to make sure that the order
159        * applies to all proposals and not just those of the compilation unit. 
160        */
161       return order(results);
162     }
163     return new IPHPCompletionProposal[0];
164   }
165
166   private int guessContextInformationPosition(ITextViewer viewer, int offset) {
167     int contextPosition = offset;
168     IDocument document = viewer.getDocument();
169     return contextPosition;
170   }
171
172   /* (non-Javadoc)
173    * Method declared on IContentAssistProcessor
174    */
175   //  public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
176   //    IContextInformation[] result = new IContextInformation[5];
177   //    for (int i = 0; i < result.length; i++)
178   //      result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
179   //      MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
180   //    return result;
181   //  }
182   /**
183    * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
184    */
185   public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
186     int contextInformationPosition = guessContextInformationPosition(viewer, offset);
187     List result = addContextInformations(viewer, contextInformationPosition);
188     return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
189   }
190
191   private List addContextInformations(ITextViewer viewer, int offset) {
192     ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
193
194     List result = new ArrayList();
195     for (int i = 0; i < proposals.length; i++) {
196       IContextInformation contextInformation = proposals[i].getContextInformation();
197       if (contextInformation != null) {
198         ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
199         wrapper.setContextInformationPosition(offset);
200         result.add(wrapper);
201       }
202     }
203     return result;
204   }
205
206   /**
207    * Order the given proposals.
208    */
209   private ICompletionProposal[] order(ICompletionProposal[] proposals) {
210     Arrays.sort(proposals, fComparator);
211     return proposals;
212   }
213
214   /**
215    * Tells this processor to order the proposals alphabetically.
216    * 
217    * @param order <code>true</code> if proposals should be ordered.
218    */
219   public void orderProposalsAlphabetically(boolean order) {
220     fComparator.setOrderAlphabetically(order);
221   }
222   
223   /**
224    * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
225    */
226   public char[] getCompletionProposalAutoActivationCharacters() {
227     return fProposalAutoActivationSet;
228   }
229   
230   /**
231    * Sets this processor's set of characters triggering the activation of the
232    * completion proposal computation.
233    * 
234    * @param activationSet the activation set
235    */
236   public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
237     fProposalAutoActivationSet= activationSet;
238   }
239   
240   /* (non-Javadoc)
241    * Method declared on IContentAssistProcessor
242    */
243   public char[] getContextInformationAutoActivationCharacters() {
244     return new char[] {
245     };
246   }
247
248   /* (non-Javadoc)
249    * Method declared on IContentAssistProcessor
250    */
251   public IContextInformationValidator getContextInformationValidator() {
252     return fValidator;
253   }
254
255   /* (non-Javadoc)
256    * Method declared on IContentAssistProcessor
257    */
258   public String getErrorMessage() {
259     return null;
260   }
261 }