/*******************************************************************************
 * Copyright (c) 2013 SAP AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Tobias Pfeifer (SAP AG) - initial implementation
 *******************************************************************************/
package org.eclipse.egit.ui.internal.rebase;

import org.eclipse.egit.core.internal.rebase.RebaseInteractivePlan;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TransferData;

final class RebaseInteractiveDropTargetListener extends ViewerDropAdapter {

	private final RebaseInteractiveView rebaseInteractiveView;

	RebaseInteractiveDropTargetListener(
			RebaseInteractiveView rebaseInteractiveView, Viewer viewer) {
		super(viewer);
		this.rebaseInteractiveView = rebaseInteractiveView;
	}

	public boolean performDrop(Object data) {
		if (!this.rebaseInteractiveView.isDragAndDropEnabled())
			return false;

		DropTargetEvent event = getCurrentEvent();
		RebaseInteractivePlan.PlanElement sourceElement = null;
		if (event.data instanceof IStructuredSelection) {
			final IStructuredSelection structuredSelection = (IStructuredSelection) data;
			if (structuredSelection.size() > 1)
				return false;
			if (structuredSelection.getFirstElement() instanceof RebaseInteractivePlan.PlanElement)
				sourceElement = (RebaseInteractivePlan.PlanElement) structuredSelection
						.getFirstElement();
		}

		if (sourceElement == null)
			return false;

		Object targetObj = getCurrentTarget();
		if (!(targetObj instanceof RebaseInteractivePlan.PlanElement))
			return false;
		RebaseInteractivePlan.PlanElement targetElement = (RebaseInteractivePlan.PlanElement) targetObj;

		boolean before = false;
		switch (getCurrentLocation()) {
		case ViewerDropAdapter.LOCATION_BEFORE:
			before = true;
			break;
		case ViewerDropAdapter.LOCATION_NONE:
			return false;
		}

		if (RebaseInteractivePreferences.isOrderReversed())
			before = !before;

		rebaseInteractiveView.getCurrentPlan().moveTodoEntry(sourceElement,
				targetElement, before);
		return true;
	}

	@Override
	public void dragOver(DropTargetEvent event) {
		super.dragOver(event);
	}

	@Override
	public boolean validateDrop(Object target, int operation,
			TransferData transferType) {
		if (!this.rebaseInteractiveView.isDragAndDropEnabled())
			return false;
		if (getCurrentTarget() instanceof RebaseInteractivePlan.PlanElement) {
			switch (getCurrentLocation()) {
			case ViewerDropAdapter.LOCATION_AFTER:
				return true;
			case ViewerDropAdapter.LOCATION_ON:
				return false;
			case ViewerDropAdapter.LOCATION_BEFORE:
				return true;
			case ViewerDropAdapter.LOCATION_NONE:
				return false;
			}
		}
		return false;
	}
}
@@ -553,16 +553,98 @@
 ebase;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A
 import o
@@ -1421,71 +1421,16 @@
 ();%0A
-%09%09RebaseInteractivePlan.PlanElement sourceElement = null;
 %0A%09%09if 
+(!
 (eve
@@ -1469,20 +1469,36 @@
 lection)
- %7B%0A%09
+)%0A%09%09%09return false;%0A%0A
 %09%09final 
@@ -1575,46 +1575,110 @@
 ;%0A%09%09
-%09if (structuredSelection.size() %3E 1
+List selectionList = structuredSelection.toList();%0A%0A%09%09if (selectionList.contains(getCurrentTarget())
 )%0A
-%09
 %09%09%09r
@@ -1694,52 +1694,167 @@
 se;%0A
+%0A
 %09%09
-%09if (structuredSelection.getFirst
+List%3CRebaseInteractivePlan.PlanElement%3E sourceElements = new ArrayList%3CRebaseInteractivePlan.Plan
 Element
+%3E
 ()
+;%0A%09%09for (Object obj : selectionList) %7B%0A%09%09%09if (obj
  ins
@@ -1909,27 +1909,30 @@
 ourceElement
- = 
+s.add(
 (RebaseInter
@@ -1959,51 +1959,11 @@
 nt) 
-structuredSelection%0A%09%09%09%09%09%09.getFirstElement(
+obj
 );%0A%09
@@ -1989,16 +1989,19 @@
 ment
- == null
+s.isEmpty()
 )%0A%09%09
@@ -2427,24 +2427,80 @@
 false;%0A%09%09%7D%0A%0A
+%09%09if (!before)%0A%09%09%09Collections.reverse(sourceElements);%0A%0A
 %09%09if (Rebase
@@ -2563,16 +2563,84 @@
 efore;%0A%0A
+%09%09for (RebaseInteractivePlan.PlanElement element : sourceElements)%0A%09
 %09%09rebase
@@ -2690,23 +2690,17 @@
 try(
-sourceE
+e
 lement,%0A
 %09%09%09%09
@@ -2695,16 +2695,17 @@
 lement,%0A
+%09
 %09%09%09%09targ
@@ -2724,16 +2724,17 @@
 efore);%0A
+%0A
 %09%09return

Allow to move multiple commits by DND in the rebase interactive view