Thanks to visit codestin.com
Credit goes to github.com

Skip to content

JLabel for JPopupMenu #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 7 additions & 1 deletion sources/net.sf.j2s.java.core/src/test/Test_J8_Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ public static void main(String[] args) {
String st = "test\ning\r\nnow";
String[] lines = st.split("\n");
int[] ptr = new int[1];
long n = 0;
if (/** @j2sNative true || */
false) {
// this is Java 11, so JavaScript-only test here
String s = "\u0000test";
assert (s.trim().length() == 4);
n = (/** @j2sNative s.strip$().length || */0);
System.out.println("strip gives length " + n);
assert (n == 5);

Stream<String> o = /** @j2sNative st.lines$() || */
null;
if (o != null) {
Expand Down Expand Up @@ -89,7 +96,6 @@ public void accept(String value) {
Consumer<Integer> cc = param::set;
Supplier<Integer> sup = param::get;

long n;
StringBuffer sb = new StringBuffer("test");
System.out.println("below is 'test'? " + sb);
sb.chars().mapToObj(i -> (char) i).forEach(System.out::print);
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