A massive organize imports and formatting of the sources using default Eclipse code...
[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                                         .getLocation().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                 if (fProject != null) {
217                         workspaceLocation = fProject.getName().toString() + '/';
218                 } else {
219                         // should never happen?
220                         workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot()
221                                         .getLocation().toString();
222                 }
223                 String filename = workspaceLocation + fLocation.getFilename();
224                 String usage = PHPDocUtil.getUsage(filename, fLocation);
225                 String result = fIdentifierName
226                                 + TemplateMessages.getString("TemplateProposal.delimiter");
227                 if (usage.length() > 0) {
228                         result += usage
229                                         + TemplateMessages.getString("TemplateProposal.delimiter");
230                 }
231                 result += filename;
232                 return result;
233         }
234
235         /*
236          * @see ICompletionProposal#getImage()
237          */
238         public Image getImage() {
239                 switch (fLocation.getType()) {
240                 case PHPIdentifierLocation.FUNCTION:
241                         return PHPUiImages.get(PHPUiImages.IMG_FUN);
242                 case PHPIdentifierLocation.CLASS:
243                         return PHPUiImages.get(PHPUiImages.IMG_CLASS);
244                 case PHPIdentifierLocation.CONSTRUCTOR:
245                         return PHPUiImages.get(PHPUiImages.IMG_CLASS);
246                 case PHPIdentifierLocation.METHOD:
247                         return PHPUiImages.get(PHPUiImages.IMG_FUN);
248                 case PHPIdentifierLocation.DEFINE:
249                         return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
250                 case PHPIdentifierLocation.VARIABLE:
251                         return PHPUiImages.get(PHPUiImages.IMG_VAR);
252                 case PHPIdentifierLocation.GLOBAL_VARIABLE:
253                         return PHPUiImages.get(PHPUiImages.IMG_VAR);
254                 }
255                 return PHPUiImages.get(PHPUiImages.IMG_FUN);
256         }
257
258         /*
259          * @see IJavaCompletionProposal#getRelevance()
260          */
261         public int getRelevance() {
262
263                 if (fContext instanceof JavaContext) {
264                         JavaContext context = (JavaContext) fContext;
265                         switch (context.getCharacterBeforeStart()) {
266                         // high relevance after whitespace
267                         case ' ':
268                         case '\r':
269                         case '\n':
270                         case '\t':
271                                 return 80;
272                         case '>': // ->
273                         case ':': // ::
274                                 return 85;
275                         default:
276                                 return 0;
277                         }
278                 } else {
279                         return 80;
280                 }
281         }
282
283 }