KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dnd > CompatibilityDragTarget


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.dnd;
12
13 import org.eclipse.jface.util.Geometry;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.graphics.Rectangle;
17 import org.eclipse.swt.widgets.Control;
18
19 /**
20  * Compatibility layer for the old-style drag-and-drop. Adapts an old-style
21  * IPartDropListener into an IDragTarget.
22  *
23  */

24 public class CompatibilityDragTarget {
25
26     // Define width of part's "hot" border
27
private final static int MARGIN = 30;
28
29     /**
30      * Returns the relative position of the given point (in display coordinates)
31      * with respect to the given control. Returns one of SWT.LEFT, SWT.RIGHT, SWT.CENTER, SWT.TOP,
32      * or SWT.BOTTOM if the point is on the control or SWT.DEFAULT if the point is not on the control.
33      *
34      * @param control control to perform hit detection on
35      * @param toTest point to test, in display coordinates
36      * @return
37      */

38     public static int getRelativePosition(Control c, Point toTest) {
39         Point p = c.toControl(toTest);
40         Point e = c.getSize();
41
42         if (p.x > e.x || p.y > e.y || p.x < 0 || p.y < 0) {
43             return SWT.DEFAULT;
44         }
45
46         // first determine whether mouse position is in center of part
47
int hmargin = Math.min(e.x / 3, MARGIN);
48         int vmargin = Math.min(e.y / 3, MARGIN);
49
50         Rectangle inner = new Rectangle(hmargin, vmargin, e.x - (hmargin * 2),
51                 e.y - (vmargin * 2));
52         if (inner.contains(p)) {
53             return SWT.CENTER;
54         } else {
55             return Geometry.getClosestSide(inner, p);
56         }
57     }
58
59 }
60
Popular Tags