diff --git a/platform/core.windows/src/org/netbeans/core/windows/view/ui/TabbedHandler.java b/platform/core.windows/src/org/netbeans/core/windows/view/ui/TabbedHandler.java index bc1b284874ba..fbac9480616a 100644 --- a/platform/core.windows/src/org/netbeans/core/windows/view/ui/TabbedHandler.java +++ b/platform/core.windows/src/org/netbeans/core/windows/view/ui/TabbedHandler.java @@ -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; @@ -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; @@ -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()); } } @@ -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) {