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

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

Introduction

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

Prototype

public static int distanceSquared(Point p1, Point p2) 

Source Link

Document

Returns the square of the distance between two points.

Usage

From source file:au.gov.ga.earthsci.jface.extras.information.AbstractInformationControlManager.java

License:Open Source License

/**
 * Copied from org.eclipse.jface.window.Window. Returns the monitor whose client area contains
 * the given point. If no monitor contains the point, returns the monitor that is closest to the
 * point. If this is ever made public, it should be moved into a separate utility class.
 *
 * @param display the display to search for monitors
 * @param rectangle the rectangle to find the closest monitor for (display coordinates)
 * @return the monitor closest to the given point
 * @since 3.3/*w ww  .j  av  a 2  s  .  co m*/
 */
private Monitor getClosestMonitor(Display display, Rectangle rectangle) {
    int closest = Integer.MAX_VALUE;

    Point toFind = Geometry.centerPoint(rectangle);
    Monitor[] monitors = display.getMonitors();
    Monitor result = monitors[0];

    for (int idx = 0; idx < monitors.length; idx++) {
        Monitor current = monitors[idx];

        Rectangle clientArea = current.getClientArea();

        if (clientArea.contains(toFind)) {
            return current;
        }

        int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), toFind);
        if (distance < closest) {
            closest = distance;
            result = current;
        }
    }

    return result;
}

From source file:com.cisco.yangide.ext.refactoring.ui.RenameInformationPopup.java

License:Open Source License

private void addMoveSupport(final Shell popupShell, final Control movedControl) {
    movedControl.addMouseListener(new MouseAdapter() {

        @Override/*  w w  w  .  j  a  va 2 s .c om*/
        public void mouseDown(final MouseEvent downEvent) {
            if (downEvent.button != 1) {
                return;
            }

            final Point POPUP_SOURCE = popupShell.getLocation();
            final StyledText textWidget = editor.getViewer().getTextWidget();
            Point pSize = getExtent();
            int originalSnapPosition = fSnapPosition;

            /*
             * Feature in Tracker: it is not possible to directly control the feedback, see
             * https://bugs.eclipse.org/bugs/show_bug.cgi?id=121300 and
             * https://bugs.eclipse.org/bugs/show_bug.cgi?id=121298#c1 .
             * 
             * Workaround is to have an offscreen rectangle for tracking mouse movement and a
             * manually updated rectangle for the actual drop target.
             */
            final Tracker tracker = new Tracker(textWidget, SWT.NONE);

            final Point[] LOCATIONS = {
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_UNDER_RIGHT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_OVER_RIGHT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_UNDER_LEFT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_OVER_LEFT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_LOWER_RIGHT)) };

            final Rectangle[] DROP_TARGETS = { Geometry.createRectangle(LOCATIONS[0], pSize),
                    Geometry.createRectangle(LOCATIONS[1], pSize),
                    new Rectangle(LOCATIONS[2].x, LOCATIONS[2].y + HAH, pSize.x, pSize.y),
                    Geometry.createRectangle(LOCATIONS[3], pSize),
                    Geometry.createRectangle(LOCATIONS[4], pSize) };
            final Rectangle MOUSE_MOVE_SOURCE = new Rectangle(1000000, 0, 0, 0);
            tracker.setRectangles(new Rectangle[] { MOUSE_MOVE_SOURCE, DROP_TARGETS[fSnapPosition] });
            tracker.setStippled(true);

            ControlListener moveListener = new ControlAdapter() {
                /*
                 * @see
                 * org.eclipse.swt.events.ControlAdapter#controlMoved(org.eclipse.swt.events
                 * .ControlEvent)
                 */
                @Override
                public void controlMoved(ControlEvent moveEvent) {
                    Rectangle[] currentRects = tracker.getRectangles();
                    final Rectangle mouseMoveCurrent = currentRects[0];
                    Point popupLoc = new Point(POPUP_SOURCE.x + mouseMoveCurrent.x - MOUSE_MOVE_SOURCE.x,
                            POPUP_SOURCE.y + mouseMoveCurrent.y - MOUSE_MOVE_SOURCE.y);

                    popupShell.setLocation(popupLoc);

                    Point ePopupLoc = textWidget.toControl(popupLoc);
                    int minDist = Integer.MAX_VALUE;
                    for (int snapPos = 0; snapPos < DROP_TARGETS.length; snapPos++) {
                        int dist = Geometry.distanceSquared(ePopupLoc, LOCATIONS[snapPos]);
                        if (dist < minDist) {
                            minDist = dist;
                            fSnapPosition = snapPos;
                            fSnapPositionChanged = true;
                            currentRects[1] = DROP_TARGETS[snapPos];
                        }
                    }
                    tracker.setRectangles(currentRects);
                }
            };
            tracker.addControlListener(moveListener);
            boolean committed = tracker.open();
            tracker.close();
            tracker.dispose();
            if (committed) {
                getDialogSettings().put(SNAP_POSITION_KEY, fSnapPosition);
            } else {
                fSnapPosition = originalSnapPosition;
                fSnapPositionChanged = true;
            }
            updatePopupLocation(true);
            activateEditor();
        }
    });
}

