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.link.LinkedPositionManager;
15 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.ITextViewer;
23 import org.eclipse.jface.text.contentassist.IContextInformation;
24 import org.eclipse.swt.graphics.Image;
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 template proposal.
32 public class TemplateProposal extends AbstractProposal {
33 private final TemplateContext fContext;
34 private final Image fImage;
35 private String fOldText;
36 private final IRegion fRegion;
38 private final Template fTemplate;
40 private TemplateBuffer fTemplateBuffer;
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 TemplateProposal(Template template, TemplateContext context, IRegion region, ITextViewer viewer, Image image) {
58 private static int getCaretOffset(TemplateBuffer buffer) {
59 TemplatePosition[] variables = buffer.getVariables();
60 for (int i = 0; i != variables.length; i++) {
61 TemplatePosition variable = variables[i];
63 if (variable.getName().equals(PHPTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
64 return variable.getOffsets()[0];
67 return buffer.getString().length();
71 * @see ICompletionProposal#apply(IDocument)
73 public void apply(IDocument document) {
75 if (fTemplateBuffer == null)
76 fTemplateBuffer = fContext.evaluate(fTemplate);
78 int start = fRegion.getOffset();
79 int end = fRegion.getOffset() + fRegion.getLength();
81 // insert template string
82 String templateString = fTemplateBuffer.getString();
83 document.replace(start, end - start, templateString);
85 // translate positions
86 LinkedPositionManager manager = new LinkedPositionManager(document);
87 TemplatePosition[] variables = fTemplateBuffer.getVariables();
88 for (int i = 0; i != variables.length; i++) {
89 TemplatePosition variable = variables[i];
91 if (variable.isResolved())
94 int[] offsets = variable.getOffsets();
95 int length = variable.getLength();
97 for (int j = 0; j != offsets.length; j++)
98 manager.addPosition(offsets[j] + start, length);
101 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
102 editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
105 fSelectedRegion = editor.getSelectedRegion();
107 } catch (BadLocationException e) {
108 PHPeclipsePlugin.log(e);
111 } catch (CoreException e) {
117 * @see ICompletionProposal#getAdditionalProposalInfo()
119 public String getAdditionalProposalInfo() {
121 if (fTemplateBuffer == null)
122 fTemplateBuffer = fContext.evaluate(fTemplate);
124 return textToHTML(fTemplateBuffer.getString());
126 } catch (CoreException e) {
133 * @see ICompletionProposal#getContextInformation()
135 public IContextInformation getContextInformation() {
140 * @see ICompletionProposal#getDisplayString()
142 public String getDisplayString() {
143 return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
147 * @see ICompletionProposal#getImage()
149 public Image getImage() {
154 * @see IJavaCompletionProposal#getRelevance()
156 public int getRelevance() {
158 if (fContext instanceof PHPUnitContext) {
159 PHPUnitContext context = (PHPUnitContext) fContext;
160 switch (context.getCharacterBeforeStart()) {
161 // high relevance after whitespace