Skip to content

Commit

Permalink
Merge pull request #4603 from errael/KeyboardFocusCanActivateTopCompo…
Browse files Browse the repository at this point in the history
…nent

Keyboard focus can activate top component
  • Loading branch information
geertjanw committed Oct 10, 2022
2 parents cafc4bb + 486b048 commit 2c27f5a
Showing 1 changed file with 33 additions and 6 deletions.
Expand Up @@ -36,13 +36,17 @@
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.AWTEventListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.netbeans.core.windows.ModeImpl;
import org.netbeans.core.windows.Switches;
import org.netbeans.core.windows.view.ui.slides.SlideBar;
Expand Down Expand Up @@ -82,6 +86,8 @@ public TabbedHandler(ModeView modeView, int kind, Tabbed tbd) {
activationManager = new ActivationManager();
Toolkit.getDefaultToolkit().addAWTEventListener(
activationManager, AWTEvent.MOUSE_EVENT_MASK);
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener("focusOwner", activationManager);
}
}
tabbed = tbd;
Expand Down Expand Up @@ -464,11 +470,33 @@ public static void handleMaximization(TabActionEvent tae) {

/** Well, we can't totally get rid of AWT event listeners - this is what
* keeps track of the activated mode. */
private static class ActivationManager implements AWTEventListener {
private static class ActivationManager implements AWTEventListener, PropertyChangeListener {
@Override
public void eventDispatched(AWTEvent e) {
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
handleActivation((MouseEvent) e);
handleActivation(e.getSource());
}
}

/**
* Keyboard focus change event handler. Handle situation where
* active TopComponent was in a different window and window
* changed without a mouse event.
* See
* Editor with Keyboard focus is not active TopComponent
* https://github.com/apache/netbeans/issues/4437
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
Frame mainWindowNB = WindowManagerImpl.getInstance().getMainWindow();
Window activeWindowKB = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
TopComponent currentTC = TopComponent.getRegistry().getActivated();
// Only do something if focus to the main window and
// active TC is not in the main window. Note that focus changes
// to detached windows handled in DefaultSeparateContainer.
if(mainWindowNB == activeWindowKB
&& mainWindowNB != SwingUtilities.getRoot(currentTC)) {
handleActivation(e.getNewValue());
}
}

Expand All @@ -490,12 +518,11 @@ public void eventDispatched(AWTEvent e) {
* components. This behavior is compatible with all window managers I can
* imagine.
*/
private void handleActivation(MouseEvent evt) {
Object obj = evt.getSource();
if (!(obj instanceof Component)) {
private void handleActivation(Object evtObject) {
if (!(evtObject instanceof Component)) {
return;
}
Component comp = (Component) obj;
Component comp = (Component) evtObject;

while (comp != null && !(comp instanceof ModeComponent)) {
if (comp instanceof JComponent) {
Expand Down

0 comments on commit 2c27f5a

Please sign in to comment.