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