001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.actions;
005:
006: import org.eclipse.core.resources.IFile;
007: import org.eclipse.core.resources.IProject;
008: import org.eclipse.core.resources.IResource;
009: import org.eclipse.core.runtime.CoreException;
010: import org.eclipse.core.runtime.IAdaptable;
011: import org.eclipse.core.runtime.IStatus;
012: import org.eclipse.jdt.core.IClassFile;
013: import org.eclipse.jdt.core.ICompilationUnit;
014: import org.eclipse.jdt.core.IField;
015: import org.eclipse.jdt.core.IImportDeclaration;
016: import org.eclipse.jdt.core.IJavaElement;
017: import org.eclipse.jdt.core.IJavaProject;
018: import org.eclipse.jdt.core.ILocalVariable;
019: import org.eclipse.jdt.core.IMethod;
020: import org.eclipse.jdt.core.IPackageDeclaration;
021: import org.eclipse.jdt.core.IPackageFragment;
022: import org.eclipse.jdt.core.IPackageFragmentRoot;
023: import org.eclipse.jdt.core.IType;
024: import org.eclipse.jdt.core.JavaCore;
025: import org.eclipse.jdt.core.JavaModelException;
026: import org.eclipse.jface.text.ITextSelection;
027: import org.eclipse.jface.viewers.ISelection;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.ui.IEditorInput;
030: import org.eclipse.ui.IEditorPart;
031: import org.eclipse.ui.IFileEditorInput;
032: import org.eclipse.ui.IWorkbenchPage;
033: import org.eclipse.ui.IWorkbenchWindow;
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.texteditor.ITextEditor;
036: import org.terracotta.dso.JdtUtils;
037: import org.terracotta.dso.TcPlugin;
038:
039: import java.util.Iterator;
040:
041: /**
042: * Collection of static methods for finding various workspace artifacts,
043: * such as the currently selected Java project. Deals with the
044: * Package Explorer as well as selected textual elements in a JavaEditor.
045: *
046: * @see org.eclipse.jface.viewers.IStructuredSelection
047: * @see org.eclipse.jface.viewers.ISelection
048: * @see org.eclipse.jface.text.ITextSelection
049: * @see org.terracotta.dso.actions
050: */
051:
052: public class ActionUtil {
053: public static IJavaProject locateSelectedJavaProject(
054: ISelection selection) {
055: if (selection != null && !selection.isEmpty()) {
056: if (selection instanceof IStructuredSelection) {
057: IStructuredSelection ss = (IStructuredSelection) selection;
058: Iterator iter = ss.iterator();
059: Object obj;
060:
061: while (iter.hasNext()) {
062: obj = iter.next();
063:
064: if (obj instanceof IJavaElement) {
065: return ((IJavaElement) obj).getJavaProject();
066: } else if (obj instanceof IProject) {
067: return findJavaProject((IProject) obj);
068: } else if (obj instanceof IResource) {
069: return findJavaProject(((IResource) obj)
070: .getProject());
071: } else if (obj instanceof IAdaptable) {
072: IAdaptable adaptable = (IAdaptable) obj;
073: IResource res = (IResource) adaptable
074: .getAdapter(IResource.class);
075:
076: if (res != null) {
077: return findJavaProject(((IResource) obj)
078: .getProject());
079: }
080: }
081: }
082: }
083: }
084:
085: IEditorPart part = findSelectedEditorPart();
086:
087: if (part != null) {
088: IEditorInput input = part.getEditorInput();
089:
090: if (input instanceof IFileEditorInput) {
091: IProject project = ((IFileEditorInput) input).getFile()
092: .getProject();
093: if (project != null) {
094: return findJavaProject(project);
095: }
096: } else {
097: IClassFile classFile = (IClassFile) input
098: .getAdapter(IClassFile.class);
099: if (classFile != null) {
100: return classFile.getJavaProject();
101: }
102: }
103: }
104:
105: return null;
106: }
107:
108: public static IJavaProject findJavaProject(IProject project) {
109: return project.isOpen() ? JavaCore.create(project) : null;
110: }
111:
112: public static IJavaProject findSelectedJavaProject(
113: ISelection selection) {
114: if (selection != null) {
115: if (selection instanceof IStructuredSelection) {
116: IJavaElement elem = findSelectedJavaElement((IStructuredSelection) selection);
117:
118: if (elem instanceof IJavaProject) {
119: return (IJavaProject) elem;
120: }
121: }
122: }
123:
124: return null;
125: }
126:
127: public static IPackageFragment findSelectedPackageFragment(
128: ISelection selection) {
129: if (selection != null) {
130: IJavaElement elem = findSelectedJavaElement(selection);
131:
132: if (elem instanceof IPackageFragment) {
133: return (IPackageFragment) elem;
134: } else if (elem instanceof IPackageDeclaration) {
135: IPackageDeclaration packageDecl = (IPackageDeclaration) elem;
136: String name = packageDecl.getElementName();
137: IPackageFragmentRoot root = JdtUtils
138: .getPackageFragmentRoot(elem);
139:
140: if (root != null) {
141: return root.getPackageFragment(name);
142: }
143: }
144: }
145:
146: return null;
147: }
148:
149: public static IField findSelectedField(ISelection selection) {
150: if (selection != null) {
151: IJavaElement elem = findSelectedJavaElement(selection);
152:
153: if (elem instanceof IField) {
154: return (IField) elem;
155: }
156: }
157:
158: return null;
159: }
160:
161: public static ICompilationUnit findSelectedCompilationUnit() {
162: ITextEditor editor = findSelectedTextEditor();
163:
164: if (editor != null) {
165: return JdtUtils.getInputAsCompilationUnit(editor);
166: }
167:
168: return null;
169: }
170:
171: public static IEditorPart findSelectedEditorPart() {
172: IWorkbenchWindow win = PlatformUI.getWorkbench()
173: .getActiveWorkbenchWindow();
174:
175: if (win != null) {
176: IWorkbenchPage page = win.getActivePage();
177:
178: if (page != null) {
179: return page.getActiveEditor();
180: }
181: }
182:
183: return null;
184: }
185:
186: public static ITextEditor findSelectedTextEditor() {
187: IEditorPart part = findSelectedEditorPart();
188:
189: if (part != null && part instanceof ITextEditor) {
190: return (ITextEditor) part;
191: }
192:
193: return null;
194: }
195:
196: public static IMethod findSelectedMethod(ISelection selection) {
197: if (selection != null) {
198: IJavaElement elem = findSelectedJavaElement(selection);
199:
200: if (elem instanceof IMethod) {
201: return (IMethod) elem;
202: }
203: }
204:
205: return null;
206: }
207:
208: public static IFile findSelectedFile(ISelection selection) {
209: if (selection != null) {
210: if (selection instanceof IStructuredSelection) {
211: IStructuredSelection ss = (IStructuredSelection) selection;
212:
213: Object obj = ss.getFirstElement();
214: if (obj instanceof IFile) {
215: return (IFile) obj;
216: }
217: }
218: }
219:
220: return null;
221: }
222:
223: public static ICompilationUnit findSelectedCompilationUnit(
224: ISelection selection) {
225: if (selection != null) {
226: if (selection instanceof IStructuredSelection) {
227: IJavaElement elem = findSelectedJavaElement((IStructuredSelection) selection);
228:
229: if (elem instanceof ICompilationUnit) {
230: return (ICompilationUnit) elem;
231: }
232: } else if (selection instanceof ITextSelection) {
233: return findSelectedCompilationUnit();
234: }
235: }
236:
237: return null;
238: }
239:
240: public static IType findSelectedType(ISelection selection) {
241: if (selection != null) {
242: IJavaElement elem = findSelectedJavaElement(selection);
243:
244: if (elem instanceof IType) {
245: return (IType) elem;
246: } else if (elem instanceof IMethod) {
247: return ((IMethod) elem).getDeclaringType();
248: } else if (elem instanceof IField) {
249: IJavaProject javaProject = elem.getJavaProject();
250: IField field = (IField) elem;
251: IType declaringType = field.getDeclaringType();
252:
253: try {
254: String sig = field.getTypeSignature();
255: String typeName = JdtUtils.getResolvedTypeName(sig,
256: declaringType);
257:
258: return JdtUtils.findType(javaProject, typeName);
259: } catch (JavaModelException jme) {/**/
260: }
261: } else if (elem instanceof ILocalVariable) {
262: IJavaProject javaProject = elem.getJavaProject();
263: ILocalVariable var = (ILocalVariable) elem;
264: IType enclosingType = (IType) elem
265: .getAncestor(IJavaElement.TYPE);
266:
267: if (enclosingType != null) {
268: try {
269: String sig = var.getTypeSignature();
270: String typeName = JdtUtils.getResolvedTypeName(
271: sig, enclosingType);
272:
273: return JdtUtils.findType(javaProject, typeName);
274: } catch (JavaModelException jme) {/**/
275: }
276: }
277: } else if (elem instanceof IImportDeclaration) {
278: IImportDeclaration importDecl = (IImportDeclaration) elem;
279: String typeName = importDecl.getElementName();
280:
281: if (!importDecl.isOnDemand()) {
282: try {
283: return JdtUtils.findType(elem.getJavaProject(),
284: typeName);
285: } catch (JavaModelException jme) {/**/
286: }
287: }
288: } else if (elem instanceof IClassFile) {
289: try {
290: return ((IClassFile) elem).getType();
291: } catch (Exception e) {/**/
292: }
293: }
294: }
295:
296: return null;
297: }
298:
299: public static IJavaElement findSelectedJavaElement(
300: ISelection selection) {
301: if (selection != null) {
302: if (selection instanceof IStructuredSelection) {
303: return findSelectedJavaElement((IStructuredSelection) selection);
304: } else if (selection instanceof ITextSelection) {
305: return findSelectedJavaElement((ITextSelection) selection);
306: }
307: }
308:
309: return null;
310: }
311:
312: public static IJavaElement findSelectedJavaElement(
313: IStructuredSelection selection) {
314: if (selection != null) {
315: Object obj = selection.getFirstElement();
316:
317: if (obj instanceof IJavaElement) {
318: return (IJavaElement) obj;
319: }
320: }
321:
322: return null;
323: }
324:
325: public static IJavaElement findSelectedJavaElement(
326: ITextSelection selection) {
327: if (selection != null) {
328: ITextEditor editor = findSelectedTextEditor();
329:
330: if (editor != null) {
331: IEditorInput input = editor.getEditorInput();
332:
333: if (input instanceof IFileEditorInput) {
334: IFile file = ((IFileEditorInput) input).getFile();
335:
336: if (TcPlugin.getDefault().hasTerracottaNature(
337: file.getProject())) {
338: try {
339: IJavaElement[] elems = JdtUtils
340: .codeResolve(editor);
341: return elems.length == 1 ? elems[0] : null;
342: //return JdtUtils.getElementAtOffset(editor);
343: } catch (JavaModelException jme) {/**/
344: }
345: }
346: } else {
347: IClassFile classFile = (IClassFile) input
348: .getAdapter(IClassFile.class);
349:
350: if (classFile != null) {
351: IJavaProject javaProj = classFile
352: .getJavaProject();
353:
354: if (TcPlugin.getDefault().hasTerracottaNature(
355: javaProj.getProject())) {
356: try {
357: IJavaElement[] elems = JdtUtils
358: .codeResolve(editor);
359: return elems.length == 1 ? elems[0]
360: : null;
361: //return JdtUtils.getElementAtOffset(editor);
362: } catch (JavaModelException jme) {/**/
363: }
364: }
365: }
366: }
367: }
368: }
369:
370: return null;
371: }
372:
373: public static String getStatusMessages(Exception e) {
374: String msg = e.getMessage();
375:
376: if (e instanceof CoreException) {
377: CoreException ce = (CoreException) e;
378: IStatus status = ce.getStatus();
379: IStatus[] children = status.getChildren();
380:
381: for (int i = 0; i < children.length; i++) {
382: msg += "\n" + children[i].getMessage();
383: }
384:
385: System.err.println(msg);
386: ce.printStackTrace(System.err);
387: }
388:
389: return msg;
390: }
391: }
|