2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
9 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
10 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
11 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.contentassist.IContextInformation;
19 import org.eclipse.swt.graphics.Image;
20 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
21 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
22 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
25 * A PHP identifier proposal.
27 public class IdentifierProposal extends AbstractProposal { //implements IPHPCompletionProposal {
28 private final TemplateContext fContext;
29 private final Image fImage_fun;
30 private final Image fImage_var;
32 //private TemplateBuffer fTemplateBuffer;
33 private String fOldText;
34 private final IRegion fRegion;
35 // private IRegion fSelectedRegion; // initialized by apply()
37 private final String fTemplate;
38 // private final ITextViewer fViewer;
41 * Creates a template proposal with a template and its context.
42 * @param template the template
43 * @param context the context in which the template was requested.
44 * @param image the icon of the proposal.
46 public IdentifierProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
52 fImage_fun= image_fun;
53 fImage_var= image_var;
59 * @see ICompletionProposal#apply(IDocument)
61 public void apply(IDocument document) {
63 // if (fTemplateBuffer == null)
64 // fTemplateBuffer= fContext.evaluate(fTemplate);
66 int start = fRegion.getOffset();
67 int end = fRegion.getOffset() + fRegion.getLength();
69 // insert template string
70 // String templateString = fTemplate; // fTemplateBuffer.getString();
71 document.replace(start, end - start, fTemplate);
73 // translate positions
74 LinkedPositionManager manager = new LinkedPositionManager(document);
75 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
76 // for (int i= 0; i != variables.length; i++) {
77 // TemplatePosition variable= variables[i];
79 // if (variable.isResolved())
82 // int[] offsets= variable.getOffsets();
83 // int length= variable.getLength();
85 // for (int j= 0; j != offsets.length; j++)
86 // manager.addPosition(offsets[j] + start, length);
89 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
90 editor.setFinalCaretOffset(fTemplate.length()+start);
91 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
94 fSelectedRegion = editor.getSelectedRegion();
96 } catch (BadLocationException e) {
97 PHPeclipsePlugin.log(e);
101 // catch (CoreException e) {
102 // handleException(e);
107 * @see ICompletionProposal#getAdditionalProposalInfo()
109 public String getAdditionalProposalInfo() {
111 // if (fTemplateBuffer == null)
112 // fTemplateBuffer= fContext.evaluate(fTemplate);
114 return textToHTML(fTemplate); // fTemplateBuffer.getString());
116 // } catch (CoreException e) {
117 // handleException(e);
123 * @see ICompletionProposal#getContextInformation()
125 public IContextInformation getContextInformation() {
130 * @see ICompletionProposal#getDisplayString()
132 public String getDisplayString() {
133 return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
134 // return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
138 * @see ICompletionProposal#getImage()
140 public Image getImage() {
141 if (fTemplate.charAt(0)=='$') {
148 * @see IJavaCompletionProposal#getRelevance()
150 public int getRelevance() {
152 if (fContext instanceof PHPUnitContext) {
153 PHPUnitContext context = (PHPUnitContext) fContext;
154 switch (context.getCharacterBeforeStart()) {
155 // high relevance after whitespace