2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import net.sourceforge.phpdt.internal.corext.template.Template;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
9 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
10 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
11 import net.sourceforge.phpdt.internal.corext.template.TemplatePosition;
12 import net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContext;
13 import net.sourceforge.phpdt.internal.corext.template.java.JavaTemplateMessages;
14 import net.sourceforge.phpdt.internal.ui.text.java.IJavaCompletionProposal;
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import org.eclipse.core.runtime.CoreException;
17 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
18 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.widgets.Shell;
28 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
29 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
30 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
33 * A PHP identifier proposal.
35 public class BuiltInProposal implements IJavaCompletionProposal {
37 private final String fTemplate;
38 private final TemplateContext fContext;
39 private final ITextViewer fViewer;
40 private final Image fImage_builtin;
42 private final IRegion fRegion;
44 //private TemplateBuffer fTemplateBuffer;
45 private String fOldText;
46 private IRegion fSelectedRegion; // initialized by apply()
49 * Creates a template proposal with a template and its context.
50 * @param template the template
51 * @param context the context in which the template was requested.
52 * @param image the icon of the proposal.
54 public BuiltInProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_builtin) {
55 // Assert.isNotNull(template);
56 // Assert.isNotNull(context);
57 // Assert.isNotNull(region);
58 // Assert.isNotNull(viewer);
63 fImage_builtin= image_builtin;
68 * @see ICompletionProposal#apply(IDocument)
70 public void apply(IDocument document) {
72 // if (fTemplateBuffer == null)
73 // fTemplateBuffer= fContext.evaluate(fTemplate);
75 int start = fRegion.getOffset();
76 int end = fRegion.getOffset() + fRegion.getLength();
78 // insert template string
79 // String templateString = fTemplate; // fTemplateBuffer.getString();
80 document.replace(start, end - start, fTemplate);
82 // translate positions
83 LinkedPositionManager manager = new LinkedPositionManager(document);
84 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
85 // for (int i= 0; i != variables.length; i++) {
86 // TemplatePosition variable= variables[i];
88 // if (variable.isResolved())
91 // int[] offsets= variable.getOffsets();
92 // int length= variable.getLength();
94 // for (int j= 0; j != offsets.length; j++)
95 // manager.addPosition(offsets[j] + start, length);
98 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
99 editor.setFinalCaretOffset(fTemplate.length()+start);
100 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
103 fSelectedRegion = editor.getSelectedRegion();
105 } catch (BadLocationException e) {
106 PHPeclipsePlugin.log(e);
110 // catch (CoreException e) {
111 // handleException(e);
115 // private static int getCaretOffset(TemplateBuffer buffer) {
116 // TemplatePosition[] variables = buffer.getVariables();
117 // for (int i = 0; i != variables.length; i++) {
118 // TemplatePosition variable = variables[i];
120 // if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
121 // return variable.getOffsets()[0];
124 // return buffer.getString().length();
128 * @see ICompletionProposal#getSelection(IDocument)
130 public Point getSelection(IDocument document) {
131 return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
136 * @see ICompletionProposal#getAdditionalProposalInfo()
138 public String getAdditionalProposalInfo() {
140 // if (fTemplateBuffer == null)
141 // fTemplateBuffer= fContext.evaluate(fTemplate);
143 return textToHTML(fTemplate); // fTemplateBuffer.getString());
145 // } catch (CoreException e) {
146 // handleException(e);
152 * @see ICompletionProposal#getDisplayString()
154 public String getDisplayString() {
155 return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
156 // return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
160 * @see ICompletionProposal#getImage()
162 public Image getImage() {
163 return fImage_builtin;
167 * @see ICompletionProposal#getContextInformation()
169 public IContextInformation getContextInformation() {
173 private static String textToHTML(String string) {
174 StringBuffer buffer = new StringBuffer(string.length());
175 buffer.append("<pre>"); //$NON-NLS-1$
177 for (int i = 0; i != string.length(); i++) {
178 char ch = string.charAt(i);
182 buffer.append("&"); //$NON-NLS-1$
186 buffer.append("<"); //$NON-NLS-1$
190 buffer.append(">"); //$NON-NLS-1$
194 buffer.append(" "); //$NON-NLS-1$
198 buffer.append("<br>"); //$NON-NLS-1$
207 buffer.append("</pre>"); //$NON-NLS-1$
208 return buffer.toString();
211 private void openErrorDialog(BadLocationException e) {
212 Shell shell = fViewer.getTextWidget().getShell();
213 MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
216 private void handleException(CoreException e) {
217 Shell shell = fViewer.getTextWidget().getShell();
218 PHPeclipsePlugin.log(e);
219 // ExceptionHandler.handle(e, shell, TemplateMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
223 * @see IJavaCompletionProposal#getRelevance()
225 public int getRelevance() {
227 if (fContext instanceof CompilationUnitContext) {
228 CompilationUnitContext context = (CompilationUnitContext) fContext;
229 switch (context.getCharacterBeforeStart()) {
230 // high relevance after whitespace