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