2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
6 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
7 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
8 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
9 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
10 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.jface.text.templates.TemplateContext;
18 import org.eclipse.swt.graphics.Image;
19 //import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
20 //import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
21 //import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
23 * A PHP identifier proposal.
25 public class IdentifierProposal extends AbstractProposal { //implements
26 // IPHPCompletionProposal
28 private final TemplateContext fContext;
29 private final Image fImage_fun;
30 private final Image fImage_var;
31 //private TemplateBuffer fTemplateBuffer;
32 private String fOldText;
33 private final IRegion fRegion;
34 // private IRegion fSelectedRegion; // initialized by apply()
35 private final String fTemplate;
36 // private final ITextViewer fViewer;
38 * Creates a template proposal with a template and its context.
43 * the context in which the template was requested.
45 * the icon of the proposal.
47 public IdentifierProposal(String template, TemplateContext context,
48 IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
53 fImage_fun = image_fun;
54 fImage_var = image_var;
58 * @see ICompletionProposal#apply(IDocument)
60 public void apply(IDocument document) {
62 // if (fTemplateBuffer == null)
63 // fTemplateBuffer= fContext.evaluate(fTemplate);
64 int start = fRegion.getOffset();
65 int end = fRegion.getOffset() + fRegion.getLength();
66 // insert template string
67 // String templateString = fTemplate; // fTemplateBuffer.getString();
68 document.replace(start, end - start, fTemplate);
69 // translate positions
70 LinkedPositionManager manager = new LinkedPositionManager(document);
71 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
72 // for (int i= 0; i != variables.length; i++) {
73 // TemplatePosition variable= variables[i];
75 // if (variable.isResolved())
78 // int[] offsets= variable.getOffsets();
79 // int length= variable.getLength();
81 // for (int j= 0; j != offsets.length; j++)
82 // manager.addPosition(offsets[j] + start, length);
84 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
85 editor.setFinalCaretOffset(fTemplate.length() + start);
86 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
88 fSelectedRegion = editor.getSelectedRegion();
89 } catch (BadLocationException e) {
90 PHPeclipsePlugin.log(e);
93 // catch (CoreException e) {
94 // handleException(e);
98 * @see ICompletionProposal#getAdditionalProposalInfo()
100 public String getAdditionalProposalInfo() {
102 // if (fTemplateBuffer == null)
103 // fTemplateBuffer= fContext.evaluate(fTemplate);
104 return textToHTML(fTemplate); // fTemplateBuffer.getString());
105 // } catch (CoreException e) {
106 // handleException(e);
111 * @see ICompletionProposal#getContextInformation()
113 public IContextInformation getContextInformation() {
117 * @see ICompletionProposal#getDisplayString()
119 public String getDisplayString() {
120 return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$
123 * @see ICompletionProposal#getImage()
125 public Image getImage() {
126 if (fTemplate.charAt(0) == '$') {
132 * @see IJavaCompletionProposal#getRelevance()
134 public int getRelevance() {
135 if (fContext instanceof JavaContext) {
136 JavaContext context = (JavaContext) fContext;
137 switch (context.getCharacterBeforeStart()) {
138 // high relevance after whitespace