*** empty log message ***
[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.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 IdentifierProposal implements IPHPCompletionProposal {
33
34   private final String fTemplate;
35   private final TemplateContext fContext;
36   private final ITextViewer fViewer;
37   private final Image fImage_fun;
38   private final Image fImage_var;
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 IdentifierProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
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_fun= image_fun;
61     fImage_var= image_var;
62     fRegion = region;
63   }
64
65   /*
66    * @see ICompletionProposal#apply(IDocument)
67    */
68   public void apply(IDocument document) {
69     try {
70       //                    if (fTemplateBuffer == null)
71       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
72
73       int start = fRegion.getOffset();
74       int end = fRegion.getOffset() + fRegion.getLength();
75
76       // insert template string
77     //  String templateString = fTemplate; // fTemplateBuffer.getString();      
78       document.replace(start, end - start, fTemplate);
79
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];
85       //
86       //                                if (variable.isResolved())
87       //                                        continue;
88       //                                
89       //                                int[] offsets= variable.getOffsets();
90       //                                int length= variable.getLength();
91       //                                
92       //                                for (int j= 0; j != offsets.length; j++)
93       //                                        manager.addPosition(offsets[j] + start, length);
94       //                        }
95
96       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
97       editor.setFinalCaretOffset(fTemplate.length()+start);
98    //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
99       editor.enter();
100
101       fSelectedRegion = editor.getSelectedRegion();
102
103     } catch (BadLocationException e) {
104       PHPeclipsePlugin.log(e);
105       openErrorDialog(e);
106
107     }
108     //      catch (CoreException e) {
109     //                  handleException(e);
110     //      }       
111   }
112
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];
117 //
118 //      if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
119 //        return variable.getOffsets()[0];
120 //    }
121 //
122 //    return buffer.getString().length();
123 //  }
124
125   /*
126    * @see ICompletionProposal#getSelection(IDocument)
127    */
128   public Point getSelection(IDocument document) {
129     return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
130     //    return null;
131   }
132
133   /*
134    * @see ICompletionProposal#getAdditionalProposalInfo()
135    */
136   public String getAdditionalProposalInfo() {
137     //      try {
138     //                  if (fTemplateBuffer == null)
139     //                          fTemplateBuffer= fContext.evaluate(fTemplate);
140
141     return textToHTML(fTemplate); // fTemplateBuffer.getString());
142
143     //      } catch (CoreException e) {
144     //                  handleException(e);                 
145     //                  return null;
146     //      }
147   }
148
149   /*
150    * @see ICompletionProposal#getDisplayString()
151    */
152   public String getDisplayString() {
153     return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
154     //          return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
155   }
156
157   /*
158    * @see ICompletionProposal#getImage()
159    */
160   public Image getImage() {
161     if (fTemplate.charAt(0)=='$') {
162       return fImage_var;
163     }
164                 return fImage_fun;
165   }
166
167   /*
168    * @see ICompletionProposal#getContextInformation()
169    */
170   public IContextInformation getContextInformation() {
171     return null;
172   }
173
174   private static String textToHTML(String string) {
175     StringBuffer buffer = new StringBuffer(string.length());
176     buffer.append("<pre>"); //$NON-NLS-1$
177
178     for (int i = 0; i != string.length(); i++) {
179       char ch = string.charAt(i);
180
181       switch (ch) {
182         case '&' :
183           buffer.append("&amp;"); //$NON-NLS-1$
184           break;
185
186         case '<' :
187           buffer.append("&lt;"); //$NON-NLS-1$
188           break;
189
190         case '>' :
191           buffer.append("&gt;"); //$NON-NLS-1$
192           break;
193
194         case '\t' :
195           buffer.append("    "); //$NON-NLS-1$
196           break;
197
198         case '\n' :
199           buffer.append("<br>"); //$NON-NLS-1$
200           break;
201
202         default :
203           buffer.append(ch);
204           break;
205       }
206     }
207
208     buffer.append("</pre>"); //$NON-NLS-1$
209     return buffer.toString();
210   }
211
212   private void openErrorDialog(BadLocationException e) {
213     Shell shell = fViewer.getTextWidget().getShell();
214     MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
215   }
216
217   private void handleException(CoreException e) {
218     Shell shell = fViewer.getTextWidget().getShell();
219     PHPeclipsePlugin.log(e);
220     //          ExceptionHandler.handle(e, shell, TemplateMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
221   }
222
223   /*
224    * @see IJavaCompletionProposal#getRelevance()
225    */
226   public int getRelevance() {
227
228     if (fContext instanceof PHPUnitContext) {
229       PHPUnitContext context = (PHPUnitContext) fContext;
230       switch (context.getCharacterBeforeStart()) {
231         // high relevance after whitespace
232         case ' ' :
233         case '\r' :
234         case '\n' :
235         case '\t' :
236           return 90;
237
238         default :
239           return 0;
240       }
241     } else {
242       return 90;
243     }
244   }
245
246 }