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.PHPUiImages;
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 net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
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.jface.text.templates.TemplateContext;
20 import org.eclipse.swt.graphics.Image;
21 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
22 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
23 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
26 * A PHP identifier proposal.
28 public class BuiltInProposal extends AbstractProposal { // implements IPHPCompletionProposal {
29 private final TemplateContext fContext;
30 private final PHPFunction fFunction;
32 //private TemplateBuffer fTemplateBuffer;
33 private String fOldText;
34 // private final Image fImage_builtin;
36 private final IRegion fRegion;
37 // private IRegion fSelectedRegion; // initialized by apply()
39 private final String fBuiltinFunctionName;
40 // private final ITextViewer fViewer;
43 * Creates a template proposal with a template and its context.
44 * @param template the template
45 * @param context the context in which the template was requested.
46 * @param image the icon of the proposal.
48 public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
50 fBuiltinFunctionName = functionName;
58 * @see ICompletionProposal#apply(IDocument)
60 public void apply(IDocument document) {
62 // if (fTemplateBuffer == null)
63 // fTemplateBuffer= fContext.evaluate(fTemplate);
65 int start = fRegion.getOffset();
66 int end = fRegion.getOffset() + fRegion.getLength();
68 // insert template string
69 // String templateString = fTemplate; // fTemplateBuffer.getString();
70 document.replace(start, end - start, fBuiltinFunctionName + "()");
72 // translate positions
73 LinkedPositionManager manager = new LinkedPositionManager(document);
74 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
75 // for (int i= 0; i != variables.length; i++) {
76 // TemplatePosition variable= variables[i];
78 // if (variable.isResolved())
81 // int[] offsets= variable.getOffsets();
82 // int length= variable.getLength();
84 // for (int j= 0; j != offsets.length; j++)
85 // manager.addPosition(offsets[j] + start, length);
88 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
89 editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
90 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
93 fSelectedRegion = editor.getSelectedRegion();
95 } catch (BadLocationException e) {
96 PHPeclipsePlugin.log(e);
100 // catch (CoreException e) {
101 // handleException(e);
106 * @see ICompletionProposal#getAdditionalProposalInfo()
108 public String getAdditionalProposalInfo() {
109 return textToHTML(fFunction.getDescription()); // fTemplateBuffer.getString());
113 * @see ICompletionProposal#getContextInformation()
115 public IContextInformation getContextInformation() {
120 * @see ICompletionProposal#getDisplayString()
122 public String getDisplayString() {
123 return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$ //$NON-NLS-1$
124 // return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
128 * @see ICompletionProposal#getImage()
130 public Image getImage() {
131 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
135 * @see IJavaCompletionProposal#getRelevance()
137 public int getRelevance() {
139 if (fContext instanceof JavaContext) {
140 JavaContext context = (JavaContext) fContext;
141 switch (context.getCharacterBeforeStart()) {
142 // high relevance after whitespace