001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.dnd;
011:
012: import org.eclipse.swt.dnd.DND;
013: import org.eclipse.swt.dnd.DropTargetEvent;
014: import org.eclipse.swt.dnd.TransferData;
015: import org.eclipse.swt.graphics.Point;
016: import org.eclipse.swt.graphics.Rectangle;
017: import org.eclipse.swt.widgets.Item;
018:
019: import org.eclipse.jface.viewers.ISelection;
020: import org.eclipse.jface.viewers.StructuredViewer;
021:
022: import org.eclipse.jdt.internal.ui.packageview.SelectionTransferDropAdapter;
023:
024: public abstract class ViewerInputDropAdapter extends
025: SelectionTransferDropAdapter {
026:
027: private static final int ITEM_MARGIN_LEFT = 40;
028: private static final int ITEM_MARGIN_RIGTH = 10;
029: private static final int OPERATION = DND.DROP_LINK;
030:
031: public ViewerInputDropAdapter(StructuredViewer viewer) {
032: super (viewer);
033: }
034:
035: protected abstract Object getInputElement(ISelection selection);
036:
037: protected abstract void doInputView(Object inputElement);
038:
039: /**
040: * {@inheritDoc}
041: */
042: protected int determineOperation(Object target, int operation,
043: TransferData transferType, int operations) {
044:
045: setSelectionFeedbackEnabled(true);
046: setExpandEnabled(true);
047:
048: initializeSelection();
049:
050: if (target != null) {
051: return super .determineOperation(target, operation,
052: transferType, operations);
053: } else if (getInputElement(getSelection()) != null) {
054: setSelectionFeedbackEnabled(false);
055: setExpandEnabled(false);
056: return OPERATION;
057: } else {
058: return DND.DROP_NONE;
059: }
060: }
061:
062: /**
063: * {@inheritDoc}
064: */
065: public boolean performDrop(Object data) {
066: setSelectionFeedbackEnabled(true);
067: setExpandEnabled(true);
068:
069: if (getCurrentTarget() != null
070: || getCurrentOperation() != OPERATION) {
071: return super .performDrop(data);
072: }
073:
074: Object input = getInputElement(getSelection());
075: if (input != null)
076: doInputView(input);
077:
078: return false;
079: }
080:
081: /**
082: * {@inheritDoc}
083: */
084: public boolean isEnabled(DropTargetEvent event) {
085: return true;
086: }
087:
088: /**
089: * {@inheritDoc}
090: */
091: protected Object determineTarget(DropTargetEvent event) {
092: if (event.item == null)
093: return super .determineTarget(event);
094:
095: Point coordinates = getViewer().getControl().toControl(
096: new Point(event.x, event.y));
097: Rectangle bounds = getBounds((Item) event.item);
098: if (coordinates.x < bounds.x - ITEM_MARGIN_LEFT
099: || coordinates.x >= bounds.x + bounds.width
100: + ITEM_MARGIN_RIGTH) {
101: event.item = null; // too far away
102: return null;
103: }
104:
105: return super.determineTarget(event);
106: }
107:
108: }
|