2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
6 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
7 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
8 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
9 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
10 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
12 import org.eclipse.jface.text.BadLocationException;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.IRegion;
15 import org.eclipse.jface.text.ITextViewer;
16 import org.eclipse.jface.text.contentassist.IContextInformation;
17 import org.eclipse.swt.graphics.Image;
18 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
19 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
20 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
22 * A PHP identifier proposal.
24 public class IdentifierProposal extends AbstractProposal { //implements
25 // IPHPCompletionProposal
27 private final TemplateContext fContext;
28 private final Image fImage_fun;
29 private final Image fImage_var;
30 //private TemplateBuffer fTemplateBuffer;
31 private String fOldText;
32 private final IRegion fRegion;
33 // private IRegion fSelectedRegion; // initialized by apply()
34 private final String fTemplate;
35 // private final ITextViewer fViewer;
37 * Creates a template proposal with a template and its context.
42 * the context in which the template was requested.
44 * the icon of the proposal.
46 public IdentifierProposal(String template, TemplateContext context,
47 IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
52 fImage_fun = image_fun;
53 fImage_var = image_var;
57 * @see ICompletionProposal#apply(IDocument)
59 public void apply(IDocument document) {
61 // if (fTemplateBuffer == null)
62 // fTemplateBuffer= fContext.evaluate(fTemplate);
63 int start = fRegion.getOffset();
64 int end = fRegion.getOffset() + fRegion.getLength();
65 // insert template string
66 // String templateString = fTemplate; // fTemplateBuffer.getString();
67 document.replace(start, end - start, fTemplate);
68 // translate positions
69 LinkedPositionManager manager = new LinkedPositionManager(document);
70 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
71 // for (int i= 0; i != variables.length; i++) {
72 // TemplatePosition variable= variables[i];
74 // if (variable.isResolved())
77 // int[] offsets= variable.getOffsets();
78 // int length= variable.getLength();
80 // for (int j= 0; j != offsets.length; j++)
81 // manager.addPosition(offsets[j] + start, length);
83 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
84 editor.setFinalCaretOffset(fTemplate.length() + start);
85 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
87 fSelectedRegion = editor.getSelectedRegion();
88 } catch (BadLocationException e) {
89 PHPeclipsePlugin.log(e);
92 // catch (CoreException e) {
93 // handleException(e);
97 * @see ICompletionProposal#getAdditionalProposalInfo()
99 public String getAdditionalProposalInfo() {
101 // if (fTemplateBuffer == null)
102 // fTemplateBuffer= fContext.evaluate(fTemplate);
103 return textToHTML(fTemplate); // fTemplateBuffer.getString());
104 // } catch (CoreException e) {
105 // handleException(e);
110 * @see ICompletionProposal#getContextInformation()
112 public IContextInformation getContextInformation() {
116 * @see ICompletionProposal#getDisplayString()
118 public String getDisplayString() {
119 return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$
122 * @see ICompletionProposal#getImage()
124 public Image getImage() {
125 if (fTemplate.charAt(0) == '$') {
131 * @see IJavaCompletionProposal#getRelevance()
133 public int getRelevance() {
134 if (fContext instanceof PHPUnitContext) {
135 PHPUnitContext context = (PHPUnitContext) fContext;
136 switch (context.getCharacterBeforeStart()) {
137 // high relevance after whitespace