01: /*******************************************************************************
02: * Copyright (c) 2004, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.dnd;
11:
12: import org.eclipse.jface.util.Geometry;
13: import org.eclipse.swt.SWT;
14: import org.eclipse.swt.graphics.Point;
15: import org.eclipse.swt.graphics.Rectangle;
16: import org.eclipse.swt.widgets.Control;
17:
18: /**
19: * Compatibility layer for the old-style drag-and-drop. Adapts an old-style
20: * IPartDropListener into an IDragTarget.
21: *
22: */
23: public class CompatibilityDragTarget {
24:
25: // Define width of part's "hot" border
26: private final static int MARGIN = 30;
27:
28: /**
29: * Returns the relative position of the given point (in display coordinates)
30: * with respect to the given control. Returns one of SWT.LEFT, SWT.RIGHT, SWT.CENTER, SWT.TOP,
31: * or SWT.BOTTOM if the point is on the control or SWT.DEFAULT if the point is not on the control.
32: *
33: * @param control control to perform hit detection on
34: * @param toTest point to test, in display coordinates
35: * @return
36: */
37: public static int getRelativePosition(Control c, Point toTest) {
38: Point p = c.toControl(toTest);
39: Point e = c.getSize();
40:
41: if (p.x > e.x || p.y > e.y || p.x < 0 || p.y < 0) {
42: return SWT.DEFAULT;
43: }
44:
45: // first determine whether mouse position is in center of part
46: int hmargin = Math.min(e.x / 3, MARGIN);
47: int vmargin = Math.min(e.y / 3, MARGIN);
48:
49: Rectangle inner = new Rectangle(hmargin, vmargin, e.x
50: - (hmargin * 2), e.y - (vmargin * 2));
51: if (inner.contains(p)) {
52: return SWT.CENTER;
53: } else {
54: return Geometry.getClosestSide(inner, p);
55: }
56: }
57:
58: }
|