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