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.php.PHPUnitContext;
13 import net.sourceforge.phpdt.internal.corext.template.php.PHPTemplateMessages;
14 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
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 IdentifierProposal implements IPHPCompletionProposal {
37 private final String fTemplate;
38 private final TemplateContext fContext;
39 private final ITextViewer fViewer;
40 private final Image fImage_fun;
41 private final Image fImage_var;
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 IdentifierProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
55 // Assert.isNotNull(template);
56 // Assert.isNotNull(context);
57 // Assert.isNotNull(region);
58 // Assert.isNotNull(viewer);
63 fImage_fun= image_fun;
64 fImage_var= image_var;
69 * @see ICompletionProposal#apply(IDocument)
71 public void apply(IDocument document) {
73 // if (fTemplateBuffer == null)
74 // fTemplateBuffer= fContext.evaluate(fTemplate);
76 int start = fRegion.getOffset();
77 int end = fRegion.getOffset() + fRegion.getLength();
79 // insert template string
80 // String templateString = fTemplate; // fTemplateBuffer.getString();
81 document.replace(start, end - start, fTemplate);
83 // translate positions
84 LinkedPositionManager manager = new LinkedPositionManager(document);
85 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
86 // for (int i= 0; i != variables.length; i++) {
87 // TemplatePosition variable= variables[i];
89 // if (variable.isResolved())
92 // int[] offsets= variable.getOffsets();
93 // int length= variable.getLength();
95 // for (int j= 0; j != offsets.length; j++)
96 // manager.addPosition(offsets[j] + start, length);
99 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
100 editor.setFinalCaretOffset(fTemplate.length()+start);
101 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
104 fSelectedRegion = editor.getSelectedRegion();
106 } catch (BadLocationException e) {
107 PHPeclipsePlugin.log(e);
111 // catch (CoreException e) {
112 // handleException(e);
116 // private static int getCaretOffset(TemplateBuffer buffer) {
117 // TemplatePosition[] variables = buffer.getVariables();
118 // for (int i = 0; i != variables.length; i++) {
119 // TemplatePosition variable = variables[i];
121 // if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
122 // return variable.getOffsets()[0];
125 // return buffer.getString().length();
129 * @see ICompletionProposal#getSelection(IDocument)
131 public Point getSelection(IDocument document) {
132 return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
137 * @see ICompletionProposal#getAdditionalProposalInfo()
139 public String getAdditionalProposalInfo() {
141 // if (fTemplateBuffer == null)
142 // fTemplateBuffer= fContext.evaluate(fTemplate);
144 return textToHTML(fTemplate); // fTemplateBuffer.getString());
146 // } catch (CoreException e) {
147 // handleException(e);
153 * @see ICompletionProposal#getDisplayString()
155 public String getDisplayString() {
156 return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
157 // return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
161 * @see ICompletionProposal#getImage()
163 public Image getImage() {
164 if (fTemplate.charAt(0)=='$') {
171 * @see ICompletionProposal#getContextInformation()
173 public IContextInformation getContextInformation() {
177 private static String textToHTML(String string) {
178 StringBuffer buffer = new StringBuffer(string.length());
179 buffer.append("<pre>"); //$NON-NLS-1$
181 for (int i = 0; i != string.length(); i++) {
182 char ch = string.charAt(i);
186 buffer.append("&"); //$NON-NLS-1$
190 buffer.append("<"); //$NON-NLS-1$
194 buffer.append(">"); //$NON-NLS-1$
198 buffer.append(" "); //$NON-NLS-1$
202 buffer.append("<br>"); //$NON-NLS-1$
211 buffer.append("</pre>"); //$NON-NLS-1$
212 return buffer.toString();
215 private void openErrorDialog(BadLocationException e) {
216 Shell shell = fViewer.getTextWidget().getShell();
217 MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
220 private void handleException(CoreException e) {
221 Shell shell = fViewer.getTextWidget().getShell();
222 PHPeclipsePlugin.log(e);
223 // ExceptionHandler.handle(e, shell, TemplateMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
227 * @see IJavaCompletionProposal#getRelevance()
229 public int getRelevance() {
231 if (fContext instanceof PHPUnitContext) {
232 PHPUnitContext context = (PHPUnitContext) fContext;
233 switch (context.getCharacterBeforeStart()) {
234 // high relevance after whitespace