KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > actions > ActionUtil


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso.actions;
5
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.core.resources.IResource;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IAdaptable;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.jdt.core.IClassFile;
13 import org.eclipse.jdt.core.ICompilationUnit;
14 import org.eclipse.jdt.core.IField;
15 import org.eclipse.jdt.core.IImportDeclaration;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.core.ILocalVariable;
19 import org.eclipse.jdt.core.IMethod;
20 import org.eclipse.jdt.core.IPackageDeclaration;
21 import org.eclipse.jdt.core.IPackageFragment;
22 import org.eclipse.jdt.core.IPackageFragmentRoot;
23 import org.eclipse.jdt.core.IType;
24 import org.eclipse.jdt.core.JavaCore;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jface.text.ITextSelection;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.ui.IEditorInput;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.IFileEditorInput;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.texteditor.ITextEditor;
36 import org.terracotta.dso.JdtUtils;
37 import org.terracotta.dso.TcPlugin;
38
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * Collection of static methods for finding various workspace artifacts,
43  * such as the currently selected Java project. Deals with the
44  * Package Explorer as well as selected textual elements in a JavaEditor.
45  *
46  * @see org.eclipse.jface.viewers.IStructuredSelection
47  * @see org.eclipse.jface.viewers.ISelection
48  * @see org.eclipse.jface.text.ITextSelection
49  * @see org.terracotta.dso.actions
50  */

