c483035c2bf6bed98eab952ae2b94276f2c389f4
[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.TemplateMessages;
9 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
10 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
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 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
15
16 import org.eclipse.core.resources.IProject;
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.ContextInformation;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.templates.TemplateContext;
24 import org.eclipse.swt.graphics.Image;
25
26 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
27 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
28 // import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
29
30 /**
31  * A PHP identifier proposal.
32  */
33 public class DeclarationProposal extends AbstractProposal { // implements
34         // IPHPCompletionProposal
35         // {
36         private IProject fProject;
37
38         private final TemplateContext fContext;
39
40         private final PHPIdentifierLocation fLocation;
41
42         String fInfo;
43
44         // private TemplateBuffer fTemplateBuffer;
45         // private String fOldText;
46         // private final Image fImage_fun;
47         // private final Image fImage_var;
48         private final IRegion fRegion;
49
50         // private IRegion fSelectedRegion; // initialized by apply()
51
52         private final String fIdentifierName;
53
54         // private final ITextViewer fViewer;
55
56         /**
57          * Creates a template proposal with a template and its context.
58          * 
59          * @param template
60          *            the template
61          * @param context
62          *            the context in which the template was requested.
63          * @param image
64          *            the icon of the proposal.
65          */
66         public DeclarationProposal(IProject project, String identifierName,
67                         PHPIdentifierLocation location, TemplateContext context,
68                         IRegion region, ITextViewer viewer) {
69                 super(viewer);
70                 // Image image_fun,
71                 // Image image_var) {
72                 fProject = project;
73                 fIdentifierName = identifierName;
74                 fLocation = location;
75                 fContext = context;
76                 fRegion = region;
77                 fInfo = null;
78         }
79
80         /*
81          * @see ICompletionProposal#apply(IDocument)
82          */
83         public void apply(IDocument document) {
84                 try {
85                         // if (fTemplateBuffer == null)
86                         // fTemplateBuffer= fContext.evaluate(fTemplate);
87
88                         int start = fRegion.getOffset();
89                         int end = fRegion.getOffset() + fRegion.getLength();
90
91                         switch (fLocation.getType()) {
92                         case PHPIdentifierLocation.FUNCTION:
93                                 document.replace(start, end - start, fIdentifierName + "()");
94                                 break;
95                         case PHPIdentifierLocation.CONSTRUCTOR:
96                                 document.replace(start, end - start, fIdentifierName + "()");
97                                 break;
98                         case PHPIdentifierLocation.METHOD:
99                                 document.replace(start, end - start, fIdentifierName + "()");
100                                 break;
101
102                         default:
103                                 document.replace(start, end - start, fIdentifierName);
104                         }
105
106                         // translate positions
107                         LinkedPositionManager manager = new LinkedPositionManager(document);
108                         // TemplatePosition[] variables= fTemplateBuffer.getVariables();
109                         // for (int i= 0; i != variables.length; i++) {
110                         // TemplatePosition variable= variables[i];
111                         //
112                         // if (variable.isResolved())
113                         // continue;
114                         //
115                         // int[] offsets= variable.getOffsets();
116                         // int length= variable.getLength();
117                         //
118                         // for (int j= 0; j != offsets.length; j++)
119                         // manager.addPosition(offsets[j] + start, length);
120                         // }
121
122                         LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
123                         switch (fLocation.getType()) {
124                         case PHPIdentifierLocation.FUNCTION:
125                                 editor
126                                                 .setFinalCaretOffset(fIdentifierName.length() + start
127                                                                 + 1);
128                                 break;
129                         case PHPIdentifierLocation.CONSTRUCTOR:
130                                 editor
131                                                 .setFinalCaretOffset(fIdentifierName.length() + start
132                                                                 + 1);
133                                 break;
134                         case PHPIdentifierLocation.METHOD:
135                                 editor
136                                                 .setFinalCaretOffset(fIdentifierName.length() + start
137                                                                 + 1);
138                                 break;
139
140                         default:
141                                 editor.setFinalCaretOffset(fIdentifierName.length() + start);
142                         }
143                         editor.enter();
144
145                         fSelectedRegion = editor.getSelectedRegion();
146
147                 } catch (BadLocationException e) {
148                         PHPeclipsePlugin.log(e);
149                         openErrorDialog(e);
150
151                 }
152                 // catch (CoreException e) {
153                 // handleException(e);
154                 // }
155         }
156
157         /*
158          * @see ICompletionProposal#getAdditionalProposalInfo()
159          */
160         public String getAdditionalProposalInfo() {
161                 if (fInfo == null) {
162                         fInfo = computeProposalInfo();
163                 }
164                 return fInfo;
165         }
166
167         private String computeProposalInfo() {
168                 StringBuffer hoverInfoBuffer = new StringBuffer();
169                 // String workspaceLocation =
170                 // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
171                 String workspaceLocation;
172                 if (fProject != null) {
173                         workspaceLocation = fProject.getLocation().toString() + '/';
174                 } else {
175                         // should never happen?
176                         workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
177                                         .getFullPath().toString();
178                 }
179                 String filename = workspaceLocation + fLocation.getFilename();
180                 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
181                 return hoverInfoBuffer.toString();
182         }
183
184         public IContextInformation getContextInformation() {
185                 if (fContextInfo == null) {
186                         if (fLocation != null) {
187                                 fInfo = fLocation.getUsage();
188                                 if (fInfo != null) {
189                                         // extract the parameter context information for the
190                                         // function:
191                                         int i0 = fInfo.indexOf('(');
192                                         int newline = fInfo.indexOf('\n');
193                                         if (i0 >= 0 && (i0 < newline || newline < 0)) {
194                                                 int i1 = fInfo.indexOf(')', i0 + 1);
195                                                 if (i1 > 0) {
196
197                                                         fContextInfo = new ContextInformation(null, fInfo
198                                                                         .substring(i0 + 1, i1));
199                                                 } else {
200                                                         fContextInfo = new ContextInformation(null, fInfo);
201                                                 }
202                                         } else {
203                                                 fContextInfo = new ContextInformation(null, fInfo);
204                                         }
205                                 }
206                         }
207                 }
208                 return fContextInfo;
209         }
210
211         /*
212          * @see ICompletionProposal#getDisplayString()
213          */
214         public String getDisplayString() {
215                 String workspaceLocation;
216                 String workspaceName;
217                 if (fProject != null) {
218                         workspaceLocation = fProject.getFullPath().toString() + '/';
219                         workspaceName = fProject.getName().toString() + '/';
220                 } else {
221                         // should never happen?
222                         workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
223                                         .getFullPath().toString();
224                         workspaceName = workspaceLocation;
225                 }
226                 String filename = fLocation.getFilename();
227                 String usage = PHPDocUtil.getUsage(workspaceLocation + filename, fLocation);
228                 String result = fIdentifierName
229                                 + TemplateMessages.getString("TemplateProposal.delimiter");
230                 if (usage.length() > 0) {
231                         result += usage
232                                         + TemplateMessages.getString("TemplateProposal.delimiter");
233                 }
234                 result += workspaceName + filename;
235                 return result;
236         }
237
238         /*
239          * @see ICompletionProposal#getImage()
240          */
241         public Image getImage() {
242                 switch (fLocation.getType()) {
243                 case PHPIdentifierLocation.FUNCTION:
244                         return PHPUiImages.get(PHPUiImages.IMG_FUN);
245                 case PHPIdentifierLocation.CLASS:
246                         return PHPUiImages.get(PHPUiImages.IMG_CLASS);
247                 case PHPIdentifierLocation.CONSTRUCTOR:
248                         return PHPUiImages.get(PHPUiImages.IMG_CLASS);
249                 case PHPIdentifierLocation.METHOD:
250                         return PHPUiImages.get(PHPUiImages.IMG_FUN);
251                 case PHPIdentifierLocation.DEFINE:
252                         return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
253                 case PHPIdentifierLocation.VARIABLE:
254                         return PHPUiImages.get(PHPUiImages.IMG_VAR);
255                 case PHPIdentifierLocation.GLOBAL_VARIABLE:
256                         return PHPUiImages.get(PHPUiImages.IMG_VAR);
257                 }
258                 return PHPUiImages.get(PHPUiImages.IMG_FUN);
259         }
260
261         /*
262          * @see IJavaCompletionProposal#getRelevance()
263          */
264         public int getRelevance() {
265
266                 if (fContext instanceof JavaContext) {
267                         JavaContext context = (JavaContext) fContext;
268                         switch (context.getCharacterBeforeStart()) {
269                         // high relevance after whitespace
270                         case ' ':
271                         case '\r':
272                         case '\n':
273                         case '\t':
274                                 return 80;
275                         case '>': // ->
276                         case ':': // ::
277                                 return 85;
278                         default:
279                                 return 0;
280                         }
281                 } else {
282                         return 80;
283                 }
284         }
285
286 }