List of usage examples for org.eclipse.jface.util Geometry createRectangle
public static Rectangle createRectangle(Point position, Point size)
From source file:au.gov.ga.earthsci.jface.extras.information.AbstractInformationControlManager.java
License:Open Source License
/** * Gets the closest monitor given an anchor and the subject area. * * @param area the subject area//ww w .j av a2s . c om * @param anchor the anchor * @return the monitor closest to the edge of <code>area</code> defined by * <code>anchor</code> * @since 3.3 */ private Monitor getClosestMonitor(Rectangle area, Anchor anchor) { Point center; if (ANCHOR_GLOBAL == anchor) center = Geometry.centerPoint(area); else center = Geometry.centerPoint(Geometry.getExtrudedEdge(area, 0, anchor.getSWTFlag())); return getClosestMonitor(fSubjectControl.getDisplay(), Geometry.createRectangle(center, new Point(0, 0))); }
From source file:au.gov.ga.earthsci.jface.extras.information.AbstractInformationControlManager.java
License:Open Source License
/** * Opens the information control with the given information and the specified * subject area. It also activates the information control closer. * * @param subjectArea the information area * @param information the information// w w w .j a va 2s. c om */ private void internalShowInformationControl(Rectangle subjectArea, Object information) { if (this instanceof InformationControlReplacer) { ((InformationControlReplacer) this).showInformationControl(subjectArea, information); return; } IInformationControl informationControl = getInformationControl(); if (informationControl != null) { Point sizeConstraints = computeSizeConstraints(fSubjectControl, fSubjectArea, informationControl); if (informationControl instanceof IInformationControlExtension3) { IInformationControlExtension3 iControl3 = (IInformationControlExtension3) informationControl; Rectangle trim = iControl3.computeTrim(); sizeConstraints.x += trim.width; sizeConstraints.y += trim.height; } informationControl.setSizeConstraints(sizeConstraints.x, sizeConstraints.y); if (informationControl instanceof IInformationControlExtension2) ((IInformationControlExtension2) informationControl).setInput(information); else informationControl.setInformation(information.toString()); if (informationControl instanceof IInformationControlExtension) { IInformationControlExtension extension = (IInformationControlExtension) informationControl; if (!extension.hasContents()) return; } Point size = null; Point location = null; Rectangle bounds = restoreInformationControlBounds(); if (bounds != null) { if (bounds.x > -1 && bounds.y > -1) location = Geometry.getLocation(bounds); if (bounds.width > -1 && bounds.height > -1) size = Geometry.getSize(bounds); } if (size == null) size = informationControl.computeSizeHint(); if (fEnforceAsMinimalSize) size = Geometry.max(size, sizeConstraints); if (fEnforceAsMaximalSize) size = Geometry.min(size, sizeConstraints); if (location == null) location = computeInformationControlLocation(subjectArea, size); Rectangle controlBounds = Geometry.createRectangle(location, size); cropToClosestMonitor(controlBounds); location = Geometry.getLocation(controlBounds); size = Geometry.getSize(controlBounds); informationControl.setLocation(location); informationControl.setSize(size.x, size.y); showInformationControl(subjectArea); } }
From source file:com.cisco.yangide.ext.refactoring.ui.RenameInformationPopup.java
License:Open Source License
/** * @param snapPosition one of the SNAP_POSITION_* constants * @return the location in display coordinates or <code>null</code> iff not visible *///from www . j a v a 2s. c o m private Point computePopupLocation(int snapPosition) { if (fPopup == null || fPopup.isDisposed()) { return null; } switch (snapPosition) { case SNAP_POSITION_LOWER_RIGHT: { StyledText eWidget = editor.getViewer().getTextWidget(); Rectangle eBounds = eWidget.getClientArea(); Point eLowerRight = eWidget.toDisplay(eBounds.x + eBounds.width, eBounds.y + eBounds.height); Point pSize = getExtent(); return new Point(eLowerRight.x - pSize.x - 5, eLowerRight.y - pSize.y - 5); } case SNAP_POSITION_UNDER_RIGHT_FIELD: case SNAP_POSITION_OVER_RIGHT_FIELD: { LinkedPosition position = renameLinkedMode.getCurrentLinkedPosition(); if (position == null) { return null; } ISourceViewer viewer = editor.getViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset + position.length); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); if (snapPosition == SNAP_POSITION_OVER_RIGHT_FIELD) { pos.y -= pSize.y + GAP; } else { pos.y += textWidget.getLineHeight(widgetOffset) + GAP; } pos.x += GAP; Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); } case SNAP_POSITION_UNDER_LEFT_FIELD: case SNAP_POSITION_OVER_LEFT_FIELD: default: // same as SNAP_POSITION_UNDER_LEFT_FIELD { LinkedPosition position = renameLinkedMode.getCurrentLinkedPosition(); if (position == null) { return null; } ISourceViewer viewer = editor.getViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset/* + position.length */); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); pSize.y += HAH + 1; pos.x -= HAO; if (snapPosition == SNAP_POSITION_OVER_LEFT_FIELD) { pos.y -= pSize.y; } else { pos.y += textWidget.getLineHeight(widgetOffset); } Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); } } }
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/*from w ww .j a va2s . c o m*/ 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 w w w . j a v a 2 s . 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:com.google.dart.tools.ui.internal.refactoring.reorg.RenameInformationPopup.java
License:Open Source License
/** * @param snapPosition one of the SNAP_POSITION_* constants * @return the location in display coordinates or <code>null</code> iff not visible *//*from www .ja v a2 s . c o m*/ private Point computePopupLocation(int snapPosition) { if (fPopup == null || fPopup.isDisposed()) { return null; } switch (snapPosition) { case SNAP_POSITION_LOWER_RIGHT: { StyledText eWidget = fEditor.getViewer().getTextWidget(); Rectangle eBounds = eWidget.getClientArea(); Point eLowerRight = eWidget.toDisplay(eBounds.x + eBounds.width, eBounds.y + eBounds.height); Point pSize = getExtent(); return new Point(eLowerRight.x - pSize.x - 5, eLowerRight.y - pSize.y - 5); } case SNAP_POSITION_UNDER_RIGHT_FIELD: case SNAP_POSITION_OVER_RIGHT_FIELD: { LinkedPosition position = fRenameLinkedMode.getCurrentLinkedPosition(); if (position == null) { return null; } ISourceViewer viewer = fEditor.getViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset + position.length); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); if (snapPosition == SNAP_POSITION_OVER_RIGHT_FIELD) { pos.y -= pSize.y + GAP; } else { pos.y += textWidget.getLineHeight(widgetOffset) + GAP; } pos.x += GAP; Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); } case SNAP_POSITION_UNDER_LEFT_FIELD: case SNAP_POSITION_OVER_LEFT_FIELD: default: // same as SNAP_POSITION_UNDER_LEFT_FIELD { LinkedPosition position = fRenameLinkedMode.getCurrentLinkedPosition(); if (position == null) { return null; } ISourceViewer viewer = fEditor.getViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset/* + position.length */); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); pSize.y += HAH + 1; pos.x -= HAO; if (snapPosition == SNAP_POSITION_OVER_LEFT_FIELD) { pos.y -= pSize.y; } else { pos.y += textWidget.getLineHeight(widgetOffset); } Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); } } }
From source file:com.redhat.ceylon.eclipse.code.refactor.RefactorInformationPopup.java
License:Open Source License
/** * @param snapPosition one of the SNAP_POSITION_* constants * @return the location in display coordinates or <code>null</code> iff not visible *//*ww w. j a v a 2 s .c o m*/ private Point computePopupLocation(int snapPosition) { if (fPopup == null || fPopup.isDisposed()) return null; switch (snapPosition) { case SNAP_POSITION_LOWER_RIGHT: { StyledText eWidget = fEditor.getCeylonSourceViewer().getTextWidget(); Rectangle eBounds = eWidget.getClientArea(); Point eLowerRight = eWidget.toDisplay(eBounds.x + eBounds.width, eBounds.y + eBounds.height); Point pSize = getExtent(); return new Point(eLowerRight.x - pSize.x - 5, eLowerRight.y - pSize.y - 5); } case SNAP_POSITION_UNDER_RIGHT_FIELD: case SNAP_POSITION_OVER_RIGHT_FIELD: { LinkedPosition position = fLinkedMode.getCurrentLinkedPosition(); if (position == null) return null; ISourceViewer viewer = fEditor.getCeylonSourceViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset + position.length); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); if (snapPosition == SNAP_POSITION_OVER_RIGHT_FIELD) { pos.y -= pSize.y + GAP; } else { pos.y += textWidget.getLineHeight(widgetOffset) + GAP; } pos.x += GAP; Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); } case SNAP_POSITION_UNDER_LEFT_FIELD: case SNAP_POSITION_OVER_LEFT_FIELD: default: // same as SNAP_POSITION_UNDER_LEFT_FIELD { LinkedPosition position = fLinkedMode.getCurrentLinkedPosition(); if (position == null) return null; ISourceViewer viewer = fEditor.getCeylonSourceViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset/* + position.length*/); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); pSize.y += HAH + 1; pos.x -= HAO; if (snapPosition == SNAP_POSITION_OVER_LEFT_FIELD) { pos.y -= pSize.y; } else { pos.y += textWidget.getLineHeight(widgetOffset); } Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); } } }
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 ww w . ja v a 2 s . c om*/ 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:ext.org.eclipse.jdt.internal.ui.refactoring.reorg.RenameInformationPopup.java
License:Open Source License
/** * @param snapPosition one of the SNAP_POSITION_* constants * @return the location in display coordinates or <code>null</code> iff not visible *//*from www . j av a2 s . c o m*/ private Point computePopupLocation(int snapPosition) { if (fPopup == null || fPopup.isDisposed()) return null; switch (snapPosition) { case SNAP_POSITION_LOWER_RIGHT: { StyledText eWidget = fEditor.getViewer().getTextWidget(); Rectangle eBounds = eWidget.getClientArea(); Point eLowerRight = eWidget.toDisplay(eBounds.x + eBounds.width, eBounds.y + eBounds.height); Point pSize = getExtent(); return new Point(eLowerRight.x - pSize.x - 5, eLowerRight.y - pSize.y - 5); } case SNAP_POSITION_UNDER_RIGHT_FIELD: case SNAP_POSITION_OVER_RIGHT_FIELD: { LinkedPosition position = fRenameLinkedMode.getCurrentLinkedPosition(); if (position == null) return null; ISourceViewer viewer = fEditor.getViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset + position.length); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); if (snapPosition == SNAP_POSITION_OVER_RIGHT_FIELD) { pos.y -= pSize.y + GAP; } else { pos.y += textWidget.getLineHeight(widgetOffset) + GAP; } pos.x += GAP; Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); } case SNAP_POSITION_UNDER_LEFT_FIELD: case SNAP_POSITION_OVER_LEFT_FIELD: default: // same as SNAP_POSITION_UNDER_LEFT_FIELD { LinkedPosition position = fRenameLinkedMode.getCurrentLinkedPosition(); if (position == null) return null; ISourceViewer viewer = fEditor.getViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset/* + position.length*/); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); pSize.y += HAH + 1; pos.x -= HAO; if (snapPosition == SNAP_POSITION_OVER_LEFT_FIELD) { pos.y -= pSize.y; } else { pos.y += textWidget.getLineHeight(widgetOffset); } Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); } } }
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 www.j a va 2s . c om*/ 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(); } }); }