Search in sources :

Example 1 with FocusRequestor

use of com.intellij.openapi.wm.FocusRequestor in project intellij-community by JetBrains.

the class BaseProjectTreeBuilder method _select.

private void _select(final Object element, final VirtualFile file, final boolean requestFocus, final Condition<AbstractTreeNode> nonStopCondition, final ActionCallback result, @NotNull final ProgressIndicator indicator, @Nullable final Ref<Object> virtualSelectTarget, final FocusRequestor focusRequestor, final boolean isSecondAttempt) {
    final AbstractTreeNode alreadySelected = alreadySelectedNode(element);
    final Runnable onDone = () -> {
        if (requestFocus && virtualSelectTarget == null && getUi().isReady()) {
            focusRequestor.requestFocus(getTree(), true);
        }
        result.setDone();
    };
    final Condition<AbstractTreeNode> condition = abstractTreeNode -> !result.isProcessed() && nonStopCondition.value(abstractTreeNode);
    if (alreadySelected == null) {
        expandPathTo(file, (AbstractTreeNode) getTreeStructure().getRootElement(), element, condition, indicator, virtualSelectTarget).doWhenDone(new Consumer<AbstractTreeNode>() {

            @Override
            public void consume(AbstractTreeNode node) {
                if (virtualSelectTarget == null) {
                    select(node, onDone);
                } else {
                    onDone.run();
                }
            }
        }).doWhenRejected(() -> {
            if (isSecondAttempt) {
                result.setRejected();
            } else {
                _select(file, file, requestFocus, nonStopCondition, result, indicator, virtualSelectTarget, focusRequestor, true);
            }
        });
    } else if (virtualSelectTarget == null) {
        scrollTo(alreadySelected, onDone);
    } else {
        onDone.run();
    }
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) UiActivityMonitor(com.intellij.ide.UiActivityMonitor) Arrays(java.util.Arrays) StatusBarProgress(com.intellij.openapi.progress.util.StatusBarProgress) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TreeNode(javax.swing.tree.TreeNode) AbstractTreeBuilder(com.intellij.ide.util.treeView.AbstractTreeBuilder) FocusRequestor(com.intellij.openapi.wm.FocusRequestor) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Progressive(com.intellij.openapi.progress.Progressive) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) FavoritesTreeNodeDescriptor(com.intellij.ide.favoritesTreeView.FavoritesTreeNodeDescriptor) TreePath(javax.swing.tree.TreePath) UiActivity(com.intellij.ide.UiActivity) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) AbstractTreeStructure(com.intellij.ide.util.treeView.AbstractTreeStructure) Registry(com.intellij.openapi.util.registry.Registry) PsiDirectory(com.intellij.psi.PsiDirectory) ObjectUtils(com.intellij.util.ObjectUtils) AbstractTreeUpdater(com.intellij.ide.util.treeView.AbstractTreeUpdater) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) Consumer(com.intellij.util.Consumer) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) javax.swing(javax.swing) Consumer(com.intellij.util.Consumer) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 2 with FocusRequestor

use of com.intellij.openapi.wm.FocusRequestor in project intellij-community by JetBrains.

the class BalloonImpl method show.