From source file:com.google.dart.tools.ui.internal.refactoring.reorg.RenameInformationPopup.java

License:Open Source License

private void addMoveSupport(final Shell popupShell, final Control movedControl) {
    movedControl.addMouseListener(new MouseAdapter() {

        @Override//from   ww  w  .j a  va 2s .  com
        public void mouseDown(final MouseEvent downEvent) {
            if (downEvent.button != 1) {
                return;
            }

            final Point POPUP_SOURCE = popupShell.getLocation();
            final StyledText textWidget = fEditor.getViewer().getTextWidget();
            Point pSize = getExtent();
            int originalSnapPosition = fSnapPosition;

            /*
             * Feature in Tracker: it is not possible to directly control the feedback, see
             * https://bugs.eclipse.org/bugs/show_bug.cgi?id=121300 and
             * https://bugs.eclipse.org/bugs/show_bug.cgi?id=121298#c1 .
             * 
             * Workaround is to have an offscreen rectangle for tracking mouse movement and a manually
             * updated rectangle for the actual drop target.
             */
            final Tracker tracker = new Tracker(textWidget, SWT.NONE);

            final Point[] LOCATIONS = {
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_UNDER_RIGHT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_OVER_RIGHT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_UNDER_LEFT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_OVER_LEFT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_LOWER_RIGHT)) };

            final Rectangle[] DROP_TARGETS = { Geometry.createRectangle(LOCATIONS[0], pSize),
                    Geometry.createRectangle(LOCATIONS[1], pSize),
                    new Rectangle(LOCATIONS[2].x, LOCATIONS[2].y + HAH, pSize.x, pSize.y),
                    Geometry.createRectangle(LOCATIONS[3], pSize),
                    Geometry.createRectangle(LOCATIONS[4], pSize) };
            final Rectangle MOUSE_MOVE_SOURCE = new Rectangle(1000000, 0, 0, 0);
            tracker.setRectangles(new Rectangle[] { MOUSE_MOVE_SOURCE, DROP_TARGETS[fSnapPosition] });
            tracker.setStippled(true);

            ControlListener moveListener = new ControlAdapter() {
                /*
                 * @see
                 * org.eclipse.swt.events.ControlAdapter#controlMoved(org.eclipse.swt.events.ControlEvent)
                 */
                @Override
                public void controlMoved(ControlEvent moveEvent) {
                    Rectangle[] currentRects = tracker.getRectangles();
                    final Rectangle mouseMoveCurrent = currentRects[0];
                    Point popupLoc = new Point(POPUP_SOURCE.x + mouseMoveCurrent.x - MOUSE_MOVE_SOURCE.x,
                            POPUP_SOURCE.y + mouseMoveCurrent.y - MOUSE_MOVE_SOURCE.y);

                    popupShell.setLocation(popupLoc);

                    Point ePopupLoc = textWidget.toControl(popupLoc);
                    int minDist = Integer.MAX_VALUE;
                    for (int snapPos = 0; snapPos < DROP_TARGETS.length; snapPos++) {
                        int dist = Geometry.distanceSquared(ePopupLoc, LOCATIONS[snapPos]);
                        if (dist < minDist) {
                            minDist = dist;
                            fSnapPosition = snapPos;
                            fSnapPositionChanged = true;
                            currentRects[1] = DROP_TARGETS[snapPos];
                        }
                    }
                    tracker.setRectangles(currentRects);
                }
            };
            tracker.addControlListener(moveListener);
            boolean committed = tracker.open();
            tracker.close();
            tracker.dispose();
            if (committed) {
                getDialogSettings().put(SNAP_POSITION_KEY, fSnapPosition);
            } else {
                fSnapPosition = originalSnapPosition;
                fSnapPositionChanged = true;
            }
            updatePopupLocation(true);
            activateEditor();
        }
    });
}

