1 11 package org.eclipse.jdt.internal.ui.workingsets; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.IAdaptable; 19 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.viewers.ITreeSelection; 22 import org.eclipse.jface.viewers.TreePath; 23 24 import org.eclipse.ui.IWorkbenchSite; 25 import org.eclipse.ui.IWorkingSet; 26 27 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 28 29 public class RemoveWorkingSetElementAction extends SelectionDispatchAction { 30 31 public RemoveWorkingSetElementAction(IWorkbenchSite site) { 32 super(site); 33 setText(WorkingSetMessages.RemoveWorkingSetElementAction_label); 34 } 35 36 public void selectionChanged(IStructuredSelection selection) { 37 IWorkingSet workingSet= getWorkingSet(selection); 38 setEnabled(workingSet != null && !OthersWorkingSetUpdater.ID.equals(workingSet.getId())); 39 } 40 41 private IWorkingSet getWorkingSet(IStructuredSelection selection) { 42 if (!(selection instanceof ITreeSelection)) 43 return null; 44 ITreeSelection treeSelection= (ITreeSelection)selection; 45 List elements= treeSelection.toList(); 46 IWorkingSet result= null; 47 for (Iterator iter= elements.iterator(); iter.hasNext();) { 48 Object element= iter.next(); 49 TreePath[] paths= treeSelection.getPathsFor(element); 50 if (paths.length != 1) 51 return null; 52 TreePath path= paths[0]; 53 if (path.getSegmentCount() != 2) 54 return null; 55 Object candidate= path.getSegment(0); 56 if (!(candidate instanceof IWorkingSet)) 57 return null; 58 if (result == null) { 59 result= (IWorkingSet)candidate; 60 } else { 61 if (result != candidate) 62 return null; 63 } 64 } 65 return result; 66 } 67 68 public void run(IStructuredSelection selection) { 69 IWorkingSet ws= getWorkingSet(selection); 70 if (ws == null) 71 return; 72 HashSet elements= new HashSet (Arrays.asList(ws.getElements())); 73 List selectedElements= selection.toList(); 74 for (Iterator iter= selectedElements.iterator(); iter.hasNext();) { 75 Object object= iter.next(); 76 if (object instanceof IAdaptable) { 77 IAdaptable[] adaptedElements= ws.adaptElements(new IAdaptable[] {(IAdaptable)object}); 78 if (adaptedElements.length == 1) { 79 elements.remove(adaptedElements[0]); 80 } 81 } 82 } 83 ws.setElements((IAdaptable[])elements.toArray(new IAdaptable[elements.size()])); 84 } 85 } 86 | Popular Tags |