private void show(PositionTracker<Balloon> tracker, AbstractPosition position) {
    assert !myDisposed : "Balloon is already disposed";
    if (isVisible())
        return;
    final Component comp = tracker.getComponent();
    if (!comp.isShowing())
        return;
    myTracker = tracker;
    myTracker.init(this);
    JRootPane root = ObjectUtils.notNull(UIUtil.getRootPane(comp));
    myVisible = true;
    myLayeredPane = root.getLayeredPane();
    myPosition = position;
    UIUtil.setFutureRootPane(myContent, root);
    myFocusManager = IdeFocusManager.findInstanceByComponent(myLayeredPane);
    final Ref<Component> originalFocusOwner = new Ref<>();
    final Ref<FocusRequestor> focusRequestor = new Ref<>();
    final Ref<ActionCallback> proxyFocusRequest = new Ref<>(ActionCallback.DONE);
    boolean mnemonicsFix = myDialogMode && SystemInfo.isMac && Registry.is("ide.mac.inplaceDialogMnemonicsFix");
    if (mnemonicsFix) {
        final IdeGlassPaneEx glassPane = (IdeGlassPaneEx) IdeGlassPaneUtil.find(myLayeredPane);
        assert glassPane != null;
        proxyFocusRequest.set(new ActionCallback());
        myFocusManager.doWhenFocusSettlesDown(new ExpirableRunnable() {

            @Override
            public boolean isExpired() {
                return isDisposed();
            }

            @Override
            public void run() {
                IdeEventQueue.getInstance().disableInputMethods(BalloonImpl.this);
                originalFocusOwner.set(myFocusManager.getFocusOwner());
                focusRequestor.set(myFocusManager.getFurtherRequestor());
            }
        });
    }
    if (myRequestFocus) {
        myFocusManager.doWhenFocusSettlesDown(new ExpirableRunnable() {

            @Override
            public boolean isExpired() {
                return isDisposed();
            }

            @Override
            public void run() {
                myOriginalFocusOwner = myFocusManager.getFocusOwner();
                // Set the accessible parent so that screen readers don't announce
                // a window context change -- the tooltip is "logically" hosted
                // inside the component (e.g. editor) it appears on top of.
                AccessibleContextUtil.setParent(myContent, myOriginalFocusOwner);
                // Set the focus to "myContent"
                myFocusManager.requestFocus(getContentToFocus(), true);
            }
        });
    }
    myLayeredPane.addComponentListener(myComponentListener);
    myTargetPoint = myPosition.getShiftedPoint(myTracker.recalculateLocation(this).getPoint(myLayeredPane), myCalloutShift);
    int positionChangeFix = 0;
    if (myShowPointer) {
        Rectangle rec = getRecForPosition(myPosition, true);
        if (!myPosition.isOkToHavePointer(myTargetPoint, rec, getPointerLength(myPosition), getPointerWidth(myPosition), getArc())) {
            rec = getRecForPosition(myPosition, false);
            Rectangle lp = new Rectangle(new Point(myContainerInsets.left, myContainerInsets.top), myLayeredPane.getSize());
            lp.width -= myContainerInsets.right;
            lp.height -= myContainerInsets.bottom;
            if (!lp.contains(rec)) {
                Rectangle2D currentSquare = lp.createIntersection(rec);
                double maxSquare = currentSquare.getWidth() * currentSquare.getHeight();
                AbstractPosition targetPosition = myPosition;
                for (AbstractPosition eachPosition : myPosition.getOtherPositions()) {
                    Rectangle2D eachIntersection = lp.createIntersection(getRecForPosition(eachPosition, false));
                    double eachSquare = eachIntersection.getWidth() * eachIntersection.getHeight();
                    if (maxSquare < eachSquare) {
                        maxSquare = eachSquare;
                        targetPosition = eachPosition;
                    }
                }
                myPosition = targetPosition;
                positionChangeFix = myPosition.getChangeShift(position, myPositionChangeXShift, myPositionChangeYShift);
            }
        }
    }
    if (myPosition != position) {
        myTargetPoint = myPosition.getShiftedPoint(myTracker.recalculateLocation(this).getPoint(myLayeredPane), myCalloutShift > 0 ? myCalloutShift + positionChangeFix : positionChangeFix);
    }
    createComponent();
    myComp.validate();
    Rectangle rec = myComp.getContentBounds();
    if (myShowPointer && !myPosition.isOkToHavePointer(myTargetPoint, rec, getPointerLength(myPosition), getPointerWidth(myPosition), getArc())) {
        myShowPointer = false;
        myComp.removeAll();
        myLayeredPane.remove(myComp);
        createComponent();
        if (!new Rectangle(myLayeredPane.getSize()).contains(new Rectangle(myComp.getSize()))) {
            // Balloon is bigger than window, don't show it at all.
            myComp.removeAll();
            myLayeredPane.remove(myComp);
            myLayeredPane = null;
            hide();
            return;
        }
    }
    for (JBPopupListener each : myListeners) {
        each.beforeShown(new LightweightWindowEvent(this));
    }
    runAnimation(true, myLayeredPane, null);
    myLayeredPane.revalidate();
    myLayeredPane.repaint();
    if (mnemonicsFix) {
        proxyFocusRequest.get().doWhenDone(() -> myFocusManager.requestFocus(originalFocusOwner.get(), true));
    }
    Toolkit.getDefaultToolkit().addAWTEventListener(myAwtActivityListener, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
    if (ApplicationManager.getApplication() != null) {
        ActionManager.getInstance().addAnActionListener(new AnActionListener.Adapter() {

            @Override
            public void beforeActionPerformed(AnAction action, DataContext dataContext, AnActionEvent event) {
                if (myHideOnAction) {
                    hide();
                }
            }
        }, this);
    }
    if (myHideOnLinkClick) {
        JEditorPane editorPane = UIUtil.uiTraverser(myContent).traverse().filter(JEditorPane.class).first();
        if (editorPane != null) {
            editorPane.addHyperlinkListener(new HyperlinkAdapter() {

                @Override
                protected void hyperlinkActivated(HyperlinkEvent e) {
                    hide();
                }
            });
        }
    }
}
Also used : HyperlinkEvent(javax.swing.event.HyperlinkEvent) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) IdeGlassPaneEx(com.intellij.openapi.wm.impl.IdeGlassPaneEx) JBPopupListener(com.intellij.openapi.ui.popup.JBPopupListener) DataContext(com.intellij.openapi.actionSystem.DataContext) AnActionListener(com.intellij.openapi.actionSystem.ex.AnActionListener) FocusRequestor(com.intellij.openapi.wm.FocusRequestor) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 3 with FocusRequestor