From source file:com.google.gwt.eclipse.oophm.breadcrumbs.BreadcrumbItemDropDown.java

License:Open Source License

/**
 * Returns the monitor whose client area contains the given point. If no
 * monitor contains the point, returns the monitor that is closest to the
 * point./*from ww  w.  j a v a2s .c  om*/
 * <p>
 * Copied from
 * <code>org.eclipse.jface.window.Window.getClosestMonitor(Display, Point)</code>
 * </p>
 * 
 * @param display the display showing the monitors
 * @param point point to find (display coordinates)
 * @return the monitor closest to the given point
 */
private static Monitor getClosestMonitor(Display display, Point point) {
    int closest = Integer.MAX_VALUE;

    Monitor[] monitors = display.getMonitors();
    Monitor result = monitors[0];

    for (int i = 0; i < monitors.length; i++) {
        Monitor current = monitors[i];

        Rectangle clientArea = current.getClientArea();

        if (clientArea.contains(point))
            return current;

        int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), point);
        if (distance < closest) {
            closest = distance;
            result = current;
        }
    }

    return result;
}

From source file:com.jointlogic.breadcrumbs.sampleapp.api.BreadcrumbItemDropDown.java

License:Open Source License

/**
 * Returns the monitor whose client area contains the given point. If no
 * monitor contains the point, returns the monitor that is closest to the
 * point.//from   w  w w. j  a v a2 s  .  com
 * <p>
 * Copied from
 * <code>org.eclipse.jface.window.Window.getClosestMonitor(Display, Point)</code>
 * </p>
 * 
 * @param display
 *            the display showing the monitors
 * @param point
 *            point to find (display coordinates)
 * @return the monitor closest to the given point
 */
private static Monitor getClosestMonitor(final Display display, final Point point) {
    int closest = Integer.MAX_VALUE;

    final Monitor[] monitors = display.getMonitors();
    Monitor result = monitors[0];

    for (int i = 0; i < monitors.length; i++) {
        final Monitor current = monitors[i];

        final Rectangle clientArea = current.getClientArea();

        if (clientArea.contains(point)) {
            return current;
        }

        final int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), point);
        if (distance < closest) {
            closest = distance;
            result = current;
        }
    }

    return result;
}

From source file:com.redhat.ceylon.eclipse.code.refactor.RefactorInformationPopup.java

License:Open Source License

