238423d96c804d02868c55d8db0e7d03248066fa
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / DeclarationProposal.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.phpdoc.PHPDocUtil;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
9 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
10 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
11 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
12 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
13 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
16
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.jdt.internal.ui.text.link.LinkedPositionManager;
24 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
25 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
26
27 /**
28  * A PHP identifier proposal.
29  */
30 public class DeclarationProposal extends AbstractProposal { //implements IPHPCompletionProposal {
31   private final TemplateContext fContext;
32   private final PHPIdentifierLocation fLocation;
33
34   //private TemplateBuffer fTemplateBuffer;
35   private String fOldText;
36   //  private final Image fImage_fun;
37   //  private final Image fImage_var;
38   private final IRegion fRegion;
39   //  private IRegion fSelectedRegion; // initialized by apply()
40
41   private final String fIdentifierName;
42   //  private final ITextViewer fViewer;
43
44   /**
45    * Creates a template proposal with a template and its context.
46    * @param template  the template
47    * @param context   the context in which the template was requested.
48    * @param image     the icon of the proposal.
49    */
50   public DeclarationProposal(
51     String identifierName,
52     PHPIdentifierLocation location,
53     TemplateContext context,
54     IRegion region,
55     ITextViewer viewer) {
56     super(viewer);
57     //    Image image_fun,
58     //    Image image_var) {
59     fIdentifierName = identifierName;
60     fLocation = location;
61     fContext = context;
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       switch (fLocation.getType()) {
77         case PHPIdentifierLocation.FUNCTION :
78           document.replace(start, end - start, fIdentifierName + "()");
79           break;
80         case PHPIdentifierLocation.CONSTRUCTOR :
81           document.replace(start, end - start, fIdentifierName + "()");
82           break;
83         case PHPIdentifierLocation.METHOD :
84           document.replace(start, end - start, fIdentifierName + "()");
85           break;
86
87         default :
88           document.replace(start, end - start, fIdentifierName);
89       }
90
91       // translate positions
92       LinkedPositionManager manager = new LinkedPositionManager(document);
93       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
94       //                        for (int i= 0; i != variables.length; i++) {
95       //                                TemplatePosition variable= variables[i];
96       //
97       //                                if (variable.isResolved())
98       //                                        continue;
99       //                                
100       //                                int[] offsets= variable.getOffsets();
101       //                                int length= variable.getLength();
102       //                                
103       //                                for (int j= 0; j != offsets.length; j++)
104       //                                        manager.addPosition(offsets[j] + start, length);
105       //                        }
106
107       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
108       switch (fLocation.getType()) {
109         case PHPIdentifierLocation.FUNCTION :
110           editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
111           break;
112         case PHPIdentifierLocation.CONSTRUCTOR :
113           editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
114           break;
115         case PHPIdentifierLocation.METHOD :
116           editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
117           break;
118
119         default :
120           editor.setFinalCaretOffset(fIdentifierName.length() + start);
121       }
122       editor.enter();
123
124       fSelectedRegion = editor.getSelectedRegion();
125
126     } catch (BadLocationException e) {
127       PHPeclipsePlugin.log(e);
128       openErrorDialog(e);
129
130     }
131     //      catch (CoreException e) {
132     //                  handleException(e);
133     //      }       
134   }
135
136   /*
137    * @see ICompletionProposal#getAdditionalProposalInfo()
138    */
139   public String getAdditionalProposalInfo() {
140     StringBuffer hoverInfoBuffer = new StringBuffer();
141     String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
142     String filename = workspaceLocation + fLocation.getFilename();
143     PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
144     return textToHTML(hoverInfoBuffer.toString());
145   }
146
147   /*
148    * @see ICompletionProposal#getContextInformation()
149    */
150   public IContextInformation getContextInformation() {
151     return null;
152   }
153
154   /*
155    * @see ICompletionProposal#getDisplayString()
156    */
157   public String getDisplayString() {
158     String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
159     String filename = workspaceLocation + fLocation.getFilename();
160     return fIdentifierName + TemplateMessages.getString("TemplateProposal.delimiter") + PHPDocUtil.getUsage(filename, fLocation) + TemplateMessages.getString("TemplateProposal.delimiter") + filename; // $NON-NLS-1$ //$NON-NLS-1$
161   }
162
163   /*
164    * @see ICompletionProposal#getImage()
165    */
166   public Image getImage() {
167     switch (fLocation.getType()) {
168       case PHPIdentifierLocation.FUNCTION :
169         return PHPUiImages.get(PHPUiImages.IMG_FUN);
170       case PHPIdentifierLocation.CLASS :
171         return PHPUiImages.get(PHPUiImages.IMG_CLASS);
172       case PHPIdentifierLocation.CONSTRUCTOR :
173         return PHPUiImages.get(PHPUiImages.IMG_CLASS);
174       case PHPIdentifierLocation.METHOD :
175         return PHPUiImages.get(PHPUiImages.IMG_FUN);
176       case PHPIdentifierLocation.DEFINE :
177         return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
178       case PHPIdentifierLocation.VARIABLE :
179         return PHPUiImages.get(PHPUiImages.IMG_VAR);
180     }
181     return PHPUiImages.get(PHPUiImages.IMG_FUN);
182   }
183
184   /*
185    * @see IJavaCompletionProposal#getRelevance()
186    */
187   public int getRelevance() {
188
189     if (fContext instanceof PHPUnitContext) {
190       PHPUnitContext context = (PHPUnitContext) fContext;
191       switch (context.getCharacterBeforeStart()) {
192         // high relevance after whitespace
193         case ' ' :
194         case '\r' :
195         case '\n' :
196         case '\t' :
197           return 90;
198         case '>' : // ->
199         case ':' : // ::
200           return 95;
201         default :
202           return 0;
203       }
204     } else {
205       return 90;
206     }
207   }
208
209 }