KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > RemoveWorkingSetElementAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.workingsets;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
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 JavaDoc elements= treeSelection.toList();
46         IWorkingSet result= null;
47         for (Iterator JavaDoc iter= elements.iterator(); iter.hasNext();) {
48             Object JavaDoc 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 JavaDoc 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 JavaDoc elements= new HashSet JavaDoc(Arrays.asList(ws.getElements()));
73         List JavaDoc selectedElements= selection.toList();
74         for (Iterator JavaDoc iter= selectedElements.iterator(); iter.hasNext();) {
75             Object JavaDoc 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