1 11 12 package org.eclipse.ui.internal.ide; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.mapping.ResourceMapping; 20 import org.eclipse.core.resources.mapping.ResourceMappingContext; 21 import org.eclipse.core.resources.mapping.ResourceTraversal; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.NullProgressMonitor; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.jface.viewers.StructuredSelection; 26 import org.eclipse.ui.ide.ResourceUtil; 27 import org.eclipse.ui.internal.ISelectionConversionService; 28 29 35 public class IDESelectionConversionService implements 36 ISelectionConversionService { 37 38 43 public IStructuredSelection convertToResources( 44 IStructuredSelection originalSelection) { 45 46 List result = new ArrayList (); 47 Iterator elements = originalSelection.iterator(); 48 49 while (elements.hasNext()) { 50 Object currentElement = elements.next(); 51 52 IResource resource = ResourceUtil.getResource(currentElement); 53 54 if (resource == null) { 55 56 ResourceMapping mapping = ResourceUtil 57 .getResourceMapping(currentElement); 58 if (mapping == null) 59 continue; 60 61 ResourceTraversal[] traversals = null; 62 try { 63 traversals = mapping.getTraversals( 64 ResourceMappingContext.LOCAL_CONTEXT, 65 new NullProgressMonitor()); 66 } catch (CoreException e) { 67 IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e 68 .getStatus()); 69 } 70 if (traversals != null) { 71 ResourceTraversal traversal = null; 72 IResource[] resources = null; 73 for (int i = 0; i < traversals.length; i++) { 74 traversal = traversals[i]; 75 resources = traversal.getResources(); 76 if (resources != null) { 77 for (int j = 0; j < resources.length; j++) { 78 result.add(resources[j]); 79 } 80 } 81 } 82 } 83 84 } else 85 result.add(resource); 86 } 87 88 if (result.isEmpty()) { 90 return StructuredSelection.EMPTY; 91 } 92 return new StructuredSelection(result.toArray()); 93 } 94 } 95 | Popular Tags |