KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > JavaWorkingSetElementAdapter


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.workingsets;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21
22 import org.eclipse.ui.IWorkingSet;
23 import org.eclipse.ui.IWorkingSetElementAdapter;
24
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.JavaCore;
27
28 public class JavaWorkingSetElementAdapter implements IWorkingSetElementAdapter {
29
30     public IAdaptable[] adaptElements(IWorkingSet ws, IAdaptable[] elements) {
31         ArrayList JavaDoc result= new ArrayList JavaDoc(elements.length);
32         
33         for (int i= 0; i < elements.length; i++) {
34             IAdaptable curr= elements[i];
35             if (curr instanceof IJavaElement) {
36                 result.add(curr);
37             } else if (curr instanceof IResource) {
38                 result.add(adaptFromResource((IResource) curr));
39             } else {
40                 Object JavaDoc elem= curr.getAdapter(IJavaElement.class);
41                 if (elem == null) {
42                     elem= curr.getAdapter(IResource.class);
43                     if (elem != null) {
44                         elem= adaptFromResource((IResource) elem);
45                     }
46                 }
47                 if (elem != null) {
48                     result.add(elem);
49                 } // ignore all others
50
}
51         }
52         return (IAdaptable[]) result.toArray(new IAdaptable[result.size()]);
53     }
54     
55     private Object JavaDoc adaptFromResource(IResource resource) {
56         IProject project= resource.getProject();
57         if (project != null && project.isAccessible()) {
58             try {
59                 if (project.hasNature(JavaCore.NATURE_ID)) {
60                     IJavaElement elem= JavaCore.create(resource);
61                     if (elem != null) {
62                         return elem;
63                     }
64                 }
65             } catch (CoreException e) {
66                 // ignore
67
}
68         }
69         return resource;
70     }
71     
72
73     public void dispose() {
74     }
75
76 }
77
Popular Tags