Example usage for org.eclipse.jface.util Geometry getClosestSide

List of usage examples for org.eclipse.jface.util Geometry getClosestSide

Introduction

In this page you can find the example usage for org.eclipse.jface.util Geometry getClosestSide.

Prototype

public static int getClosestSide(Rectangle boundary, Point toTest) 

Source Link

Document

Returns the edge of the given rectangle is closest to the given point.

Usage

From source file:org.eclipse.papyrus.infra.core.sasheditor.internal.TabFolderPart.java

License:Open Source License

/**
 * /*from ww  w .j  a  va 2  s  .  co  m*/
 * @see org.eclipse.papyrus.infra.core.sasheditor.internal.AbstractPanelPart#getDropTarget(java.lang.Object,
 *      org.eclipse.papyrus.infra.core.sasheditor.internal.TabFolderPart, org.eclipse.swt.graphics.Point)
 * 
 * @param draggedObject
 * @param sourcePart
 * @param position
 * @return
 */
public IDropTarget getDropTarget(Object draggedObject, TabFolderPart sourcePart, Point position) {
    // see org.eclipse.ui.internal.presentations.util.ReplaceDragHandler
    // Determine which tab we're currently dragging over
    CTabItem tabUnderPointer = pTabFolder.getItem(position);

    // Compute source tab index. If folder, index==-1
    int sourceIndex = PTabFolder.getDraggedObjectTabIndex(draggedObject);
    // This drop target only deals with tabs... if we're not dragging over
    // a tab, exit.
    if (tabUnderPointer == null) {
        Rectangle titleArea = pTabFolder.getTabArea();

        // If we're dragging over the title area, treat this as a drop in the last
        // tab position.
        if (titleArea.contains(position) && pTabFolder.getTabFolder().getItemCount() > 0) {
            int dragOverIndex = pTabFolder.getTabFolder().getItemCount();
            CTabItem lastTab = pTabFolder.getTabFolder().getItem(dragOverIndex - 1);

            // Can't drag to end unless you can see the end
            if (!lastTab.isShowing()) {
                return null;
            }

            // If we are unable to compute the bounds for this tab, then ignore the drop
            Rectangle lastTabBounds = lastTab.getBounds();
            if (lastTabBounds.isEmpty()) {
                return null;
            }

            // if (dragStart >= 0) {
            // dragOverIndex--;
            //
            // return createDropTarget( sourcePart, lastTabBounds, dragOverIndex);
            // // return new StackDropResult(lastTabBounds, new Integer(dragOverIndex));
            // }

            // Make the drag-over rectangle look like a tab at the end of the tab region.
            // We don't actually know how wide the tab will be when it's dropped, so just
            // make it 3 times wider than it is tall.
            // titleArea is in Display coordinate, lastTabBounds in parent coordinate
            Rectangle dropRectangle = titleArea;

            dropRectangle.x = dropRectangle.x + lastTabBounds.x + lastTabBounds.width;
            dropRectangle.width = 3 * dropRectangle.height;
            return createDropTarget(sourcePart, sourceIndex, dropRectangle, dragOverIndex);
            // return new StackDropResult(dropRectangle, new Integer(dragOverIndex));

        } else {
            // If the closest side is the side with the tabs, consider this a stack operation.
            // Otherwise, let the drop fall through to whatever the default behavior is
            Rectangle displayBounds = DragUtil.getDisplayBounds(pTabFolder.getControl());
            int closestSide = Geometry.getClosestSide(displayBounds, position);
            if (closestSide == pTabFolder.getTabFolder().getTabPosition()) {
                return createDropTarget(sourcePart, sourceIndex, displayBounds, -1);
            }

            return null;
        }
    }

    if (!tabUnderPointer.isShowing()) {
        return null;
    }

    // Get thumbnail bounds in display coordinates
    Rectangle tabBounds = pTabFolder.getItemBounds(tabUnderPointer);

    if (tabBounds.isEmpty()) {
        return null;
    }

    return createDropTarget(sourcePart, sourceIndex, tabBounds,
            pTabFolder.getTabFolder().indexOf(tabUnderPointer));
}

