< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicButtonUI.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 258     public void paint(Graphics g, JComponent c)
 259     {
 260         AbstractButton b = (AbstractButton) c;
 261         ButtonModel model = b.getModel();
 262 
 263         String text = layout(b, SwingUtilities2.getFontMetrics(b, g),
 264                b.getWidth(), b.getHeight());
 265 
 266         clearTextShiftOffset();
 267 
 268         // perform UI specific press action, e.g. Windows L&F shifts text
 269         if (model.isArmed() && model.isPressed()) {
 270             paintButtonPressed(g,b);
 271         }
 272 
 273         // Paint the Icon
 274         if(b.getIcon() != null) {
 275             paintIcon(g,c,iconRect);
 276         }
 277 
 278         if (text != null && !text.equals("")){
 279             View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 280             if (v != null) {
 281                 v.paint(g, textRect);
 282             } else {
 283                 paintText(g, b, textRect, text);
 284             }
 285         }
 286 
 287         if (b.isFocusPainted() && b.hasFocus()) {
 288             // paint UI specific focus
 289             paintFocus(g,b,viewRect,textRect,iconRect);
 290         }
 291     }
 292 
 293     /**
 294      * Paints an icon of the current button.
 295      *
 296      * @param g an instance of {@code Graphics}
 297      * @param c a component
 298      * @param iconRect a bounding rectangle to render the icon


 478         Dimension d = getPreferredSize(c);
 479         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 480         if (v != null) {
 481             d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
 482         }
 483         return d;
 484     }
 485 
 486     /**
 487      * Returns the baseline.
 488      *
 489      * @throws NullPointerException {@inheritDoc}
 490      * @throws IllegalArgumentException {@inheritDoc}
 491      * @see javax.swing.JComponent#getBaseline(int, int)
 492      * @since 1.6
 493      */
 494     public int getBaseline(JComponent c, int width, int height) {
 495         super.getBaseline(c, width, height);
 496         AbstractButton b = (AbstractButton)c;
 497         String text = b.getText();
 498         if (text == null || "".equals(text)) {
 499             return -1;
 500         }
 501         FontMetrics fm = b.getFontMetrics(b.getFont());
 502         layout(b, fm, width, height);
 503         return BasicHTML.getBaseline(b, textRect.y, fm.getAscent(),
 504                                      textRect.width, textRect.height);
 505     }
 506 
 507     /**
 508      * Returns an enum indicating how the baseline of the component
 509      * changes as the size changes.
 510      *
 511      * @throws NullPointerException {@inheritDoc}
 512      * @see javax.swing.JComponent#getBaseline(int, int)
 513      * @since 1.6
 514      */
 515     public Component.BaselineResizeBehavior getBaselineResizeBehavior(
 516             JComponent c) {
 517         super.getBaselineResizeBehavior(c);
 518         if (c.getClientProperty(BasicHTML.propertyKey) != null) {


   1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 258     public void paint(Graphics g, JComponent c)
 259     {
 260         AbstractButton b = (AbstractButton) c;
 261         ButtonModel model = b.getModel();
 262 
 263         String text = layout(b, SwingUtilities2.getFontMetrics(b, g),
 264                b.getWidth(), b.getHeight());
 265 
 266         clearTextShiftOffset();
 267 
 268         // perform UI specific press action, e.g. Windows L&F shifts text
 269         if (model.isArmed() && model.isPressed()) {
 270             paintButtonPressed(g,b);
 271         }
 272 
 273         // Paint the Icon
 274         if(b.getIcon() != null) {
 275             paintIcon(g,c,iconRect);
 276         }
 277 
 278         if (text != null && !text.isEmpty()){
 279             View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 280             if (v != null) {
 281                 v.paint(g, textRect);
 282             } else {
 283                 paintText(g, b, textRect, text);
 284             }
 285         }
 286 
 287         if (b.isFocusPainted() && b.hasFocus()) {
 288             // paint UI specific focus
 289             paintFocus(g,b,viewRect,textRect,iconRect);
 290         }
 291     }
 292 
 293     /**
 294      * Paints an icon of the current button.
 295      *
 296      * @param g an instance of {@code Graphics}
 297      * @param c a component
 298      * @param iconRect a bounding rectangle to render the icon


 478         Dimension d = getPreferredSize(c);
 479         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 480         if (v != null) {
 481             d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
 482         }
 483         return d;
 484     }
 485 
 486     /**
 487      * Returns the baseline.
 488      *
 489      * @throws NullPointerException {@inheritDoc}
 490      * @throws IllegalArgumentException {@inheritDoc}
 491      * @see javax.swing.JComponent#getBaseline(int, int)
 492      * @since 1.6
 493      */
 494     public int getBaseline(JComponent c, int width, int height) {
 495         super.getBaseline(c, width, height);
 496         AbstractButton b = (AbstractButton)c;
 497         String text = b.getText();
 498         if (text == null || text.isEmpty()) {
 499             return -1;
 500         }
 501         FontMetrics fm = b.getFontMetrics(b.getFont());
 502         layout(b, fm, width, height);
 503         return BasicHTML.getBaseline(b, textRect.y, fm.getAscent(),
 504                                      textRect.width, textRect.height);
 505     }
 506 
 507     /**
 508      * Returns an enum indicating how the baseline of the component
 509      * changes as the size changes.
 510      *
 511      * @throws NullPointerException {@inheritDoc}
 512      * @see javax.swing.JComponent#getBaseline(int, int)
 513      * @since 1.6
 514      */
 515     public Component.BaselineResizeBehavior getBaselineResizeBehavior(
 516             JComponent c) {
 517         super.getBaselineResizeBehavior(c);
 518         if (c.getClientProperty(BasicHTML.propertyKey) != null) {


< prev index next >