Skip to content

Commit

Permalink
Allows for JLabel in JMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
BobHanson committed Mar 4, 2023
1 parent 3bda224 commit eabd241
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 41 deletions.
Binary file modified sources/net.sf.j2s.core/dist/swingjs/SwingJS-site.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20230301203155
20230304111725
Binary file modified sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/SwingJS-site.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20230301203155
20230304111725
Binary file modified sources/net.sf.j2s.java.core/dist/SwingJS-site.zip
Binary file not shown.
33 changes: 2 additions & 31 deletions sources/net.sf.j2s.java.core/src/swingjs/plaf/JSButtonUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,17 @@ protected DOMNode createItem(String type, DOMNode buttonNode) {
// separator masquerading as a menu item
text = null;
}
itemNode = newDOMObject("li", id);
if (text == null && icon == null)
return itemNode;
DOMNode.setStyle(itemNode, "outline", "none");
menuAnchorNode = newDOMObject("div", id + "_a");// this needed? , "tabindex", "8");
if (type != "_bar") {
addClass(menuAnchorNode, "a");
// DOMNode.setStyles(menuAnchorNode, "margin", "1px 2px 1px 2px", "height", "1em");
}
itemNode.appendChild(menuAnchorNode);
setDoPropagate();
if (buttonNode == null) {
// not a radio or checkbox
addCentering(menuAnchorNode);
enableNode = itemNode;
setIconAndText("btn", icon, gap, text);
} else {
menuAnchorNode.appendChild(buttonNode);
setMenuItem(buttonNode);
}
setMenuItem(menuAnchorNode);
setMenuItem();
if (createItemNode(type, icon, gap, text, buttonNode)) {
// j2sMenu.js will set the mouse-up event for the <a> tag with the
// role=menuitem
// attribute via j2sApplet.setMouse().
// That event will then fire handleJSEvent
setDataComponent(menuAnchorNode);
setDataComponent(itemNode);
}
return itemNode;
}

protected void setMenuItem() {
setMenuItem(itemNode);
setMenuItem(iconNode);
if (actionNode != null && actionNode != iconNode)
setMenuItem(actionNode);
setMenuItem(textNode);
setMenuItem(centeringNode);
}

@SuppressWarnings("unused")
@Override
protected void enableNode(DOMNode node, boolean b) {
Expand Down
45 changes: 43 additions & 2 deletions sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComponentUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ protected void restoreCellNodes(DOMNode td) {
*
*/
protected boolean isSimpleButton, isLabel;

protected boolean isMenuBarLabel;

protected int x, y;

Expand Down Expand Up @@ -1734,7 +1736,7 @@ public Dimension getHTMLSize(DOMNode obj) {
}

private Dimension getIconSize(AbstractButton b) {

//
return (iconNode == null || imageNode == null || b.getIcon() == null ? null
: new Dimension(b.getIcon().getIconWidth(), b.getIcon().getIconHeight()));
}
Expand Down Expand Up @@ -3035,10 +3037,10 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
itop = 70;
iscale = "scale(0.8,0.8)";
}

break;
}
if (itop >= 0) {
if (!isLabel || !isMenuBarLabel)
addJSKeyVal(cssTxt, "top", top + "%", "transform",
"translateY(" + (yoff == null ? "-" + top + "%" : yoff) + ")" + voff);
addJSKeyVal(cssIcon, "top", top + "%", "transform", "translateY(-" + itop + "%)" + voff + iscale);
Expand Down Expand Up @@ -3067,6 +3069,10 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
}

private void setMenuAnchorAndAccelerator(AbstractButton b, int wCtr, boolean ltr, Insets margins) {
if (isLabel) {
// allowing label here -- not nec AbstractButton
return;
}
int wAccel = 0;
if (isMenu) {
// Correct for dimText calc losing position:absolute
Expand Down Expand Up @@ -3847,5 +3853,40 @@ public void setPasteHandler(JSFunction handler) {
}
}

public boolean createItemNode(String type, Icon icon, int gap, String text, DOMNode buttonNode) {
itemNode = newDOMObject("li", id);
if (text == null && icon == null)
return false;
DOMNode.setStyle(itemNode, "outline", "none");
menuAnchorNode = newDOMObject("div", id + "_a");// this needed? , "tabindex", "8");
if (type != "_bar") {
addClass(menuAnchorNode, "a");
// DOMNode.setStyles(menuAnchorNode, "margin", "1px 2px 1px 2px", "height", "1em");
}
itemNode.appendChild(menuAnchorNode);
setDoPropagate();
if (buttonNode == null) {
// not a radio or checkbox
addCentering(menuAnchorNode);
enableNode = itemNode;
setIconAndText("btn", icon, gap, text);
} else {
menuAnchorNode.appendChild(buttonNode);
setMenuItem(buttonNode);
}
setMenuItem(menuAnchorNode);
setMenuItem();
return true;
}

protected void setMenuItem() {
setMenuItem(itemNode);
setMenuItem(iconNode);
if (actionNode != null && actionNode != iconNode)
setMenuItem(actionNode);
setMenuItem(textNode);
setMenuItem(centeringNode);
}


}
23 changes: 18 additions & 5 deletions sources/net.sf.j2s.java.core/src/swingjs/plaf/JSLabelUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPopupMenu;
import javax.swing.LookAndFeel;

import swingjs.api.js.DOMNode;
Expand All @@ -34,15 +36,26 @@ public JSLabelUI() {
public DOMNode updateDOMNode() {
// if (jc.getTopLevelAncestor() == null)
// return domNode;
isMenuItem = jc.getParent() instanceof JPopupMenu;
isMenuBarLabel = jc.getParent() instanceof JMenuBar;
if (domNode == null) {
enableNode = domNode = newDOMObject("label", id);
textNode = iconNode = null;
addCentering(domNode);
if (isMenuItem) {
createItemNode("_item", icon, 4, label.getText(), null);
domNode = itemNode;
// TODO -- still not positioned correctly
} else {
enableNode = domNode = newDOMObject("label", id);
textNode = iconNode = null;
addCentering(domNode);
}
}
getIconAndText(); // could be ToolTip
setIconAndText("label", icon, gap, text);
DOMNode.setStyles(domNode, "position", "absolute", "width", c.getWidth() + "px", "height",
c.getHeight() + "px");
if (isMenuItem) {
} else {
DOMNode.setStyles(domNode, "position", "absolute", "width", c.getWidth() + "px", "height",
c.getHeight() + "px");
}
updateCenteringNode();
if (allowTextAlignment) {
// not for JToolTip
Expand Down
7 changes: 6 additions & 1 deletion sources/net.sf.j2s.java.core/src/test/Test_Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
Expand Down Expand Up @@ -87,7 +88,7 @@ public Test_Menu() {
private JMenuBar getMenuBar() {
JMenuBar mb = new JMenuBar();
JMenu mb1 = new JMenu("Test1");
JMenu mb2 = new JMenu("Test2");
JMenu mb2 = new JMenu("Testing2");
JMenuItem mb1a = new JMenuItem("test-1");
mb1a.addActionListener((e)->{
System.out.println("test-1 clicked");
Expand Down Expand Up @@ -128,6 +129,10 @@ public void menuCanceled(MenuEvent e) {

mb.add(mb1);
mb.add(mb2);
JLabel l = new JLabel("label1");
mb.add(l);
l = new JLabel("label2");
mb1.add(l);

ActionListener al = new ActionListener() {

Expand Down

0 comments on commit eabd241

Please sign in to comment.