51
52 public class ActionUtil {
53   public static IJavaProject locateSelectedJavaProject(ISelection selection) {
54     if(selection != null && !selection.isEmpty()) {
55       if(selection instanceof IStructuredSelection) {
56         IStructuredSelection ss = (IStructuredSelection)selection;
57         Iterator JavaDoc iter = ss.iterator();
58         Object JavaDoc obj;
59         
60         while(iter.hasNext()) {
61           obj = iter.next();
62           
63           if(obj instanceof IJavaElement) {
64             return ((IJavaElement)obj).getJavaProject();
65           }
66           else if(obj instanceof IProject) {
67             return findJavaProject((IProject)obj);
68           }
69           else if(obj instanceof IResource) {
70             return findJavaProject(((IResource)obj).getProject());
71           }
72           else if(obj instanceof IAdaptable) {
73             IAdaptable adaptable = (IAdaptable)obj;
74             IResource res = (IResource)adaptable.getAdapter(IResource.class);
75             
76             if(res != null) {
77               return findJavaProject(((IResource)obj).getProject());
78             }
79           }
80         }
81       }
82     }
83
84     IEditorPart part = findSelectedEditorPart();
85     
86     if(part != null) {
87       IEditorInput input = part.getEditorInput();
88       
89       if(input instanceof IFileEditorInput) {
90         IProject project = ((IFileEditorInput)input).getFile().getProject();
91         if(project != null) {
92           return findJavaProject(project);
93         }
94       } else {
95         IClassFile classFile = (IClassFile)input.getAdapter(IClassFile.class);
96         if(classFile != null) {
97           return classFile.getJavaProject();
98         }
99       }
100     }
101     
102     return null;
103   }
104
105   public static IJavaProject findJavaProject(IProject project) {
106     return project.isOpen() ? JavaCore.create(project) : null;
107   }
108   
109   public static IJavaProject findSelectedJavaProject(ISelection selection) {
110     if(selection != null) {
111       if(selection instanceof IStructuredSelection) {
112         IJavaElement elem = findSelectedJavaElement((IStructuredSelection)selection);
113         
114         if(elem instanceof IJavaProject) {
115           return (IJavaProject)elem;
116         }
117       }
118     }
119
120     return null;
121   }
122
123   public static IPackageFragment findSelectedPackageFragment(ISelection selection) {
124     if(selection != null) {
125       IJavaElement elem = findSelectedJavaElement(selection);
126       
127       if(elem instanceof IPackageFragment) {
128         return (IPackageFragment)elem;
129       }
130       else if(elem instanceof IPackageDeclaration) {
131         IPackageDeclaration packageDecl = (IPackageDeclaration)elem;
132         String JavaDoc name = packageDecl.getElementName();
133         IPackageFragmentRoot root = JdtUtils.getPackageFragmentRoot(elem);
134         
135         if(root != null) {
136           return root.getPackageFragment(name);
137         }
138       }
139     }
140
141     return null;
142   }
143
144   public static IField findSelectedField(ISelection selection) {
145     if(selection != null) {
146       IJavaElement elem = findSelectedJavaElement(selection);
147       
148       if(elem instanceof IField) {
149         return (IField)elem;
150       }
151     }
152
153     return null;
154   }
155  
156   public static ICompilationUnit findSelectedCompilationUnit() {
157     ITextEditor editor = findSelectedTextEditor();
158     
159     if(editor != null) {
160       return JdtUtils.getInputAsCompilationUnit(editor);
161     }
162     
163     return null;
164   }
165   
166   public static IEditorPart findSelectedEditorPart() {
167     IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
168     
169     if(win != null) {
170       IWorkbenchPage page = win.getActivePage();
171       
172       if(page != null) {
173         return page.getActiveEditor();
174       }
175     }
176     
177     return null;
178   }
179
180   public static ITextEditor findSelectedTextEditor() {
181     IEditorPart part = findSelectedEditorPart();
182         
183     if(part != null && part instanceof ITextEditor) {
184       return (ITextEditor)part;
185     }
186     
187     return null;
188   }
189
190   public static IMethod findSelectedMethod(ISelection selection) {
191     if(selection != null) {
192       IJavaElement elem = findSelectedJavaElement(selection);
193       
194       if(elem instanceof IMethod) {
195         return (IMethod)elem;
196       }
197     }
198
199     return null;
200   }
201       
202   public static IFile findSelectedFile(ISelection selection) {
203     if(selection != null) {
204       if(selection instanceof IStructuredSelection) {
205         IStructuredSelection ss = (IStructuredSelection)selection;
206
207         Object JavaDoc obj = ss.getFirstElement();
208         if(obj instanceof IFile) {
209           return (IFile)obj;
210         }
211       }
212     }
213
214     return null;
215   }
216
217   public static ICompilationUnit findSelectedCompilationUnit(ISelection selection) {
218     if(selection != null) {
219       if(selection instanceof IStructuredSelection) {
220         IJavaElement elem = findSelectedJavaElement((IStructuredSelection)selection);
221         
222         if(elem instanceof ICompilationUnit) {
223           return (ICompilationUnit)elem;
224         }
225       }
226       else if(selection instanceof ITextSelection) {
227         return findSelectedCompilationUnit();
228       }
229     }
230
231     return null;
232   }
233
234   public static IType findSelectedType(ISelection selection) {
235     if(selection != null) {
236       IJavaElement elem = findSelectedJavaElement(selection);
237         
238       if(elem instanceof IType) {
239         return (IType)elem;
240       }
241       else if(elem instanceof IMethod) {
242         return ((IMethod)elem).getDeclaringType();
243       }
244       else if(elem instanceof IField){
245         IJavaProject javaProject = elem.getJavaProject();
246         IField field = (IField)elem;
247         IType declaringType = field.getDeclaringType();
248
249         try {
250           String JavaDoc sig = field.getTypeSignature();
251           String JavaDoc typeName = JdtUtils.getResolvedTypeName(sig, declaringType);
252
253           return JdtUtils.findType(javaProject, typeName);
254         } catch(JavaModelException jme) {/**/}
255       }
256       else if(elem instanceof ILocalVariable){
257         IJavaProject javaProject = elem.getJavaProject();
258         ILocalVariable var = (ILocalVariable)elem;
259         IType enclosingType = (IType)elem.getAncestor(IJavaElement.TYPE);
260
261         if(enclosingType != null) {
262           try {
263             String JavaDoc sig = var.getTypeSignature();
264             String JavaDoc typeName = JdtUtils.getResolvedTypeName(sig, enclosingType);
265   
266             return JdtUtils.findType(javaProject, typeName);
267           } catch(JavaModelException jme) {/**/}
268         }
269       }
270       else if(elem instanceof IImportDeclaration) {
271         IImportDeclaration importDecl = (IImportDeclaration)elem;
272         String JavaDoc typeName = importDecl.getElementName();
273         
274         if(!importDecl.isOnDemand()) {
275           try {
276             return JdtUtils.findType(elem.getJavaProject(), typeName);
277           } catch(JavaModelException jme) {/**/}
278         }
279       }
280       else if(elem instanceof IClassFile) {
281         try {
282           return ((IClassFile)elem).getType();
283         } catch(JavaModelException jme) {/**/}
284       }
285     }
286
287     return null;
288   }
289
290   public static IJavaElement findSelectedJavaElement(ISelection selection) {
291     if(selection != null) {
292       if(selection instanceof IStructuredSelection) {
293         return findSelectedJavaElement((IStructuredSelection)selection);
294       }
295       else if(selection instanceof ITextSelection) {
296         return findSelectedJavaElement((ITextSelection)selection);
297       }
298     }
299     
300     return null;
301   }
302
303   public static IJavaElement findSelectedJavaElement(IStructuredSelection selection) {
304     if(selection != null) {
305       Object JavaDoc obj = selection.getFirstElement();
306       
307       if(obj instanceof IJavaElement) {
308         return (IJavaElement)obj;
309       }
310     }
311     
312     return null;
313   }
314
315   public static IJavaElement findSelectedJavaElement(ITextSelection selection) {
316     if(selection != null) {
317       ITextEditor editor = findSelectedTextEditor();
318       
319       if(editor != null) {
320         IEditorInput input = editor.getEditorInput();
321         
322         if(input instanceof IFileEditorInput) {
323           IFile file = ((IFileEditorInput)input).getFile();
324           
325           if(TcPlugin.getDefault().hasTerracottaNature(file.getProject())) {
326             try {
327               IJavaElement[] elems = JdtUtils.codeResolve(editor);
328               return elems.length == 1 ? elems[0] : null;
329               //return JdtUtils.getElementAtOffset(editor);
330
} catch(JavaModelException jme) {/**/}
331           }
332         }
333         else {
334           IClassFile classFile = (IClassFile)input.getAdapter(IClassFile.class);
335           
336           if(classFile != null) {
337             IJavaProject javaProj = classFile.getJavaProject();
338             
339             if(TcPlugin.getDefault().hasTerracottaNature(javaProj.getProject())) {
340               try {
341                 IJavaElement[] elems = JdtUtils.codeResolve(editor);
342                 return elems.length == 1 ? elems[0] : null;
343                 //return JdtUtils.getElementAtOffset(editor);
344
} catch(JavaModelException jme) {/**/}
345             }
346           }
347         }
348       }
349     }
350     
351     return null;
352   }
353
354   public static String JavaDoc getStatusMessages(Exception JavaDoc e) {
355     String JavaDoc msg = e.getMessage();
356
357     if(e instanceof CoreException) {
358       CoreException ce = (CoreException)e;
359       IStatus status = ce.getStatus();
360       IStatus[] children = status.getChildren();
361
362       for(int i = 0; i < children.length; i++) {
363         msg += "\n" + children[i].getMessage();
364       }
365
366       System.err.println(msg);
367       ce.printStackTrace(System.err);
368     }
369
370     return msg;
371   }
372 }
373
Free Books   Free Magazines  
Popular Tags