1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.dnd;
14 import org.eclipse.swt.dnd.DND;
15 import org.eclipse.swt.dnd.DropTargetEvent;
16 import org.eclipse.swt.dnd.TransferData;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Item;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.StructuredViewer;
24 import org.eclipse.jdt.internal.ui.packageview.SelectionTransferDropAdapter;
26 public abstract class ViewerInputDropAdapter extends SelectionTransferDropAdapter {
28 private static final int ITEM_MARGIN_LEFT= 40;
29 private static final int ITEM_MARGIN_RIGTH= 10;
30 private static final int OPERATION= DND.DROP_LINK;
32 public ViewerInputDropAdapter(StructuredViewer viewer) {
36 protected abstract Object getInputElement(ISelection selection);
38 protected abstract void doInputView(Object inputElement);
44 protected int determineOperation(Object target, int operation, TransferData transferType, int operations) {
46 setSelectionFeedbackEnabled(true);
47 setExpandEnabled(true);
49 initializeSelection();
52 return super.determineOperation(target, operation, transferType, operations);
53 } else if (getInputElement(getSelection()) != null) {
54 setSelectionFeedbackEnabled(false);
55 setExpandEnabled(false);
66 public boolean performDrop(Object data) {
67 setSelectionFeedbackEnabled(true);
68 setExpandEnabled(true);
70 if (getCurrentTarget() != null || getCurrentOperation() != OPERATION) {
71 return super.performDrop(data);
74 Object input= getInputElement(getSelection());
87 public boolean isEnabled(DropTargetEvent event) {
95 protected Object determineTarget(DropTargetEvent event) {
96 if (event.item == null)
97 return super.determineTarget(event);
99 Point coordinates= getViewer().getControl().toControl(new Point(event.x, event.y));
100 Rectangle bounds= getBounds((Item) event.item);
101 if (coordinates.x < bounds.x - ITEM_MARGIN_LEFT || coordinates.x >= bounds.x + bounds.width + ITEM_MARGIN_RIGTH) {
102 event.item= null; // too far away
106 return super.determineTarget(event);