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.PHPTemplateMessages;
13 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
14 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
15 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
16 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.contentassist.IContextInformation;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.widgets.Shell;
29 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
30 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
31 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
34 * A template proposal.
36 public class TemplateProposal implements IPHPCompletionProposal {
38 private final Template fTemplate;
39 private final TemplateContext fContext;
40 private final ITextViewer fViewer;
41 private final Image fImage;
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 TemplateProposal(Template template, TemplateContext context, IRegion region, ITextViewer viewer, Image image) {
55 // Assert.isNotNull(template);
56 // Assert.isNotNull(context);
57 // Assert.isNotNull(region);
58 // Assert.isNotNull(viewer);
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 = fTemplateBuffer.getString();
80 document.replace(start, end - start, templateString);
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(getCaretOffset(fTemplateBuffer) + start);
102 fSelectedRegion = editor.getSelectedRegion();
104 } catch (BadLocationException e) {
105 PHPeclipsePlugin.log(e);
108 } catch (CoreException 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(PHPTemplateMessages.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(fTemplateBuffer.getString());
143 } catch (CoreException e) {
150 * @see ICompletionProposal#getDisplayString()
152 public String getDisplayString() {
153 return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
157 * @see ICompletionProposal#getImage()
159 public Image getImage() {
164 * @see ICompletionProposal#getContextInformation()
166 public IContextInformation getContextInformation() {
170 private static String textToHTML(String string) {
171 StringBuffer buffer = new StringBuffer(string.length());
172 buffer.append("<pre>"); //$NON-NLS-1$
174 for (int i = 0; i != string.length(); i++) {
175 char ch = string.charAt(i);
179 buffer.append("&"); //$NON-NLS-1$
183 buffer.append("<"); //$NON-NLS-1$
187 buffer.append(">"); //$NON-NLS-1$
191 buffer.append(" "); //$NON-NLS-1$
195 buffer.append("<br>"); //$NON-NLS-1$
204 buffer.append("</pre>"); //$NON-NLS-1$
205 return buffer.toString();
208 private void openErrorDialog(BadLocationException e) {
209 Shell shell = fViewer.getTextWidget().getShell();
210 MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
213 private void handleException(CoreException e) {
214 Shell shell = fViewer.getTextWidget().getShell();
215 PHPeclipsePlugin.log(e);
216 // ExceptionHandler.handle(e, shell, ObfuscatorMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
220 * @see IJavaCompletionProposal#getRelevance()
222 public int getRelevance() {
224 if (fContext instanceof PHPUnitContext) {
225 PHPUnitContext context = (PHPUnitContext) fContext;
226 switch (context.getCharacterBeforeStart()) {
227 // high relevance after whitespace