KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > WorkingSetDropAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.packageview;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.runtime.IAdaptable;
23
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.swt.dnd.DND;
27 import org.eclipse.swt.dnd.DropTargetEvent;
28 import org.eclipse.swt.dnd.Transfer;
29
30 import org.eclipse.jface.util.TransferDropTargetListener;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.ITreeSelection;
34 import org.eclipse.jface.viewers.TreePath;
35
36 import org.eclipse.ui.IWorkingSet;
37
38 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
39
40 import org.eclipse.jdt.core.IJavaElement;
41
42 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
43
44 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropAdapter;
45 import org.eclipse.jdt.internal.ui.workingsets.OthersWorkingSetUpdater;
46 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetModel;
47
48 public class WorkingSetDropAdapter extends JdtViewerDropAdapter implements TransferDropTargetListener {
49     
50     private PackageExplorerPart fPackageExplorer;
51     
52     private IStructuredSelection fSelection;
53     private Object JavaDoc[] fElementsToAdds;
54     private Set JavaDoc fCurrentElements;
55     private IWorkingSet fWorkingSet;
56
57     public WorkingSetDropAdapter(PackageExplorerPart part) {
58         super(part.getTreeViewer(), DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND);
59         fPackageExplorer= part;
60     }
61
62     //---- TransferDropTargetListener interface ---------------------------------------
63

64     public Transfer getTransfer() {
65         return LocalSelectionTransfer.getInstance();
66     }
67     
68     public boolean isEnabled(DropTargetEvent event) {
69         Object JavaDoc target= event.item != null ? event.item.getData() : null;
70         if (target == null)
71             return false;
72         ISelection selection= LocalSelectionTransfer.getInstance().getSelection();
73         if (!isValidSelection(selection)) {
74             return false;
75         }
76         if (!isValidTarget(target))
77             return false;
78         
79         initializeState(target, selection);
80         return true;
81     }
82
83     //---- Actual DND -----------------------------------------------------------------
84

85     public void validateDrop(Object JavaDoc target, DropTargetEvent event, int operation) {
86         event.detail= DND.DROP_NONE;
87         switch(operation) {
88             case DND.DROP_DEFAULT:
89             case DND.DROP_COPY:
90             case DND.DROP_MOVE:
91                 event.detail= validateTarget(target, operation);
92                 break;
93             case DND.DROP_LINK:
94                 event.detail= DND.DROP_NONE;
95                 break;
96         }
97     }
98     
99     private int validateTarget(Object JavaDoc target, int operation) {
100         showInsertionFeedback(false);
101         setDefaultFeedback(DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND);
102         if (!isValidTarget(target))
103             return DND.DROP_NONE;
104         ISelection s= LocalSelectionTransfer.getInstance().getSelection();
105         if (!isValidSelection(s)) {
106             return DND.DROP_NONE;
107         }
108         
109         initializeState(target, s);
110         
111         if (isWorkingSetSelection()) {
112             setDefaultFeedback(DND.FEEDBACK_SCROLL);
113             if (fLocation == LOCATION_BEFORE || fLocation == LOCATION_AFTER) {
114                 showInsertionFeedback(true);
115                 return DND.DROP_MOVE;
116             }
117             return DND.DROP_NONE;
118         } else {
119             if (isOthersWorkingSet(fWorkingSet) && operation == DND.DROP_COPY)
120                 return DND.DROP_NONE;
121             
122             List JavaDoc realJavaElements= new ArrayList JavaDoc();
123             List JavaDoc realResource= new ArrayList JavaDoc();
124             ReorgUtils.splitIntoJavaElementsAndResources(fElementsToAdds, realJavaElements, realResource);
125             if (fElementsToAdds.length != realJavaElements.size() + realResource.size())
126                 return DND.DROP_NONE;
127             for (Iterator JavaDoc iter= realJavaElements.iterator(); iter.hasNext();) {
128                 IJavaElement element= (IJavaElement)iter.next();
129                 if (ReorgUtils.containsElementOrParent(fCurrentElements, element))
130                     return DND.DROP_NONE;
131             }
132             for (Iterator JavaDoc iter= realResource.iterator(); iter.hasNext();) {
133                 IResource element= (IResource)iter.next();
134                 if (ReorgUtils.containsElementOrParent(fCurrentElements, element))
135                     return DND.DROP_NONE;
136             }
137             if (!(fSelection instanceof ITreeSelection)) {
138                 return DND.DROP_COPY;
139             }
140             ITreeSelection treeSelection= (ITreeSelection)fSelection;
141             TreePath[] paths= treeSelection.getPaths();
142             for (int i= 0; i < paths.length; i++) {
143                 TreePath path= paths[i];
144                 if (path.getSegmentCount() != 2)
145                     return DND.DROP_COPY;
146                 if (!(path.getSegment(0) instanceof IWorkingSet))
147                     return DND.DROP_COPY;
148                 if (paths.length == 1) {
149                     IWorkingSet ws= (IWorkingSet)path.getSegment(0);
150                     if (OthersWorkingSetUpdater.ID.equals(ws.getId()))
151                         return DND.DROP_MOVE;
152                 }
153             }
154         }
155         if (operation == DND.DROP_DEFAULT)
156             return DND.DROP_MOVE;
157         return operation;
158     }
159
160     private boolean isValidTarget(Object JavaDoc target) {
161         return target instanceof IWorkingSet;
162     }
163     
164     private boolean isValidSelection(ISelection selection) {
165         return selection instanceof IStructuredSelection;
166     }
167     
168     private boolean isOthersWorkingSet(IWorkingSet ws) {
169         return OthersWorkingSetUpdater.ID.equals(ws.getId());
170     }
171     
172     private void initializeState(Object JavaDoc target, ISelection s) {
173         fWorkingSet= (IWorkingSet)target;
174         fSelection= (IStructuredSelection)s;
175         fElementsToAdds= fSelection.toArray();
176         fCurrentElements= new HashSet JavaDoc(Arrays.asList(fWorkingSet.getElements()));
177     }
178     
179     private boolean isWorkingSetSelection() {
180         for (int i= 0; i < fElementsToAdds.length; i++) {
181             if (!(fElementsToAdds[i] instanceof IWorkingSet))
182                 return false;
183         }
184         return true;
185     }
186
187     public void drop(Object JavaDoc target, final DropTargetEvent event) {
188         if (isWorkingSetSelection()) {
189             performWorkingSetReordering();
190         } else {
191             performElementRearrange(event.detail);
192         }
193         // drag adapter has nothing to do, even on move.
194
event.detail= DND.DROP_NONE;
195     }
196
197     private void performWorkingSetReordering() {
198         WorkingSetModel model= fPackageExplorer.getWorkingSetModel();
199         List JavaDoc activeWorkingSets= new ArrayList JavaDoc(Arrays.asList(model.getActiveWorkingSets()));
200         int index= activeWorkingSets.indexOf(fWorkingSet);
201         if (index != -1) {
202             if (fLocation == LOCATION_AFTER)
203                 index++;
204             List JavaDoc result= new ArrayList JavaDoc(activeWorkingSets.size());
205             List JavaDoc selected= new ArrayList JavaDoc(Arrays.asList(fElementsToAdds));
206             for (int i= 0; i < activeWorkingSets.size(); i++) {
207                 if (i == index) {
208                     result.addAll(selected);
209                 }
210                 Object JavaDoc element= activeWorkingSets.get(i);
211                 if (!selected.contains(element)) {
212                     result.add(element);
213                 }
214             }
215             if (index == activeWorkingSets.size())
216                 result.addAll(selected);
217             model.setActiveWorkingSets((IWorkingSet[])result.toArray(new IWorkingSet[result.size()]));
218         }
219     }
220     
221     private void performElementRearrange(int eventDetail) {
222         // only move if target isn't the other working set. If this is the case
223
// the move will happenn automatically by refreshing the other working set
224
if (!isOthersWorkingSet(fWorkingSet)) {
225             List JavaDoc elements= new ArrayList JavaDoc(Arrays.asList(fWorkingSet.getElements()));
226             elements.addAll(Arrays.asList(fElementsToAdds));
227             fWorkingSet.setElements((IAdaptable[])elements.toArray(new IAdaptable[elements.size()]));
228         }
229         if (eventDetail == DND.DROP_MOVE) {
230             ITreeSelection treeSelection= (ITreeSelection)fSelection;
231             Map JavaDoc workingSets= groupByWorkingSets(treeSelection.getPaths());
232             for (Iterator JavaDoc iter= workingSets.keySet().iterator(); iter.hasNext();) {
233                 IWorkingSet ws= (IWorkingSet)iter.next();
234                 List JavaDoc toRemove= (List JavaDoc)workingSets.get(ws);
235                 List JavaDoc currentElements= new ArrayList JavaDoc(Arrays.asList(ws.getElements()));
236                 currentElements.removeAll(toRemove);
237                 ws.setElements((IAdaptable[])currentElements.toArray(new IAdaptable[currentElements.size()]));
238             }
239         }
240     }
241
242     private Map JavaDoc/*<List<IWorkingSet>>*/ groupByWorkingSets(TreePath[] paths) {
243         Map JavaDoc result= new HashMap JavaDoc();
244         for (int i= 0; i < paths.length; i++) {
245             TreePath path= paths[i];
246             IWorkingSet ws= (IWorkingSet)path.getSegment(0);
247             List JavaDoc l= (List JavaDoc)result.get(ws);
248             if (l == null) {
249                 l= new ArrayList JavaDoc();
250                 result.put(ws, l);
251             }
252             l.add(path.getSegment(1));
253         }
254         return result;
255     }
256     
257     //---- test methods for JUnit test since DnD is hard to simulate
258

259     public int internalTestValidateTarget(Object JavaDoc target, int operation) {
260         return validateTarget(target, operation);
261     }
262     
263     public void internalTestDrop(Object JavaDoc target, int eventDetail) {
264         if (isWorkingSetSelection()) {
265             performWorkingSetReordering();
266         } else {
267             performElementRearrange(eventDetail);
268         }
269     }
270 }
271
Popular Tags