1 11 package org.eclipse.jdt.internal.ui.packageview; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 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 [] fElementsToAdds; 54 private Set 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 64 public Transfer getTransfer() { 65 return LocalSelectionTransfer.getInstance(); 66 } 67 68 public boolean isEnabled(DropTargetEvent event) { 69 Object 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 85 public void validateDrop(Object 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 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 realJavaElements= new ArrayList (); 123 List realResource= new ArrayList (); 124 ReorgUtils.splitIntoJavaElementsAndResources(fElementsToAdds, realJavaElements, realResource); 125 if (fElementsToAdds.length != realJavaElements.size() + realResource.size()) 126 return DND.DROP_NONE; 127 for (Iterator 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 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 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 target, ISelection s) { 173 fWorkingSet= (IWorkingSet)target; 174 fSelection= (IStructuredSelection)s; 175 fElementsToAdds= fSelection.toArray(); 176 fCurrentElements= new HashSet (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 target, final DropTargetEvent event) { 188 if (isWorkingSetSelection()) { 189 performWorkingSetReordering(); 190 } else { 191 performElementRearrange(event.detail); 192 } 193 event.detail= DND.DROP_NONE; 195 } 196 197 private void performWorkingSetReordering() { 198 WorkingSetModel model= fPackageExplorer.getWorkingSetModel(); 199 List activeWorkingSets= new ArrayList (Arrays.asList(model.getActiveWorkingSets())); 200 int index= activeWorkingSets.indexOf(fWorkingSet); 201 if (index != -1) { 202 if (fLocation == LOCATION_AFTER) 203 index++; 204 List result= new ArrayList (activeWorkingSets.size()); 205 List selected= new ArrayList (Arrays.asList(fElementsToAdds)); 206 for (int i= 0; i < activeWorkingSets.size(); i++) { 207 if (i == index) { 208 result.addAll(selected); 209 } 210 Object 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 if (!isOthersWorkingSet(fWorkingSet)) { 225 List elements= new ArrayList (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 workingSets= groupByWorkingSets(treeSelection.getPaths()); 232 for (Iterator iter= workingSets.keySet().iterator(); iter.hasNext();) { 233 IWorkingSet ws= (IWorkingSet)iter.next(); 234 List toRemove= (List )workingSets.get(ws); 235 List currentElements= new ArrayList (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 groupByWorkingSets(TreePath[] paths) { 243 Map result= new HashMap (); 244 for (int i= 0; i < paths.length; i++) { 245 TreePath path= paths[i]; 246 IWorkingSet ws= (IWorkingSet)path.getSegment(0); 247 List l= (List )result.get(ws); 248 if (l == null) { 249 l= new ArrayList (); 250 result.put(ws, l); 251 } 252 l.add(path.getSegment(1)); 253 } 254 return result; 255 } 256 257 259 public int internalTestValidateTarget(Object target, int operation) { 260 return validateTarget(target, operation); 261 } 262 263 public void internalTestDrop(Object target, int eventDetail) { 264 if (isWorkingSetSelection()) { 265 performWorkingSetReordering(); 266 } else { 267 performElementRearrange(eventDetail); 268 } 269 } 270 } 271 | Popular Tags |