From source file:org.eclipse.ui.internal.dnd.CompatibilityDragTarget.java

License:Open Source License

/**
 * Returns the relative position of the given point (in display coordinates)
 * with respect to the given control. Returns one of SWT.LEFT, SWT.RIGHT, SWT.CENTER, SWT.TOP, 
 * or SWT.BOTTOM if the point is on the control or SWT.DEFAULT if the point is not on the control. 
 * //from w w w .j a  va2s  .co  m
 * @param control control to perform hit detection on
 * @param toTest point to test, in display coordinates
 * @return
 */
public static int getRelativePosition(Control c, Point toTest) {
    Point p = c.toControl(toTest);
    Point e = c.getSize();

    if (p.x > e.x || p.y > e.y || p.x < 0 || p.y < 0) {
        return SWT.DEFAULT;
    }

    // first determine whether mouse position is in center of part
    int hmargin = Math.min(e.x / 3, MARGIN);
    int vmargin = Math.min(e.y / 3, MARGIN);

    Rectangle inner = new Rectangle(hmargin, vmargin, e.x - (hmargin * 2), e.y - (vmargin * 2));
    if (inner.contains(p)) {
        return SWT.CENTER;
    } else {
        return Geometry.getClosestSide(inner, p);
    }
}

From source file:org.eclipse.ui.internal.presentations.util.ReplaceDragHandler.java

License:Open Source License

public StackDropResult dragOver(Control currentControl, Point location, int dragStart) {

    // Determine which tab we're currently dragging over
    //Point localPos = tabFolder.getControl().toControl(location);

    AbstractTabItem tabUnderPointer = tabFolder.getItem(location);

    // This drop target only deals with tabs... if we're not dragging over
    // a tab, exit.
    if (tabUnderPointer == null) {
        Rectangle titleArea = tabFolder.getTabArea();

        // If we're dragging over the title area, treat this as a drop in the last
        // tab position.
        if (titleArea.contains(location) && tabFolder.getItemCount() > 0) {
            int dragOverIndex = tabFolder.getItemCount();
            AbstractTabItem lastTab = tabFolder.getItem(dragOverIndex - 1);

            // Can't drag to end unless you can see the end
            if (!lastTab.isShowing()) {
                return null;
            }/*from w w w.java  2  s  .com*/

            // If we are unable to compute the bounds for this tab, then ignore the drop
            Rectangle lastTabBounds = lastTab.getBounds();
            if (lastTabBounds.isEmpty()) {
                return null;
            }

            if (dragStart >= 0) {
                dragOverIndex--;

                return new StackDropResult(lastTabBounds, new Integer(dragOverIndex));
            }

            // Make the drag-over rectangle look like a tab at the end of the tab region.
            // We don't actually know how wide the tab will be when it's dropped, so just
            // make it 3 times wider than it is tall.
            Rectangle dropRectangle = titleArea;

            dropRectangle.x = lastTabBounds.x + lastTabBounds.width;
            dropRectangle.width = 3 * dropRectangle.height;
            return new StackDropResult(dropRectangle, new Integer(dragOverIndex));

        } else {
            // If the closest side is the side with the tabs, consider this a stack operation.
            // Otherwise, let the drop fall through to whatever the default behavior is
            Rectangle displayBounds = DragUtil.getDisplayBounds(tabFolder.getControl());
            int closestSide = Geometry.getClosestSide(displayBounds, location);
            if (closestSide == tabFolder.getTabPosition()) {
                return new StackDropResult(displayBounds, null);
            }

            return null;
        }
    }

    if (!tabUnderPointer.isShowing()) {
        return null;
    }

    Rectangle tabBounds = tabUnderPointer.getBounds();

    if (tabBounds.isEmpty()) {
        return null;
    }

    return new StackDropResult(tabBounds, new DragCookie(tabFolder.indexOf(tabUnderPointer)));
}