Skip to content

Commit 69ad981

Browse files
hansonrhansonr
authored andcommitted
label/button work for any alignment
1 parent 5bcc8fe commit 69ad981

16 files changed

+327
-514
lines changed

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

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,14 @@
3333
import java.awt.Graphics;
3434
import java.awt.event.MouseEvent;
3535
import java.awt.event.MouseMotionListener;
36+
3637
import javax.swing.AbstractButton;
3738
import javax.swing.BorderFactory;
3839
import javax.swing.ImageIcon;
3940
import javax.swing.JComponent;
4041
import javax.swing.JMenuItem;
41-
import javax.swing.JTable;
42-
import javax.swing.JToggleButton;
4342
import javax.swing.LookAndFeel;
44-
import javax.swing.SwingConstants;
4543
import javax.swing.UIManager;
46-
import javax.swing.plaf.TableUI;
4744
import javax.swing.plaf.UIResource;
4845

4946
import swingjs.api.js.DOMNode;
@@ -78,7 +75,6 @@ public class JSButtonUI extends JSLightweightUI {
7875
/**
7976
* a wrapper if this is not a menu item
8077
*/
81-
protected DOMNode itemNode;
8278
protected JMenuItem menuItem;
8379
protected AbstractButton button;
8480

@@ -90,20 +86,16 @@ public DOMNode updateDOMNode() {
9086
// all subclasses will have their own version of this.
9187
// this one is only for a simple button
9288
if (domNode == null) {
93-
domNode = buttonNode = newDOMObject ("button", id, "type", "button");
94-
iconNode = newDOMObject("span", id + "_icon");
95-
enableNode = buttonNode;
89+
domNode = enableNode = buttonNode = newDOMObject ("button", id + "_dom", "type", "button");
90+
iconNode = null;
9691
createButton();
97-
DOMNode.setStyles(domNode, "lineHeight", "0.8");
9892
}
9993
setupButton();
10094
return updateDOMNodeCUI();
10195
}
10296

10397
protected void createButton() {
104-
textNode = newDOMObject ("span", id + "_txt");
105-
buttonNode.appendChild(iconNode);
106-
buttonNode.appendChild(textNode);
98+
addCentering(buttonNode);
10799
setDataComponent(domNode);
108100
setDataComponent(iconNode); // needed for mac safari/chrome
109101
setDataComponent(textNode); // needed for mac safari/chrome
@@ -114,11 +106,11 @@ protected void createButton() {
114106
*
115107
* @param type
116108
* "_item" or "_menu"
117-
* @param label
109+
* @param buttonNode
118110
* will be a for-label for radio and checkbox only; otherwise null
119111
* @return
120112
*/
121-
protected DOMNode createItem(String type, DOMNode label) {
113+
protected DOMNode createItem(String type, DOMNode buttonNode) {
122114
// all subclasses will call this method, including
123115
// standard MenuItem and Menu labels
124116

@@ -128,34 +120,28 @@ protected DOMNode createItem(String type, DOMNode label) {
128120
String text = button.getText();
129121
ImageIcon icon = (ImageIcon) button.getIcon();
130122
int gap = button.getIconTextGap();
131-
if (("|").equals(text) || ("-").equals(text)) {
123+
isMenuSep = (("|").equals(text) || ("-").equals(text));
124+
if (isMenuSep) {
125+
// separator masquerading as a menu item
132126
text = null;
133127
}
134128
itemNode = newDOMObject("li", id + type);
135129
if (text == null && icon == null)
136130
return itemNode;
137-
centeringNode = menuAnchorNode = newDOMObject("a", id + type + "_a");
131+
menuAnchorNode = newDOMObject("a", id + type + "_a");
138132
DOMNode.setStyles(menuAnchorNode, "margin", "1px 2px 1px 2px");
139133
itemNode.appendChild(menuAnchorNode);
140-
if (label == null) {
134+
if (buttonNode == null) {
141135
// not a radio or checkbox
142-
// TODO: add vertical centering
143-
boolean hasIcon = (iconNode != null);
144-
if (iconNode == null)
145-
iconNode = newDOMObject("span", id + "_icon");
146-
if (textNode == null)
147-
textNode = newDOMObject("span", id + "_text");
136+
addCentering(menuAnchorNode);
148137
$(iconNode).attr("role", "menucloser");
149138
$(textNode).attr("role", "menucloser");
150139
setDataUI(iconNode);
151140
setDataUI(textNode);
152-
menuAnchorNode.appendChild(iconNode);
153-
menuAnchorNode.appendChild(textNode);
154-
setCssFont(menuAnchorNode, c.getFont());
155141
enableNode = menuAnchorNode;
156142
setIconAndText("btn", icon, gap, text);
157143
} else {
158-
menuAnchorNode.appendChild(label);
144+
menuAnchorNode.appendChild(buttonNode);
159145
}
160146
// j2sMenu.js will set the mouse-up event for the <a> tag with the
161147
// role=menuitem
@@ -169,14 +155,14 @@ protected DOMNode createItem(String type, DOMNode label) {
169155
}
170156

171157
protected void setupButton() {
172-
if (!isMenuItem)
173-
setPadding(button.getMargin());
174158
setIconAndText("button", (ImageIcon) button.getIcon(), button.getIconTextGap(), button.getText());
175159
// "emptyBorder" is not really empty.
176160
if (button.getBorder() == null || button.getBorder() == BorderFactory.emptyBorder)
177161
DOMNode.setStyles(buttonNode, "border", "none");
178162
else if (button.getBorder() == BorderFactory.html5Border)
179163
DOMNode.setStyles(buttonNode, "border", null);
164+
if (!isMenuSep)
165+
setAlignments(button);
180166
}
181167

182168
/**
@@ -744,14 +730,21 @@ protected Dimension getCSSAdjustment(boolean addingCSS) {
744730
@Override
745731
protected void setInnerComponentBounds(int width, int height) {
746732
if (isSimpleButton && (imageNode == null || button.getText() == null))
747-
DOMNode.setSize(innerNode = domNode, width, height);
733+
DOMNode.setSize(domNode, width, height);
748734
}
749735

750736
@Override
751737
public Dimension getMaximumSize(JComponent c) {
752738
Dimension d = getPreferredSize(c);
753739
return d;
754740
}
741+
742+
@Override
743+
protected Dimension getHTMLSizePreferred(DOMNode obj, boolean addCSS) {
744+
// addCSS is always false
745+
setAlignments(button, !addCSS);
746+
return setHTMLSize1(obj, addCSS, true);
747+
}
755748

756749
@Override
757750
public void paint(Graphics g, JComponent c) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class JSCheckBoxMenuItemUI extends JSCheckBoxUI {
1010
public JSCheckBoxMenuItemUI() {
1111
super();
1212
isMenuItem = true;
13-
actionItemOffset = "-7px";
13+
actionItemOffset = -7;
1414
}
1515

1616
@Override

0 commit comments

Comments
 (0)