From b013e9ddcab3ca9f05eeddf277008dbeb0ad563c Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Thu, 12 Jul 2018 21:30:31 +0300 Subject: [PATCH] IDEA-183801: move focus traversal policy from toolwindow to decorator because its header may contain action buttons --- .../openapi/wm/impl/InternalDecorator.java | 24 ++++++- .../openapi/wm/impl/ToolWindowImpl.java | 62 ------------------- 2 files changed, 21 insertions(+), 65 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/InternalDecorator.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/InternalDecorator.java index 0c8ed5ac75ba..1fd048c781fd 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/InternalDecorator.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/InternalDecorator.java @@ -1,6 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.wm.impl; import com.intellij.icons.AllIcons; @@ -265,6 +263,7 @@ public final class InternalDecorator extends JPanel implements Queryable, DataPr enableEvents(AWTEvent.COMPONENT_EVENT_MASK); final JPanel contentPane = new JPanel(new BorderLayout()); + installFocusTraversalPolicy(contentPane, new LayoutFocusTraversalPolicy()); contentPane.add(myHeader, BorderLayout.NORTH); JPanel innerPanel = new JPanel(new BorderLayout()); @@ -903,4 +902,23 @@ public final class InternalDecorator extends JPanel implements Queryable, DataPr return name; } } + + /** + * Installs a focus traversal policy for the tool window. + * If the policy cannot handle a keystroke, it delegates the handling to + * the nearest ancestors focus traversal policy. For instance, + * this policy does not handle KeyEvent.VK_ESCAPE, so it can delegate the handling + * to a ThreeComponentSplitter instance. + */ + private static void installFocusTraversalPolicy(@NotNull Container container, @NotNull FocusTraversalPolicy policy) { + container.setFocusCycleRoot(true); + container.setFocusTraversalPolicyProvider(true); + container.setFocusTraversalPolicy(policy); + installDefaultFocusTraversalKeys(container, KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); + installDefaultFocusTraversalKeys(container, KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS); + } + + private static void installDefaultFocusTraversalKeys(@NotNull Container container, int id) { + container.setFocusTraversalKeys(id, KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(id)); + } } diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/ToolWindowImpl.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/ToolWindowImpl.java index 24374f9f1052..efb14edb6238 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/ToolWindowImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/ToolWindowImpl.java @@ -36,13 +36,9 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; import java.awt.event.InputEvent; -import java.awt.event.KeyEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; /** * @author Anton Katilin @@ -67,18 +63,6 @@ public final class ToolWindowImpl implements ToolWindowEx { private boolean myPlaceholderMode; private ToolWindowFactory myContentFactory; - private static final Set FORWARD_TRAVERSAL_KEYSTROKES = new HashSet<>(Arrays.asList( - new KeyStroke[]{ - KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0) - } - )); - - private static final Set BACKWARD_TRAVERSAL_KEYSTROKES = new HashSet<>(Arrays.asList( - new KeyStroke[]{ - KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK) - } - )); - private final BusyObject.Impl myShowing = new BusyObject.Impl() { @Override public boolean isReady() { @@ -106,8 +90,6 @@ public final class ToolWindowImpl implements ToolWindowEx { myComponent = myContentManager.getComponent(); - installToolwindowFocusPolicy(); - UiNotifyConnector notifyConnector = new UiNotifyConnector(myComponent, new Activatable.Adapter() { @Override public void showNotify() { @@ -117,50 +99,6 @@ public final class ToolWindowImpl implements ToolWindowEx { Disposer.register(myContentManager, notifyConnector); } - /** - * Installs a focus traversal policy for the tool window. - * If the policy cannot handle a keystroke, it delegates the handling to - * the nearest ancestors focus traversal policy. For instance, - * this policy does not handle KeyEvent.VK_ESCAPE, so it can delegate the handling - * to a ThreeComponentSplitter instance. - */ - private void installToolwindowFocusPolicy() { - - myComponent.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, FORWARD_TRAVERSAL_KEYSTROKES); - myComponent.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYSTROKES); - - FocusTraversalPolicy layoutFocusTraversalPolicy = new LayoutFocusTraversalPolicy(); - - myComponent.setFocusCycleRoot(true); - myComponent.setFocusTraversalPolicyProvider(true); - myComponent.setFocusTraversalPolicy(new FocusTraversalPolicy() { - @Override - public Component getComponentAfter(Container container, Component component) { - return layoutFocusTraversalPolicy.getComponentAfter(container, component); - } - - @Override - public Component getComponentBefore(Container container, Component component) { - return layoutFocusTraversalPolicy.getComponentBefore(container, component); - } - - @Override - public Component getFirstComponent(Container container) { - return layoutFocusTraversalPolicy.getFirstComponent(container); - } - - @Override - public Component getLastComponent(Container container) { - return layoutFocusTraversalPolicy.getLastComponent(container); - } - - @Override - public Component getDefaultComponent(Container container) { - return layoutFocusTraversalPolicy.getDefaultComponent(container); - } - }); - } - public final void addPropertyChangeListener(final PropertyChangeListener l) { myChangeSupport.addPropertyChangeListener(l); }