1 11 package org.eclipse.jdt.internal.ui.util; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IResource; 19 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 24 import org.eclipse.ui.IWorkbenchPage; 25 import org.eclipse.ui.IWorkbenchPart; 26 import org.eclipse.ui.IWorkbenchPartReference; 27 import org.eclipse.ui.IWorkbenchWindow; 28 import org.eclipse.ui.part.ISetSelectionTarget; 29 30 public class SelectionUtil { 31 32 public static List toList(ISelection selection) { 33 if (selection instanceof IStructuredSelection) 34 return ((IStructuredSelection) selection).toList(); 35 return null; 36 } 37 38 45 public static Object getSingleElement(ISelection s) { 46 if (! (s instanceof IStructuredSelection)) 47 return null; 48 IStructuredSelection selection= (IStructuredSelection) s; 49 if (selection.size() != 1) 50 return null; 51 52 return selection.getFirstElement(); 53 } 54 55 56 73 public static void selectAndReveal(IResource[] resources, IWorkbenchWindow window) { 74 if (window == null || resources == null || Arrays.asList(resources).contains(null)) { 76 return; 77 } 78 IWorkbenchPage page= window.getActivePage(); 79 if (page == null) { 80 return; 81 } 82 83 List parts= new ArrayList (); 85 IWorkbenchPartReference refs[]= page.getViewReferences(); 86 for (int i= 0; i < refs.length; i++) { 87 IWorkbenchPart part= refs[i].getPart(false); 88 if (part != null) { 89 parts.add(part); 90 } 91 } 92 refs= page.getEditorReferences(); 93 for (int i= 0; i < refs.length; i++) { 94 if (refs[i].getPart(false) != null) { 95 parts.add(refs[i].getPart(false)); 96 } 97 } 98 99 final ISelection selection= new StructuredSelection(resources); 100 Iterator itr= parts.iterator(); 101 while (itr.hasNext()) { 102 IWorkbenchPart part= (IWorkbenchPart) itr.next(); 103 104 ISetSelectionTarget target= null; 106 if (part instanceof ISetSelectionTarget) { 107 target= (ISetSelectionTarget) part; 108 } else { 109 target= (ISetSelectionTarget) part.getAdapter(ISetSelectionTarget.class); 110 } 111 112 if (target != null) { 113 final ISetSelectionTarget finalTarget= target; 115 window.getShell().getDisplay().asyncExec(new Runnable () { 116 public void run() { 117 finalTarget.selectReveal(selection); 118 } 119 }); 120 } 121 } 122 } 123 124 } 125 | Popular Tags |