use of com.intellij.openapi.wm.FocusRequestor in project intellij-community by JetBrains.

the class BaseProjectTreeBuilder method revalidateElement.

@NotNull
@Override
public AsyncResult<Object> revalidateElement(Object element) {
    final AsyncResult<Object> result = new AsyncResult<>();
    if (element instanceof AbstractTreeNode) {
        AbstractTreeNode node = (AbstractTreeNode) element;
        final Object value = node.getValue();
        final ActionCallback callback = new ActionCallback();
        final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(ObjectUtils.tryCast(value, PsiElement.class));
        final FocusRequestor focusRequestor = IdeFocusManager.getInstance(myProject).getFurtherRequestor();
        batch(new Progressive() {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                final Ref<Object> target = new Ref<>();
                _select(value, virtualFile, false, Conditions.<AbstractTreeNode>alwaysTrue(), callback, indicator, target, focusRequestor, false);
                callback.doWhenDone(() -> result.setDone(target.get())).doWhenRejected(() -> result.setRejected());
            }
        });
    } else {
        result.setRejected();
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FocusRequestor(com.intellij.openapi.wm.FocusRequestor) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Progressive(com.intellij.openapi.progress.Progressive) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with FocusRequestor

use of com.intellij.openapi.wm.FocusRequestor in project intellij-community by JetBrains.

the class BaseProjectTreeBuilder method _select.

@NotNull
private ActionCallback _select(final Object element, final VirtualFile file, final boolean requestFocus, final Condition<AbstractTreeNode> nonStopCondition) {
    AbstractTreeUpdater updater = getUpdater();
    if (updater == null)
        return ActionCallback.REJECTED;
    final ActionCallback result = new ActionCallback();
    final FocusRequestor requestor = IdeFocusManager.getInstance(myProject).getFurtherRequestor();
    UiActivityMonitor.getInstance().addActivity(myProject, new UiActivity.AsyncBgOperation("projectViewSelect"), updater.getModalityState());
    batch(indicator -> {
        _select(element, file, requestFocus, nonStopCondition, result, indicator, null, requestor, false);
        UiActivityMonitor.getInstance().removeActivity(myProject, new UiActivity.AsyncBgOperation("projectViewSelect"));
    });
    return result;
}
Also used : FocusRequestor(com.intellij.openapi.wm.FocusRequestor) AbstractTreeUpdater(com.intellij.ide.util.treeView.AbstractTreeUpdater) UiActivity(com.intellij.ide.UiActivity) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FocusRequestor (com.intellij.openapi.wm.FocusRequestor)4 UiActivity (com.intellij.ide.UiActivity)2 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 AbstractTreeUpdater (com.intellij.ide.util.treeView.AbstractTreeUpdater)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Progressive (com.intellij.openapi.progress.Progressive)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiElement (com.intellij.psi.PsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 UiActivityMonitor (com.intellij.ide.UiActivityMonitor)1 FavoritesTreeNodeDescriptor (com.intellij.ide.favoritesTreeView.FavoritesTreeNodeDescriptor)1 AbstractTreeBuilder (com.intellij.ide.util.treeView.AbstractTreeBuilder)1 AbstractTreeStructure (com.intellij.ide.util.treeView.AbstractTreeStructure)1 NodeDescriptor (com.intellij.ide.util.treeView.NodeDescriptor)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 AnActionListener (com.intellij.openapi.actionSystem.ex.AnActionListener)1 StatusBarProgress (com.intellij.openapi.progress.util.StatusBarProgress)1 Project (com.intellij.openapi.project.Project)1