001: package net.refractions.udig.internal.ui;
002:
003: import net.refractions.udig.ui.UDIGDropTargetListener;
004: import net.refractions.udig.ui.ViewerDropLocation;
005:
006: import org.eclipse.jface.viewers.Viewer;
007: import org.eclipse.jface.viewers.ViewerDropAdapter;
008: import org.eclipse.swt.dnd.DropTargetEvent;
009: import org.eclipse.swt.dnd.TransferData;
010: import org.eclipse.swt.graphics.Point;
011: import org.eclipse.swt.graphics.Rectangle;
012: import org.eclipse.swt.widgets.Item;
013:
014: public class UDIGViewerDropAdapter extends ViewerDropAdapter implements
015: UDIGDropTargetListener {
016:
017: UDIGDropHandler handler;
018: private IDropTargetProvider defaultTarget;
019:
020: public UDIGViewerDropAdapter(Viewer viewer,
021: IDropTargetProvider defaultTarget) {
022: super (viewer);
023: setFeedbackEnabled(true);
024: handler = new UDIGDropHandler();
025: this .defaultTarget = defaultTarget;
026: }
027:
028: private void initHandler(DropTargetEvent event) {
029: Object target = super .determineTarget(event);
030: if (target == null)
031: target = defaultTarget.getTarget(event);
032: handler.setTarget(target);
033: handler.setViewerLocation(ViewerDropLocation
034: .valueOf(getCurrentLocation()));
035: }
036:
037: public UDIGDropHandler getDropHandler() {
038: return handler;
039: }
040:
041: @Override
042: public void dragEnter(DropTargetEvent event) {
043: initHandler(event);
044: handler.dragEnter(event);
045: }
046:
047: @Override
048: public void dragOperationChanged(DropTargetEvent event) {
049: initHandler(event);
050: handler.dragOperationChanged(event);
051: }
052:
053: @Override
054: public void dragOver(DropTargetEvent event) {
055: initHandler(event);
056: super .dragOver(event);
057: handler.dragOver(event);
058: }
059:
060: @Override
061: public void dropAccept(DropTargetEvent event) {
062: initHandler(event);
063: handler.dropAccept(event);
064: }
065:
066: @Override
067: public void drop(DropTargetEvent event) {
068: initHandler(event);
069: handler.drop(event);
070: }
071:
072: @Override
073: public boolean performDrop(Object data) {
074: handler.performDrop(data, null);
075: return false;
076: }
077:
078: @Override
079: public boolean validateDrop(Object target, int operation,
080: TransferData transferType) {
081: return true;
082: }
083:
084: public IDropTargetProvider getDropTargetProvider() {
085: return defaultTarget;
086: }
087:
088: public void setDropTargetProvider(IDropTargetProvider newProvider) {
089: defaultTarget = newProvider;
090: }
091:
092: @Override
093: protected int determineLocation(DropTargetEvent event) {
094: if (!(event.item instanceof Item)) {
095: return LOCATION_NONE;
096: }
097: Item item = (Item) event.item;
098: Point coordinates = new Point(event.x, event.y);
099: coordinates = getViewer().getControl().toControl(coordinates);
100: if (item != null) {
101: Rectangle bounds = getBounds(item);
102: if (bounds == null) {
103: return LOCATION_NONE;
104: }
105: int thirdOfItem = bounds.height / 3;
106: if ((coordinates.y - bounds.y) < thirdOfItem) {
107: return LOCATION_BEFORE;
108: }
109: if ((bounds.y + bounds.height - coordinates.y) < thirdOfItem) {
110: return LOCATION_AFTER;
111: }
112: }
113: return LOCATION_ON;
114: }
115:
116: }
|