Skip to content

Commit cceb0b8

Browse files
hansonrhansonr
authored andcommitted
JButtonUI update for ImageIcon disabledIcon
1 parent 6e37ae0 commit cceb0b8

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

sources/net.sf.j2s.java.core/src/java/awt/JSComponent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ public interface A2SWrappedComponent {
153153
*
154154
*/
155155
private int 秘iPaintMyself = PAINTS_SELF_UNKNOWN;
156+
157+
/**
158+
* paint(g) or paintComponent(g) or update(g) or paintContainer(g) is overridden
159+
*/
156160
private boolean 秘iPaintMyselfEntirely;
157161
private boolean 秘repaintAsUpdate = true;
158162
private static boolean 秘isRepaint = true;

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232
import java.awt.Dimension;
3333
import java.awt.Graphics;
3434
import java.awt.Insets;
35+
import java.awt.JSComponent;
3536
import java.awt.event.KeyEvent;
3637
import java.awt.event.MouseEvent;
3738
import java.awt.event.MouseMotionListener;
39+
import java.awt.image.BufferedImage;
3840

3941
import javax.swing.AbstractButton;
4042
import javax.swing.BorderFactory;
43+
import javax.swing.ButtonModel;
4144
import javax.swing.Icon;
4245
import javax.swing.ImageIcon;
4346
import javax.swing.JComponent;
@@ -46,6 +49,7 @@
4649
import javax.swing.UIManager;
4750
import javax.swing.plaf.UIResource;
4851

52+
import swingjs.JSToolkit;
4953
import swingjs.api.js.DOMNode;
5054

5155
/**
@@ -141,7 +145,7 @@ protected DOMNode createItem(String type, DOMNode buttonNode) {
141145
// all subclasses will call this method, including
142146
// standard MenuItem and Menu labels
143147
String text = button.getText();
144-
ImageIcon icon = (ImageIcon) button.getIcon();
148+
Icon icon = getIcon();
145149
int gap = button.getIconTextGap();
146150
isMenuSep = (("|").equals(text) || ("-").equals(text));
147151
if (isMenuSep) {
@@ -188,21 +192,24 @@ protected void setMenuItem() {
188192
setMenuItem(centeringNode);
189193
}
190194

195+
@SuppressWarnings("unused")
191196
@Override
192197
protected void enableNode(DOMNode node, boolean b) {
198+
if (node == null || isUIDisabled)
199+
return;
193200
if (isMenuItem) {
194201
if (b) {
195202
removeClass(node, JSPopupMenuUI.UI_DISABLED);
196203
} else {
197204
addClass(node, JSPopupMenuUI.UI_DISABLED);
198205
}
199-
} else {
200-
super.enableNode(node, b);
206+
return;
201207
}
208+
super.enableNode(node, b);
202209
}
203210

204211
protected void setupButton() {
205-
setIconAndText("button", (ImageIcon) button.getIcon(), button.getIconTextGap(), button.getText());
212+
setIconAndText("button", getIcon(), button.getIconTextGap(), button.getText());
206213
// "emptyBorder" is not really empty.
207214
if (button.getBorder() == null || button.getBorder() == BorderFactory.emptyBorder)
208215
DOMNode.setStyle(buttonNode, "border", "none");
@@ -802,13 +809,38 @@ protected Dimension getHTMLSizePreferred(DOMNode obj, boolean addCSS) {
802809
return setHTMLSize1(obj, addCSS, true);
803810
}
804811

812+
/**
813+
* set in getIcon(); checked in paint
814+
*
815+
*/
816+
private Icon thisIcon;
817+
818+
@SuppressWarnings("unused")
805819
@Override
806820
public void paint(Graphics g, JComponent c) {
807-
if (jc.秘paintsSelfEntirely())
821+
if (jc.秘paintsSelfEntirely()) {
808822
DOMNode.setStyle(centeringNode, "visibility", "visible");
823+
} else if (currentIcon != null && imageNode != null && imagePersists) {
824+
Icon icon = thisIcon;
825+
if (icon != null && icon != getIcon()) {
826+
$(iconNode).empty();
827+
currentIcon = getOrCreateIcon(c, thisIcon);
828+
imageNode = ((BufferedImage) currentIcon.getImage()).秘getImageNode(BufferedImage.GET_IMAGE_FOR_ICON);
829+
iconNode.appendChild(imageNode);
830+
}
831+
}
809832
super.paint(g, c);
810833
}
811834

835+
@Override
836+
protected Icon getIcon() {
837+
ButtonModel model = button.getModel();
838+
Icon disabledIcon = /** @j2sNative this.button.disabledIcon || */
839+
null;
840+
return thisIcon = (disabledIcon != null && !model.isEnabled() ? disabledIcon : button.getIcon());
841+
}
842+
843+
812844
@Override
813845
public boolean isFocusable() {
814846
return true;
@@ -819,7 +851,7 @@ public Dimension getPreferredSize(JComponent jc) {
819851
if (isAWT && isSimpleButton)
820852
return JSLabelUI.getMinimumSizePeer(jc, button);
821853
Icon ic;
822-
if (!isSimpleButton || isAWT || button.getText() != null || (ic = button.getIcon()) == null)
854+
if (!isSimpleButton || isAWT || button.getText() != null || (ic = getIcon()) == null)
823855
return super.getPreferredSize(jc);
824856
Insets in = button.getInsets();
825857
return new Dimension(in.left + in.right + ic.getIconWidth(), in.top + in.bottom + ic.getIconHeight());

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ protected void restoreCellNodes(DOMNode td) {
578578
protected Color inactiveForeground = colorUNKNOWN,
579579
inactiveBackground = colorUNKNOWN;
580580

581+
/**
582+
* from JButton.isEnabled()
583+
*/
581584
private boolean enabled = true;
582585

583586
/**
@@ -1448,7 +1451,7 @@ protected void updateIcon() {
14481451
// note that we use AbstractButton cast here just because
14491452
// it has a getIcon() method. JavaScript will not care if
14501453
// it is really a JLabel or JOptionPane, which also have icons
1451-
ImageIcon icon = getIcon(c, null);
1454+
ImageIcon icon = getOrCreateIcon(c, null);
14521455
if (icon == null ? currentIcon != null : !icon.equals(currentIcon))
14531456
setIconAndText("icon", icon, currentGap, currentText);
14541457
}
@@ -2442,12 +2445,22 @@ protected void setJSDimensions(int width, int height) {
24422445
protected void setInnerComponentBounds(int width, int height) {
24432446
}
24442447

2445-
private ImageIcon getIcon(JSComponent c, Icon icon) {
2446-
return (c == null || icon == null && (icon = ((AbstractButton) c).getIcon()) == null ? null
2448+
/**
2449+
* Create an image icon if necessary
2450+
* @param c
2451+
* @param icon
2452+
* @return
2453+
*/
2454+
protected ImageIcon getOrCreateIcon(JSComponent c, Icon icon) {
2455+
return (c == null || icon == null && (icon = getIcon()) == null ? null
24472456
: icon.getIconWidth() <= 0 || icon.getIconHeight() <= 0 ? null
24482457
: (icon instanceof ImageIcon) ? (ImageIcon) icon : JSToolkit.createImageIcon(jc, icon, id + "tmpIcon"));
24492458
}
24502459

2460+
protected Icon getIcon() {
2461+
return ((AbstractButton) c).getIcon();
2462+
}
2463+
24512464
@SuppressWarnings("unused")
24522465
private static Object re0 = /** @j2sNative new RegExp("\u0000","gm") || */null;
24532466
@SuppressWarnings("unused")
@@ -2500,13 +2513,13 @@ protected void setIconAndText(String prop, Icon icon, int gap, String text) {
25002513
imageNode = null;
25012514
int h = 0;
25022515
if (iconNode != null) {
2503-
icon = currentIcon = getIcon(jc, icon);
2516+
icon = currentIcon = getOrCreateIcon(jc, icon);
25042517
$(iconNode).empty();
25052518
if (currentIcon != null) {
25062519
imageNode = ((BufferedImage) currentIcon.getImage()).秘getImageNode(BufferedImage.GET_IMAGE_FOR_ICON);
2520+
iconNode.appendChild(imageNode);
25072521
if (DOMNode.getAttr(imageNode, "tagName") == "VIDEO")
25082522
isVideoIcon = imagePersists = true;
2509-
iconNode.appendChild(imageNode);
25102523
int w;
25112524
if (isVideoIcon) {
25122525
if (jc.isPreferredSizeSet()) {

0 commit comments

Comments
 (0)