private void addMoveSupport(final Shell popupShell, final Control movedControl) {
    movedControl.addMouseListener(new MouseAdapter() {

        @Override//from   w w  w.  j  a v a2  s  .co  m
        public void mouseDown(final MouseEvent downEvent) {
            if (downEvent.button != 1) {
                return;
            }

            final Point POPUP_SOURCE = popupShell.getLocation();
            final StyledText textWidget = fEditor.getCeylonSourceViewer().getTextWidget();
            Point pSize = getExtent();
            int originalSnapPosition = fSnapPosition;

            /*
             * Feature in Tracker: it is not possible to directly control the feedback,
             * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=121300
             * and https://bugs.eclipse.org/bugs/show_bug.cgi?id=121298#c1 .
             *
             * Workaround is to have an offscreen rectangle for tracking mouse movement
             * and a manually updated rectangle for the actual drop target.
             */
            final Tracker tracker = new Tracker(textWidget, SWT.NONE);

            final Point[] LOCATIONS = {
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_UNDER_RIGHT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_OVER_RIGHT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_UNDER_LEFT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_OVER_LEFT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_LOWER_RIGHT)) };

            final Rectangle[] DROP_TARGETS = { Geometry.createRectangle(LOCATIONS[0], pSize),
                    Geometry.createRectangle(LOCATIONS[1], pSize),
                    new Rectangle(LOCATIONS[2].x, LOCATIONS[2].y + HAH, pSize.x, pSize.y),
                    Geometry.createRectangle(LOCATIONS[3], pSize),
                    Geometry.createRectangle(LOCATIONS[4], pSize) };
            final Rectangle MOUSE_MOVE_SOURCE = new Rectangle(1000000, 0, 0, 0);
            tracker.setRectangles(new Rectangle[] { MOUSE_MOVE_SOURCE, DROP_TARGETS[fSnapPosition] });
            tracker.setStippled(true);

            ControlListener moveListener = new ControlAdapter() {
                /*
                 * @see org.eclipse.swt.events.ControlAdapter#controlMoved(org.eclipse.swt.events.ControlEvent)
                 */
                @Override
                public void controlMoved(ControlEvent moveEvent) {
                    Rectangle[] currentRects = tracker.getRectangles();
                    final Rectangle mouseMoveCurrent = currentRects[0];
                    Point popupLoc = new Point(POPUP_SOURCE.x + mouseMoveCurrent.x - MOUSE_MOVE_SOURCE.x,
                            POPUP_SOURCE.y + mouseMoveCurrent.y - MOUSE_MOVE_SOURCE.y);

                    popupShell.setLocation(popupLoc);

                    Point ePopupLoc = textWidget.toControl(popupLoc);
                    int minDist = Integer.MAX_VALUE;
                    for (int snapPos = 0; snapPos < DROP_TARGETS.length; snapPos++) {
                        int dist = Geometry.distanceSquared(ePopupLoc, LOCATIONS[snapPos]);
                        if (dist < minDist) {
                            minDist = dist;
                            fSnapPosition = snapPos;
                            fSnapPositionChanged = true;
                            currentRects[1] = DROP_TARGETS[snapPos];
                        }
                    }
                    tracker.setRectangles(currentRects);
                }
            };
            tracker.addControlListener(moveListener);
            boolean committed = tracker.open();
            tracker.close();
            tracker.dispose();
            if (committed) {
                getDialogSettings().put(SNAP_POSITION_KEY, fSnapPosition);
            } else {
                fSnapPosition = originalSnapPosition;
                fSnapPositionChanged = true;
            }
            updatePopupLocation(true);
            activateEditor();
        }
    });
}

From source file:de.walware.ecommons.ui.util.DialogUtil.java

License:Open Source License

public static Monitor getClosestMonitor(final Display toSearch, final Rectangle rectangle) {
    int closest = Integer.MAX_VALUE;

    final Point toFind = Geometry.centerPoint(rectangle);
    final Monitor[] monitors = toSearch.getMonitors();
    Monitor result = monitors[0];

    for (int idx = 0; idx < monitors.length; idx++) {
        final Monitor current = monitors[idx];

        final Rectangle clientArea = current.getClientArea();

        if (clientArea.contains(toFind)) {
            return current;
        }// w  w w. j  a v  a2s.  c om

        final int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), toFind);
        if (distance < closest) {
            closest = distance;
            result = current;
        }
    }

    return result;
}

From source file:eu.numberfour.n4js.ui.utils.UIUtils.java

License:Open Source License

/**
 * Returns the monitor whose client area contains the given point. If no monitor contains the point, returns the
 * monitor that is closest to the point.
 *
 * @param toSearch/*from  w  w  w . jav  a2  s .c o  m*/
 *            point to find (display coordinates).
 * @param toFind
 *            point to find (display coordinates).
 * @return the monitor closest to the given point.
 */
private static Monitor getClosestMonitor(final Display toSearch, final Point toFind) {
    int closest = Integer.MAX_VALUE;

    final Monitor[] monitors = toSearch.getMonitors();
    Monitor result = monitors[0];

    for (int index = 0; index < monitors.length; index++) {
        final Monitor current = monitors[index];

        final Rectangle clientArea = current.getClientArea();

        if (clientArea.contains(toFind)) {
            return current;
        }

        final int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), toFind);
        if (distance < closest) {
            closest = distance;
            result = current;
        }
    }

    return result;
}

From source file:ext.org.eclipse.jdt.internal.ui.refactoring.reorg.RenameInformationPopup.java

License:Open Source License

