Skip to content

Commit 39fb513

Browse files
committed
Focus fix for JSPopupMenuUI collapseAll
1 parent 0cab9dc commit 39fb513

File tree

4 files changed

+67
-33
lines changed

4 files changed

+67
-33
lines changed

sources/net.sf.j2s.java.core/src/javax/swing/JPopupMenu.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ public void pack() {
680680
}
681681
}
682682

683+
684+
public void hide() {
685+
System.out.println("JPopupMenu hide");
686+
super.hide();
687+
}
683688
/**
684689
* Sets the visibility of the popup menu.
685690
*
@@ -696,8 +701,14 @@ public void setVisible(boolean b) {
696701
return;
697702
}
698703
getUI().setVisible(b);
699-
if (!b)
704+
if (!b) {
700705
popup = null;
706+
707+
System.out.println("JPopupMenu.setVis false to invoker " + invoker);
708+
// SwingJS added
709+
if (invoker != null && invoker.isFocusable())
710+
invoker.requestFocus();
711+
}
701712
// /**
702713
// * @j2sNative
703714
// *

sources/net.sf.j2s.java.core/src/swingjs/JSToolkit.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -651,22 +651,6 @@ public static boolean hasFocus(Component c) {
651651
return (ui != null && !ui.isNull && ui.hasFocus());
652652
}
653653

654-
public static boolean requestFocus(Component c) {
655-
final JSComponentUI ui = getUI(c, false);
656-
if (ui == null || ui.isNull || !ui.jc.isFocusable())
657-
return false;
658-
Runnable r = new Runnable() {
659-
660-
@Override
661-
public void run() {
662-
ui.requestFocus(null, false, false, 0, null);
663-
}
664-
665-
};
666-
dispatch(r, 50, 0);
667-
return true;
668-
}
669-
670654
private static JSAudio audioPlayer;
671655

672656
private static JSAudio getAudioPlayer() {

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComponentUI.java

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,10 @@ private void setTabIndex(int i) {
947947
}
948948
}
949949

950+
/**
951+
* Called by DefaultFocusTraversalPolicy only.
952+
* Overridden to return true in buttons, menu items, JEditorPane, JTextPane, JTextArea
953+
*/
950954
@Override
951955
public boolean isFocusable() {
952956
// meaning "can use TAB to set their focus" within a focus cycle
@@ -970,7 +974,7 @@ public boolean isFocusable() {
970974
// return (jc.isFocusable() && setFocusable());
971975
}
972976

973-
protected boolean setFocusable() {
977+
public boolean setFocusable() {
974978
if (focusNode == null)
975979
addFocusHandler();
976980
if (focusNode != null)
@@ -984,16 +988,14 @@ public boolean hasFocus() {
984988
*/false;
985989
}
986990

987-
@Override
988-
public boolean requestFocus(Component lightweightChild, boolean temporary, boolean focusedWindowChangeAllowed,
989-
long time, Cause cause) {
990-
if (lightweightChild == null)
991-
return focus();
992-
if (!jc.isFocusable())
993-
return false;
994-
setFocusable();
995-
return JSToolkit.requestFocus(lightweightChild);
996-
}
991+
private Runnable focusRunnable = new Runnable() {
992+
993+
@Override
994+
public void run() {
995+
focus();
996+
}
997+
998+
};
997999

9981000
/**
9991001
* Outgoing: Initiate a focus() event in the system directly
@@ -1017,6 +1019,29 @@ protected void setComponentFocus() {
10171019
c.requestFocus();
10181020
}
10191021

1022+
/**
1023+
* A Window ComponentPeer method called from Component.requestFocusHelper
1024+
* in response to a requestFocus() call.
1025+
*
1026+
* @param me The lightweight component to receive the focus.
1027+
* @param temporary ignored
1028+
* @param focusedWindowChangeAllowed ignored
1029+
* @param time ignored
1030+
* @param cause ignored
1031+
* @return true if successful
1032+
*/
1033+
@Override
1034+
public boolean requestFocus(Component me, boolean temporary, boolean focusedWindowChangeAllowed, long time,
1035+
Cause cause) {
1036+
if (me == null)
1037+
return focus();
1038+
if (!me.isFocusable())
1039+
return false;
1040+
JSComponentUI ui = ((JSComponent) me).秘getUI();
1041+
ui.setFocusable();
1042+
JSToolkit.dispatch(ui.focusRunnable, 50, 0);
1043+
return true;
1044+
}
10201045
/**
10211046
* Add the $().focus() and $().blur() events to a DOM button.
10221047
*
@@ -3500,10 +3525,12 @@ protected void setBackgroundDOM(DOMNode node, Color color) {
35003525
public void clearPaintPath() {
35013526
JSComponent c = jc;
35023527
while (c != null) {
3503-
((JSComponentUI) c.ui).inPaintPath = true;
3504-
3528+
JSComponentUI ui = c.秘getUI();
3529+
if (ui == null)
3530+
return;
3531+
ui.inPaintPath = true;
35053532
c.秘setPaintsSelf(JSComponent.PAINTS_SELF_ALWAYS);
3506-
((JSComponentUI) c.ui).setTransparent();
3533+
ui.setTransparent();
35073534
c = c.getParent();
35083535
}
35093536
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSPopupMenuUI.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public void propertyChange(PropertyChangeEvent e) {
9292

9393
private static JSSwingMenu j2sSwingMenu;
9494

95+
private static Component lastInvoker;
96+
9597
private JPopupMenu popupMenu;
9698
// private PopupMenuListener popupMenuListener;
9799

@@ -200,6 +202,7 @@ public void setVisible(boolean b) {
200202
updateMenu(true);
201203
}
202204
}
205+
lastInvoker = menu.getInvoker();
203206
// issue here?? jc.addNotify();
204207
//have to cheat here; these values are private to JMenu.
205208
int x = /** @j2sNative this.menu.desiredLocationX || */0;
@@ -1193,8 +1196,13 @@ public static void processJ2SMenuCmd(Object[] data) {
11931196
case "collapseAll":
11941197
hideMenusAndToolTip();
11951198
isMenuOpen = false;
1196-
j2smenu.options.jPopupMenu.visible = false;
1197-
((JComponent) j2smenu.options.jPopupMenu.getInvoker()).getRootPane().requestFocus();
1199+
c = j2smenu.options.jPopupMenu;
1200+
c.visible = false;
1201+
JSPopupMenuUI ui = (JSPopupMenuUI) c.getUI();
1202+
JComponent invoker = ((JComponent)((JPopupMenu)c).getInvoker());
1203+
if (invoker instanceof JMenu)
1204+
invoker = invoker.getRootPane();
1205+
invoker.requestFocus();
11981206
break;
11991207
case "setFocus":
12001208
isMenuOpen = true;
@@ -1243,6 +1251,10 @@ public boolean isPopupTrigger(MouseEvent e) {
12431251

12441252
public static void closeAllMenus() {
12451253
// top-level or submenu:
1254+
if (lastInvoker != null) {
1255+
lastInvoker.requestFocus();
1256+
lastInvoker = null;
1257+
}
12461258
JSUtil.jQuery.$(".ui-j2smenu").hide();
12471259
JSUtil.jQuery.$(".ui-j2smenu-node").removeClass("ui-state-active").removeClass("ui-state-focus");
12481260
}

0 commit comments

Comments
 (0)