Skip to content

Commit 3c93e5b

Browse files
hansonrhansonr
authored andcommitted
menu fixes for late-switching of JPopupMenu and JMenuBar menus
1 parent aae03a6 commit 3c93e5b

File tree

7 files changed

+1044
-48
lines changed

7 files changed

+1044
-48
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,9 @@ public void setMenuLocation(int x, int y) {
562562
*/
563563
public JMenuItem add(JMenuItem menuItem) {
564564
ensurePopupMenuCreated();
565-
return popupMenu.add(menuItem);
565+
popupMenu.add(menuItem);
566+
firePropertyChange("JSitem", null, menuItem);
567+
return menuItem;
566568
}
567569

568570
/**
@@ -576,6 +578,7 @@ public JMenuItem add(JMenuItem menuItem) {
576578
public Component add(Component c) {
577579
ensurePopupMenuCreated();
578580
popupMenu.add(c);
581+
firePropertyChange("JSitem", null, c);
579582
return c;
580583
}
581584

@@ -593,6 +596,7 @@ public Component add(Component c) {
593596
public Component add(Component c, int index) {
594597
ensurePopupMenuCreated();
595598
popupMenu.add(c, index);
599+
firePropertyChange("JSitem", null, c);
596600
return c;
597601
}
598602

@@ -828,11 +832,9 @@ public void remove(int pos) {
828832
*/
829833
@Override
830834
public void remove(Component c) {
831-
// why did I add this? BH 2018 if (c instanceof JMenuItem)
832-
// if (popupMenu != null)
833-
// popupMenu.remove(c);
834835
if (popupMenu != null)
835836
popupMenu.remove(c);
837+
firePropertyChange("JSitem", c, null);
836838
}
837839

838840
/**
@@ -842,6 +844,7 @@ public void remove(Component c) {
842844
public void removeAll() {
843845
if (popupMenu != null)
844846
popupMenu.removeAll();
847+
firePropertyChange("JSitem", this, null);
845848
}
846849

847850
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public JPopupMenu(String label) {
190190
setOpaque(true); // BH added
191191
//setFocusTraversalKeysEnabled(false);
192192
updateUI();
193+
// no peer for JPopupMenus // getOrCreatePeer();
193194

194195
}
195196

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
*/
2828
package javax.swing;
2929

30-
import java.util.Vector;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
//import java.util.Vector;
3133

3234
import java.awt.Component;
3335
import java.awt.Point;
@@ -39,15 +41,15 @@
3941
import javax.swing.event.ChangeListener;
4042
import javax.swing.event.EventListenerList;
4143
import sun.awt.AppContext;
44+
import swingjs.JSMenuManager;
4245

4346
/**
4447
* A MenuSelectionManager owns the selection in menu hierarchy.
4548
*
4649
* @author Arnaud Weber
4750
*/
48-
@SuppressWarnings({"rawtypes", "unchecked"})
4951
public class MenuSelectionManager {
50-
private Vector selection = new Vector();
52+
private List<MenuElement> selection = new ArrayList<MenuElement>();//Vector();
5153

5254
// /* diagnostic aids -- should be false for production builds. */
5355
// private static final boolean TRACE = false; // trace creates and disposes
@@ -67,7 +69,7 @@ public static MenuSelectionManager defaultManager() {
6769
MenuSelectionManager msm = (MenuSelectionManager)context.get(
6870
MENU_SELECTION_MANAGER_KEY);
6971
if (msm == null) {
70-
msm = new MenuSelectionManager();
72+
msm = new JSMenuManager();
7173
context.put(MENU_SELECTION_MANAGER_KEY, msm);
7274
}
7375

@@ -109,21 +111,21 @@ public void setSelectedPath(MenuElement[] path) {
109111
// }
110112
//
111113
for(i=0,c=path.length;i<c;i++) {
112-
if(i < currentSelectionCount && (MenuElement)selection.elementAt(i) == path[i])
114+
if(i < currentSelectionCount && selection.get(i) == path[i])
113115
firstDifference++;
114116
else
115117
break;
116118
}
117119

118120
for(i=currentSelectionCount - 1 ; i >= firstDifference ; i--) {
119-
MenuElement me = (MenuElement)selection.elementAt(i);
120-
selection.removeElementAt(i);
121+
MenuElement me = selection.get(i);
122+
selection.remove(i);
121123
me.menuSelectionChanged(false);
122124
}
123125

124126
for(i = firstDifference, c = path.length ; i < c ; i++) {
125127
if (path[i] != null) {
126-
selection.addElement(path[i]);
128+
selection.add(path[i]);
127129
path[i].menuSelectionChanged(true);
128130
}
129131
}
@@ -140,7 +142,7 @@ public MenuElement[] getSelectedPath() {
140142
MenuElement res[] = new MenuElement[selection.size()];
141143
int i,c;
142144
for(i=0,c=selection.size();i<c;i++)
143-
res[i] = (MenuElement) selection.elementAt(i);
145+
res[i] = selection.get(i);
144146
return res;
145147
}
146148

@@ -223,7 +225,7 @@ public void processMouseEvent(MouseEvent event) {
223225
MenuElement menuElement;
224226
MenuElement subElements[];
225227
MenuElement path[];
226-
Vector tmp;
228+
//List tmp;
227229
int selectionSize;
228230
p = event.getPoint();
229231

@@ -250,11 +252,11 @@ public void processMouseEvent(MouseEvent event) {
250252
screenX = p.x;
251253
screenY = p.y;
252254

253-
tmp = (Vector)selection.clone();
254-
selectionSize = tmp.size();
255+
//tmp = (Vector)selection.clone();
256+
selectionSize = selection.size();//tmp.size();
255257
boolean success = false;
256258
for (i=selectionSize - 1;i >= 0 && success == false; i--) {
257-
menuElement = (MenuElement) tmp.elementAt(i);
259+
menuElement = (MenuElement) selection.get(i);
258260
subElements = menuElement.getSubElements();
259261

260262
path = null;
@@ -285,7 +287,7 @@ public void processMouseEvent(MouseEvent event) {
285287
if(path == null) {
286288
path = new MenuElement[i+2];
287289
for(k=0;k<=i;k++)
288-
path[k] = (MenuElement)tmp.elementAt(k);
290+
path[k] = (MenuElement)selection.get(k);
289291
}
290292
path[i+1] = subElements[j];
291293
MenuElement currentSelection[] = getSelectedPath();
@@ -385,18 +387,18 @@ public Component componentForPoint(Component source, Point sourcePoint) {
385387
int cWidth,cHeight;
386388
MenuElement menuElement;
387389
MenuElement subElements[];
388-
Vector tmp;
390+
//Vector tmp;
389391
int selectionSize;
390392

391393
SwingUtilities.convertPointToScreen(p,source);
392394

393395
screenX = p.x;
394396
screenY = p.y;
395397

396-
tmp = (Vector)selection.clone();
397-
selectionSize = tmp.size();
398+
//tmp = (Vector)selection.clone();
399+
selectionSize = selection.size();
398400
for(i=selectionSize - 1 ; i >= 0 ; i--) {
399-
menuElement = (MenuElement) tmp.elementAt(i);
401+
menuElement = (MenuElement) selection.get(i);
400402
subElements = menuElement.getSubElements();
401403

402404
for(j = 0, d = subElements.length ; j < d ; j++) {
@@ -481,7 +483,7 @@ public void processKeyEvent(KeyEvent e) {
481483
*/
482484
public boolean isComponentPartOfCurrentMenu(Component c) {
483485
if(selection.size() > 0) {
484-
MenuElement me = (MenuElement)selection.elementAt(0);
486+
MenuElement me = (MenuElement)selection.get(0);
485487
return isComponentPartOfCurrentMenu(me,c);
486488
} else
487489
return false;
@@ -505,4 +507,7 @@ private boolean isComponentPartOfCurrentMenu(MenuElement root,Component c) {
505507
}
506508
return false;
507509
}
510+
511+
512+
508513
}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.awt.Dimension;
3333
import java.awt.event.ContainerEvent;
3434
import java.awt.event.ContainerListener;
35+
import java.beans.PropertyChangeEvent;
3536

3637
import javax.swing.BoxLayout;
3738
import javax.swing.JComponent;
@@ -75,7 +76,20 @@ public DOMNode updateDOMNode() {
7576
return updateDOMNodeCUI();
7677
}
7778

78-
// private void setMenu() {
79+
@Override
80+
public void propertyChangedFromListener(String prop) {
81+
System.out.println("JSMenuBarUI prop = " + prop );
82+
super.propertyChangedFromListener(prop);
83+
}
84+
85+
@Override
86+
public void propertyChange(PropertyChangeEvent e) {
87+
String prop = e.getPropertyName();
88+
System.out.println("JSMenuBarUI prop " + prop);
89+
super.propertyChange(e);
90+
}
91+
92+
// private void setMenu() {
7993
//
8094
// /**
8195
// * @j2sNative
@@ -150,14 +164,15 @@ public Dimension getPreferredSize(JComponent jc) {
150164
public void componentAdded(ContainerEvent e) {
151165
// OK, the idea here is that we detach all child nodes
152166
// and then reattach them.
153-
DOMNode.detachAll(outerNode);
154-
setTainted();
155-
setHTMLElement();
167+
// (already done)
168+
// DOMNode.detachAll(domNode);
169+
// setTainted();
170+
// setHTMLElement();
156171
}
157172

158173
@Override
159174
public void componentRemoved(ContainerEvent e) {
160-
DOMNode.detachAll(outerNode);
175+
DOMNode.detachAll(domNode);
161176
setTainted();
162177
setHTMLElement();
163178
}

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

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import javax.swing.JComponent;
99
import javax.swing.JMenu;
10+
import javax.swing.event.MenuEvent;
11+
import javax.swing.event.MenuListener;
1012

1113
import swingjs.api.js.DOMNode;
1214
public class JSMenuUI extends JSMenuItemUI {
@@ -20,17 +22,21 @@ public JSMenuUI() {
2022

2123
@Override
2224
public DOMNode updateDOMNode() {
25+
boolean isMenuBarMenu = jm.isTopLevelMenu();
26+
if (domNode != null && isMenuItem == isMenuBarMenu) {
27+
// we have changed the type of menu, from popup to menubar or something like that.
28+
// the outerNode for a menu item is its domNode, the <li> tag. But a MenuBar needs
29+
// the outerNode. The domNode should be fine. The big change is the containerNode
30+
outerNode = null;
31+
if (isMenuBarMenu)
32+
DOMNode.detachAll(((JSComponentUI)jc.getParent().ui).domNode);
33+
}
2334
if (domNode == null) {
24-
isMenuItem = !jm.isTopLevelMenu();
25-
if (isMenuItem) {
26-
containerNode = domNode = createItem("_menu", null);
27-
} else {
28-
allowTextAlignment = false;
29-
domNode = createItem("_item", null);
30-
}
35+
domNode = createItem("_elem", null);
3136
bindJQueryEvents(domNode, "mouseenter mouseleave", -1);
3237
}
33-
38+
allowTextAlignment = isMenuItem = !isMenuBarMenu;
39+
containerNode = (isMenuBarMenu ? null : domNode);
3440
setCssFont(domNode, c.getFont());
3541
DOMNode.setVisible(domNode, jc.isVisible());
3642
return domNode;
@@ -55,29 +61,66 @@ public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) {
5561
}
5662
}
5763
return super.handleJSEvent(target, eventType, jQueryEvent);
64+
}
65+
66+
@Override
67+
public void propertyChangedFromListener(String prop) {
68+
System.out.println("JSMenuUI prop = " + prop + " " + jm.getText());
69+
super.propertyChangedFromListener(prop);
5870
}
5971

6072
@Override
6173
public void propertyChange(PropertyChangeEvent e) {
6274
String prop = e.getPropertyName();
75+
System.out.println("JSMenuUI prop " + prop);
6376
if (jc.isVisible()) {
6477
if (prop == "ancestor") {
65-
if (jc.getParent() != null) {
66-
if (domNode != null && isMenuItem == jm.isTopLevelMenu()) {
67-
reInit();
68-
outerNode = null;
69-
updateDOMNode();
70-
return;
71-
}
72-
}
78+
rebuild();
7379
}
7480
}
7581
super.propertyChange(e);
7682
}
7783

84+
private void rebuild() {
85+
if (jc.getParent() != null) {
86+
if (domNode != null && isMenuItem == jm.isTopLevelMenu()) {
87+
reInit();
88+
outerNode = null;
89+
updateDOMNode();
90+
return;
91+
}
92+
}
93+
}
94+
95+
7896
@Override
7997
public void installUI(JComponent jc) {
8098
jm = (JMenu) jc;
99+
100+
// jm.addMenuListener(new MenuListener() {
101+
//
102+
// @Override
103+
// public void menuSelected(MenuEvent e) {
104+
// // TODO Auto-generated method stub
105+
//
106+
// }
107+
//
108+
// @Override
109+
// public void menuDeselected(MenuEvent e) {
110+
// if (jm == null)
111+
// return;
112+
// System.out.println("check");
113+
// // $(domNode)e.ch.children()[1].hide();
114+
// // TODO Auto-generated method stub
115+
//
116+
// }
117+
//
118+
// @Override
119+
// public void menuCanceled(MenuEvent e) {
120+
// hideAllMenus();
121+
// }
122+
//
123+
// });
81124
super.installUI(jc);
82125
}
83126

0 commit comments

Comments
 (0)