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.phpeclipse.PHPeclipsePlugin;
13 import org.eclipse.core.runtime.CoreException;
14 import net.sourceforge.phpdt.internal.corext.template.java.CompilationUnitContext;
15 import net.sourceforge.phpdt.internal.corext.template.java.JavaTemplateMessages;
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.JavaPlugin;
26 import net.sourceforge.phpdt.internal.ui.text.java.IJavaCompletionProposal;
27 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
28 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
29 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
32 * A template proposal.
34 public class TemplateProposal implements IJavaCompletionProposal {
36 private final Template fTemplate;
37 private final TemplateContext fContext;
38 private final ITextViewer fViewer;
39 // private final Image fImage;
40 private final IRegion fRegion;
42 private TemplateBuffer fTemplateBuffer;
43 private String fOldText;
44 // private IRegion fSelectedRegion; // initialized by apply()
47 * Creates a template proposal with a template and its context.
48 * @param template the template
49 * @param context the context in which the template was requested.
50 * @param image the icon of the proposal.
52 public TemplateProposal(Template template, TemplateContext context, IRegion region, ITextViewer viewer){ //, Image image) {
53 // Assert.isNotNull(template);
54 // Assert.isNotNull(context);
55 // Assert.isNotNull(region);
56 // Assert.isNotNull(viewer);
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= fTemplateBuffer.getString();
78 document.replace(start, end - start, templateString);
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(getCaretOffset(fTemplateBuffer) + start);
100 // fSelectedRegion= editor.getSelectedRegion();
102 } catch (BadLocationException e) {
103 PHPeclipsePlugin.log(e);
106 } catch (CoreException e) {
111 private static int getCaretOffset(TemplateBuffer buffer) {
112 TemplatePosition[] variables= buffer.getVariables();
113 for (int i= 0; i != variables.length; i++) {
114 TemplatePosition variable= variables[i];
116 if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
117 return variable.getOffsets()[0];
120 return buffer.getString().length();
124 * @see ICompletionProposal#getSelection(IDocument)
126 public Point getSelection(IDocument document) {
127 // return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
132 * @see ICompletionProposal#getAdditionalProposalInfo()
134 public String getAdditionalProposalInfo() {
136 if (fTemplateBuffer == null)
137 fTemplateBuffer= fContext.evaluate(fTemplate);
139 return textToHTML(fTemplateBuffer.getString());
141 } catch (CoreException e) {
148 * @see ICompletionProposal#getDisplayString()
150 public String getDisplayString() {
151 return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
155 * @see ICompletionProposal#getImage()
157 public Image getImage() {
163 * @see ICompletionProposal#getContextInformation()
165 public IContextInformation getContextInformation() {
169 private static String textToHTML(String string) {
170 StringBuffer buffer= new StringBuffer(string.length());
171 buffer.append("<pre>"); //$NON-NLS-1$
173 for (int i= 0; i != string.length(); i++) {
174 char ch= string.charAt(i);
178 buffer.append("&"); //$NON-NLS-1$
182 buffer.append("<"); //$NON-NLS-1$
186 buffer.append(">"); //$NON-NLS-1$
190 buffer.append(" "); //$NON-NLS-1$
194 buffer.append("<br>"); //$NON-NLS-1$
203 buffer.append("</pre>"); //$NON-NLS-1$
204 return buffer.toString();
207 private void openErrorDialog(BadLocationException e) {
208 Shell shell= fViewer.getTextWidget().getShell();
209 MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
212 private void handleException(CoreException e) {
213 Shell shell= fViewer.getTextWidget().getShell();
214 PHPeclipsePlugin.log(e);
215 // ExceptionHandler.handle(e, shell, TemplateMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
219 * @see IJavaCompletionProposal#getRelevance()
221 public int getRelevance() {
223 if (fContext instanceof CompilationUnitContext) {
224 CompilationUnitContext context= (CompilationUnitContext) fContext;
225 switch (context.getCharacterBeforeStart()) {
226 // high relevance after whitespace