3232import java .awt .Dimension ;
3333import java .awt .Graphics ;
3434import java .awt .Insets ;
35+ import java .awt .JSComponent ;
3536import java .awt .event .KeyEvent ;
3637import java .awt .event .MouseEvent ;
3738import java .awt .event .MouseMotionListener ;
39+ import java .awt .image .BufferedImage ;
3840
3941import javax .swing .AbstractButton ;
4042import javax .swing .BorderFactory ;
43+ import javax .swing .ButtonModel ;
4144import javax .swing .Icon ;
4245import javax .swing .ImageIcon ;
4346import javax .swing .JComponent ;
4649import javax .swing .UIManager ;
4750import javax .swing .plaf .UIResource ;
4851
52+ import swingjs .JSToolkit ;
4953import 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 ());
0 commit comments