1 11 12 package org.eclipse.jdt.internal.ui.workingsets; 13 14 import java.util.ArrayList ; 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 result= new ArrayList (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 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 } } 51 } 52 return (IAdaptable[]) result.toArray(new IAdaptable[result.size()]); 53 } 54 55 private Object 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 } 68 } 69 return resource; 70 } 71 72 73 public void dispose() { 74 } 75 76 } 77 | Popular Tags |