2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
9 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
10 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
11 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
12 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.contentassist.IContextInformation;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.widgets.Shell;
25 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
26 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
27 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
30 * A PHP identifier proposal.
32 public class IdentifierProposal implements IPHPCompletionProposal {
34 private final String fTemplate;
35 private final TemplateContext fContext;
36 private final ITextViewer fViewer;
37 private final Image fImage_fun;
38 private final Image fImage_var;
39 private final IRegion fRegion;
41 //private TemplateBuffer fTemplateBuffer;
42 private String fOldText;
43 private IRegion fSelectedRegion; // initialized by apply()
46 * Creates a template proposal with a template and its context.
47 * @param template the template
48 * @param context the context in which the template was requested.
49 * @param image the icon of the proposal.
51 public IdentifierProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
52 // Assert.isNotNull(template);
53 // Assert.isNotNull(context);
54 // Assert.isNotNull(region);
55 // Assert.isNotNull(viewer);
60 fImage_fun= image_fun;
61 fImage_var= image_var;
66 * @see ICompletionProposal#apply(IDocument)
68 public void apply(IDocument document) {
70 // if (fTemplateBuffer == null)
71 // fTemplateBuffer= fContext.evaluate(fTemplate);
73 int start = fRegion.getOffset();
74 int end = fRegion.getOffset() + fRegion.getLength();
76 // insert template string
77 // String templateString = fTemplate; // fTemplateBuffer.getString();
78 document.replace(start, end - start, fTemplate);
80 // translate positions
81 LinkedPositionManager manager = new LinkedPositionManager(document);
82 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
83 // for (int i= 0; i != variables.length; i++) {
84 // TemplatePosition variable= variables[i];
86 // if (variable.isResolved())
89 // int[] offsets= variable.getOffsets();
90 // int length= variable.getLength();
92 // for (int j= 0; j != offsets.length; j++)
93 // manager.addPosition(offsets[j] + start, length);
96 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
97 editor.setFinalCaretOffset(fTemplate.length()+start);
98 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
101 fSelectedRegion = editor.getSelectedRegion();
103 } catch (BadLocationException e) {
104 PHPeclipsePlugin.log(e);
108 // catch (CoreException e) {
109 // handleException(e);
113 // private static int getCaretOffset(TemplateBuffer buffer) {
114 // TemplatePosition[] variables = buffer.getVariables();
115 // for (int i = 0; i != variables.length; i++) {
116 // TemplatePosition variable = variables[i];
118 // if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
119 // return variable.getOffsets()[0];
122 // return buffer.getString().length();
126 * @see ICompletionProposal#getSelection(IDocument)
128 public Point getSelection(IDocument document) {
129 return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
134 * @see ICompletionProposal#getAdditionalProposalInfo()
136 public String getAdditionalProposalInfo() {
138 // if (fTemplateBuffer == null)
139 // fTemplateBuffer= fContext.evaluate(fTemplate);
141 return textToHTML(fTemplate); // fTemplateBuffer.getString());
143 // } catch (CoreException e) {
144 // handleException(e);
150 * @see ICompletionProposal#getDisplayString()
152 public String getDisplayString() {
153 return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
154 // return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
158 * @see ICompletionProposal#getImage()
160 public Image getImage() {
161 if (fTemplate.charAt(0)=='$') {
168 * @see ICompletionProposal#getContextInformation()
170 public IContextInformation getContextInformation() {
174 private static String textToHTML(String string) {
175 StringBuffer buffer = new StringBuffer(string.length());
176 buffer.append("<pre>"); //$NON-NLS-1$
178 for (int i = 0; i != string.length(); i++) {
179 char ch = string.charAt(i);
183 buffer.append("&"); //$NON-NLS-1$
187 buffer.append("<"); //$NON-NLS-1$
191 buffer.append(">"); //$NON-NLS-1$
195 buffer.append(" "); //$NON-NLS-1$
199 buffer.append("<br>"); //$NON-NLS-1$
208 buffer.append("</pre>"); //$NON-NLS-1$
209 return buffer.toString();
212 private void openErrorDialog(BadLocationException e) {
213 Shell shell = fViewer.getTextWidget().getShell();
214 MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
217 private void handleException(CoreException e) {
218 Shell shell = fViewer.getTextWidget().getShell();
219 PHPeclipsePlugin.log(e);
220 // ExceptionHandler.handle(e, shell, TemplateMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
224 * @see IJavaCompletionProposal#getRelevance()
226 public int getRelevance() {
228 if (fContext instanceof PHPUnitContext) {
229 PHPUnitContext context = (PHPUnitContext) fContext;
230 switch (context.getCharacterBeforeStart()) {
231 // high relevance after whitespace