*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / BuiltInProposal.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6
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;
14
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;
28
29 /**
30  * A PHP identifier proposal.
31  */
32 public class BuiltInProposal implements IPHPCompletionProposal {
33
34   private final String fTemplate;
35   private final TemplateContext fContext;
36   private final ITextViewer fViewer;
37   private final Image fImage_builtin;
38
39   private final IRegion fRegion;
40
41   //private TemplateBuffer fTemplateBuffer;
42   private String fOldText;
43   private IRegion fSelectedRegion; // initialized by apply()
44
45   /**
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.
50    */
51   public BuiltInProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_builtin) {
52     //          Assert.isNotNull(template);
53     //          Assert.isNotNull(context);
54     //          Assert.isNotNull(region);
55     //          Assert.isNotNull(viewer);
56
57     fTemplate = template;
58     fContext = context;
59     fViewer = viewer;
60         fImage_builtin= image_builtin;
61     fRegion = region;
62   }
63
64   /*
65    * @see ICompletionProposal#apply(IDocument)
66    */
67   public void apply(IDocument document) {
68     try {
69       //                    if (fTemplateBuffer == null)
70       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
71
72       int start = fRegion.getOffset();
73       int end = fRegion.getOffset() + fRegion.getLength();
74
75       // insert template string
76     //  String templateString = fTemplate; // fTemplateBuffer.getString();      
77       document.replace(start, end - start, fTemplate);
78
79       // translate positions
80       LinkedPositionManager manager = new LinkedPositionManager(document);
81       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
82       //                        for (int i= 0; i != variables.length; i++) {
83       //                                TemplatePosition variable= variables[i];
84       //
85       //                                if (variable.isResolved())
86       //                                        continue;
87       //                                
88       //                                int[] offsets= variable.getOffsets();
89       //                                int length= variable.getLength();
90       //                                
91       //                                for (int j= 0; j != offsets.length; j++)
92       //                                        manager.addPosition(offsets[j] + start, length);
93       //                        }
94
95       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
96       editor.setFinalCaretOffset(fTemplate.length()+start);
97    //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
98       editor.enter();
99
100       fSelectedRegion = editor.getSelectedRegion();
101
102     } catch (BadLocationException e) {
103       PHPeclipsePlugin.log(e);
104       openErrorDialog(e);
105
106     }
107     //      catch (CoreException e) {
108     //                  handleException(e);
109     //      }       
110   }
111
112 //  private static int getCaretOffset(TemplateBuffer buffer) {
113 //    TemplatePosition[] variables = buffer.getVariables();
114 //    for (int i = 0; i != variables.length; i++) {
115 //      TemplatePosition variable = variables[i];
116 //
117 //      if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
118 //        return variable.getOffsets()[0];
119 //    }
120 //
121 //    return buffer.getString().length();
122 //  }
123
124   /*
125    * @see ICompletionProposal#getSelection(IDocument)
126    */
127   public Point getSelection(IDocument document) {
128     return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
129     //    return null;
130   }
131
132   /*
133    * @see ICompletionProposal#getAdditionalProposalInfo()
134    */
135   public String getAdditionalProposalInfo() {
136     //      try {
137     //                  if (fTemplateBuffer == null)
138     //                          fTemplateBuffer= fContext.evaluate(fTemplate);
139
140     return textToHTML(fTemplate); // fTemplateBuffer.getString());
141
142     //      } catch (CoreException e) {
143     //                  handleException(e);                 
144     //                  return null;
145     //      }
146   }
147
148   /*
149    * @see ICompletionProposal#getDisplayString()
150    */
151   public String getDisplayString() {
152     return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
153     //          return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
154   }
155
156   /*
157    * @see ICompletionProposal#getImage()
158    */
159   public Image getImage() {
160                 return fImage_builtin;
161   }
162
163   /*
164    * @see ICompletionProposal#getContextInformation()
165    */
166   public IContextInformation getContextInformation() {
167     return null;
168   }
169
170   private static String textToHTML(String string) {
171     StringBuffer buffer = new StringBuffer(string.length());
172     buffer.append("<pre>"); //$NON-NLS-1$
173
174     for (int i = 0; i != string.length(); i++) {
175       char ch = string.charAt(i);
176
177       switch (ch) {
178         case '&' :
179           buffer.append("&amp;"); //$NON-NLS-1$
180           break;
181
182         case '<' :
183           buffer.append("&lt;"); //$NON-NLS-1$
184           break;
185
186         case '>' :
187           buffer.append("&gt;"); //$NON-NLS-1$
188           break;
189
190         case '\t' :
191           buffer.append("    "); //$NON-NLS-1$
192           break;
193
194         case '\n' :
195           buffer.append("<br>"); //$NON-NLS-1$
196           break;
197
198         default :
199           buffer.append(ch);
200           break;
201       }
202     }
203
204     buffer.append("</pre>"); //$NON-NLS-1$
205     return buffer.toString();
206   }
207
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$
211   }
212
213   private void handleException(CoreException e) {
214     Shell shell = fViewer.getTextWidget().getShell();
215     PHPeclipsePlugin.log(e);
216     //          ExceptionHandler.handle(e, shell, TemplateMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
217   }
218
219   /*
220    * @see IJavaCompletionProposal#getRelevance()
221    */
222   public int getRelevance() {
223
224     if (fContext instanceof PHPUnitContext) {
225       PHPUnitContext context = (PHPUnitContext) fContext;
226       switch (context.getCharacterBeforeStart()) {
227         // high relevance after whitespace
228         case ' ' :
229         case '\r' :
230         case '\n' :
231         case '\t' :
232           return 90;
233
234         default :
235           return 0;
236       }
237     } else {
238       return 90;
239     }
240   }
241
242 }