1 11 package org.eclipse.ui.views.navigator; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.viewers.StructuredSelection; 21 22 30 public class ResourceSelectionUtil { 31 34 private ResourceSelectionUtil() { 35 } 36 37 49 public static boolean allResourcesAreOfType(IStructuredSelection selection, 50 int resourceMask) { 51 Iterator resources = selection.iterator(); 52 while (resources.hasNext()) { 53 Object next = resources.next(); 54 if (!(next instanceof IResource)) { 55 return false; 56 } 57 if (!resourceIsType((IResource) next, resourceMask)) { 58 return false; 59 } 60 } 61 return true; 62 } 63 64 74 public static IStructuredSelection allResources( 75 IStructuredSelection selection, int resourceMask) { 76 Iterator adaptables = selection.iterator(); 77 List result = new ArrayList (); 78 while (adaptables.hasNext()) { 79 Object next = adaptables.next(); 80 if (next instanceof IAdaptable) { 81 Object resource = ((IAdaptable) next) 82 .getAdapter(IResource.class); 83 if (resource == null) { 84 return null; 85 } else if (resourceIsType((IResource) resource, resourceMask)) { 86 result.add(resource); 87 } 88 } else { 89 return null; 90 } 91 } 92 return new StructuredSelection(result); 93 94 } 95 96 107 public static boolean resourceIsType(IResource resource, int resourceMask) { 108 return (resource.getType() & resourceMask) != 0; 109 } 110 111 } 112 | Popular Tags |