Example usage for com.intellij.openapi.ui.popup JBPopupFactory isBestPopupLocationVisible

List of usage examples for com.intellij.openapi.ui.popup JBPopupFactory isBestPopupLocationVisible

Introduction

In this page you can find the example usage for com.intellij.openapi.ui.popup JBPopupFactory isBestPopupLocationVisible.

Prototype

public abstract boolean isBestPopupLocationVisible(@NotNull Editor editor);

Source Link

Usage

From source file:com.intellij.refactoring.rename.inplace.InplaceRefactoring.java

License:Apache License

protected void showBalloon() {
    final JComponent component = getComponent();
    if (component == null)
        return;/*from w  w  w. j av a2 s . c  o  m*/
    if (ApplicationManager.getApplication().isHeadlessEnvironment())
        return;
    final BalloonBuilder balloonBuilder = JBPopupFactory.getInstance()
            .createDialogBalloonBuilder(component, null).setSmallVariant(true);
    myBalloon = balloonBuilder.createBalloon();
    final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(myEditor);
    Disposer.register(myProject, myBalloon);
    Disposer.register(myBalloon, new Disposable() {
        @Override
        public void dispose() {
            releaseIfNotRestart();
            topLevelEditor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, null);
        }
    });
    topLevelEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    final JBPopupFactory popupFactory = JBPopupFactory.getInstance();
    myBalloon.show(new PositionTracker<Balloon>(topLevelEditor.getContentComponent()) {
        @Override
        public RelativePoint recalculateLocation(Balloon object) {
            if (myTarget != null && !popupFactory.isBestPopupLocationVisible(topLevelEditor)) {
                return myTarget;
            }
            if (myCaretRangeMarker != null && myCaretRangeMarker.isValid()) {
                topLevelEditor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION,
                        topLevelEditor.offsetToVisualPosition(myCaretRangeMarker.getStartOffset()));
            }
            final RelativePoint target = popupFactory.guessBestPopupLocation(topLevelEditor);
            final Point screenPoint = target.getScreenPoint();
            int y = screenPoint.y;
            if (target.getPoint().getY() > topLevelEditor.getLineHeight()
                    + myBalloon.getPreferredSize().getHeight()) {
                y -= topLevelEditor.getLineHeight();
            }
            myTarget = new RelativePoint(new Point(screenPoint.x, y));
            return myTarget;
        }
    }, Balloon.Position.above);
}