private void addMoveSupport(final Shell popupShell, final Control movedControl) {
    movedControl.addMouseListener(new MouseAdapter() {

        @Override/*from  ww  w. ja v  a 2s. c  o m*/
        public void mouseDown(final MouseEvent downEvent) {
            if (downEvent.button != 1) {
                return;
            }

            final Point POPUP_SOURCE = popupShell.getLocation();
            final StyledText textWidget = fEditor.getViewer().getTextWidget();
            Point pSize = getExtent();
            int originalSnapPosition = fSnapPosition;

            /*
             * Feature in Tracker: it is not possible to directly control the feedback,
             * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=121300
             * and https://bugs.eclipse.org/bugs/show_bug.cgi?id=121298#c1 .
             *
             * Workaround is to have an offscreen rectangle for tracking mouse movement
             * and a manually updated rectangle for the actual drop target.
             */
            final Tracker tracker = new Tracker(textWidget, SWT.NONE);

            final Point[] LOCATIONS = {
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_UNDER_RIGHT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_OVER_RIGHT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_UNDER_LEFT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_OVER_LEFT_FIELD)),
                    textWidget.toControl(computePopupLocation(SNAP_POSITION_LOWER_RIGHT)) };

            final Rectangle[] DROP_TARGETS = { Geometry.createRectangle(LOCATIONS[0], pSize),
                    Geometry.createRectangle(LOCATIONS[1], pSize),
                    new Rectangle(LOCATIONS[2].x, LOCATIONS[2].y + HAH, pSize.x, pSize.y),
                    Geometry.createRectangle(LOCATIONS[3], pSize),
                    Geometry.createRectangle(LOCATIONS[4], pSize) };
            final Rectangle MOUSE_MOVE_SOURCE = new Rectangle(1000000, 0, 0, 0);
            tracker.setRectangles(new Rectangle[] { MOUSE_MOVE_SOURCE, DROP_TARGETS[fSnapPosition] });
            tracker.setStippled(true);

            ControlListener moveListener = new ControlAdapter() {
                /*
                 * @see org.eclipse.swt.events.ControlAdapter#controlMoved(org.eclipse.swt.events.ControlEvent)
                 */
                @Override
                public void controlMoved(ControlEvent moveEvent) {
                    Rectangle[] currentRects = tracker.getRectangles();
                    final Rectangle mouseMoveCurrent = currentRects[0];
                    Point popupLoc = new Point(POPUP_SOURCE.x + mouseMoveCurrent.x - MOUSE_MOVE_SOURCE.x,
                            POPUP_SOURCE.y + mouseMoveCurrent.y - MOUSE_MOVE_SOURCE.y);

                    popupShell.setLocation(popupLoc);

                    Point ePopupLoc = textWidget.toControl(popupLoc);
                    int minDist = Integer.MAX_VALUE;
                    for (int snapPos = 0; snapPos < DROP_TARGETS.length; snapPos++) {
                        int dist = Geometry.distanceSquared(ePopupLoc, LOCATIONS[snapPos]);
                        if (dist < minDist) {
                            minDist = dist;
                            fSnapPosition = snapPos;
                            fSnapPositionChanged = true;
                            currentRects[1] = DROP_TARGETS[snapPos];
                        }
                    }
                    tracker.setRectangles(currentRects);
                }
            };
            tracker.addControlListener(moveListener);
            boolean committed = tracker.open();
            tracker.close();
            tracker.dispose();
            if (committed) {
                getDialogSettings().put(SNAP_POSITION_KEY, fSnapPosition);
            } else {
                fSnapPosition = originalSnapPosition;
                fSnapPositionChanged = true;
            }
            updatePopupLocation(true);
            activateEditor();
        }
    });
}

From source file:nu.bibi.breadcrumb.BreadcrumbItemArrow.java

License:Open Source License

/**
 * Returns the monitor whose client area contains the given point. If no
 * monitor contains the point, returns the monitor that is closest to the
 * point./*from  w w  w.  java  2s. c o  m*/
 * <p>
 * Copied from
 * <code>org.eclipse.jface.window.Window.getClosestMonitor(Display, Point)</code>
 * .
 * </p>
 * 
 * @param display
 *            the display showing the monitors.
 * @param point
 *            point to find (display coordinates).
 * @return the monitor closest to the given point.
 */
private static Monitor getClosestMonitor(final Display display, final Point point) {
    int closest = Integer.MAX_VALUE;

    final Monitor[] monitors = display.getMonitors();
    Monitor result = monitors[0];

    for (final Monitor current : monitors) {
        final Rectangle clientArea = current.getClientArea();

        if (clientArea.contains(point)) {
            return current;
        }

        final int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), point);
        if (distance < closest) {
            closest = distance;
            result = current;
        }
    }

    return result;
}