Skip to content

Commit

Permalink
Fix: an unexpected component gains focus on changing active tab
Browse files Browse the repository at this point in the history
  • Loading branch information
kohii committed Jun 6, 2020
1 parent 5ac58c0 commit 22e7edf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Expand Up @@ -25,17 +25,20 @@
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.SwingUtilities;

import com.smoothcsv.core.ApplicationStatus;
import com.smoothcsv.core.csvsheet.edits.GridSheetUndoManager;
import com.smoothcsv.core.find.FindAndReplacePanel;
import com.smoothcsv.core.util.CoreBundle;
import com.smoothcsv.framework.Env;
import com.smoothcsv.framework.component.BaseTabView;
import com.smoothcsv.framework.component.support.SCFocusManager;
import com.smoothcsv.framework.component.support.SmoothComponentSupport;
import com.smoothcsv.swing.gridsheet.event.GridSheetFocusEvent;
import com.smoothcsv.swing.gridsheet.event.GridSheetFocusListener;
import com.smoothcsv.swing.gridsheet.model.DefaultGridSheetSelectionModel;
import com.smoothcsv.swing.utils.SwingUtils;
import command.app.CloseCommand;
import lombok.Getter;

Expand Down Expand Up @@ -145,10 +148,22 @@ public void requestFocus() {

@Override
protected void onTabActivated() {
boolean findAndReplacePanelVisible = ApplicationStatus.getInstance().isFindAndReplacePanelVisible();
FindAndReplacePanel findAndReplacePanel = FindAndReplacePanel.getInstance();
Component nextFocusOwner = getGridSheetPane();
if (findAndReplacePanelVisible) {
Component focusOwner = SCFocusManager.getFocusOwner();
Component closestAncestor = SwingUtils.getClosestAncestor(focusOwner, FindAndReplacePanel.class, false);
if (closestAncestor != null) {
// FindAndReplacePanel had focus on the previous active tab
nextFocusOwner = focusOwner;
}
}
super.onTabActivated();
if (ApplicationStatus.getInstance().isFindAndReplacePanelVisible()) {
FindAndReplacePanel findAndReplacePanel = FindAndReplacePanel.getInstance();
if (findAndReplacePanelVisible) {
findAndReplacePanel.open();
Component focusOwner = nextFocusOwner;
SwingUtilities.invokeLater(() -> focusOwner.requestFocusInWindow());
}
showCellValueOnValuePanel();
}
Expand Down
Expand Up @@ -690,4 +690,16 @@ public static Component getClosestDialog(Component c,
}
return null;
}

public static Component getClosestAncestor(Component c,
Class<? extends Component> ancestorClass,
boolean visibleOnly) {
for (; c != null; c = c.getParent()) {
if (ancestorClass.isAssignableFrom(c.getClass())
&& (!visibleOnly || c.isVisible())) {
return c;
}
}
return null;
}
}

0 comments on commit 22e7edf

Please sign in to comment.