1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/* * @(#)BasicToggleButtonUI.java 1.59 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.plaf.basic; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.plaf.*; import javax.swing.text.View; /** * BasicToggleButton implementation * <p> * * @version 1.59 11/17/05 * @author Jeff Dinkins */ public class BasicToggleButtonUI extends BasicButtonUI { private final static BasicToggleButtonUI toggleButtonUI = new BasicToggleButtonUI(); private final static String propertyPrefix = "ToggleButton" + "."; // ******************************** // Create PLAF // ******************************** public static ComponentUI createUI(JComponent b) { return toggleButtonUI; } protected String getPropertyPrefix() { return propertyPrefix; } // ******************************** // Paint Methods // ******************************** public void paint(Graphics g, JComponent c) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); Dimension size = b.getSize(); FontMetrics fm = g.getFontMetrics(); Insets i = c.getInsets(); Rectangle viewRect = new Rectangle(size); viewRect.x += i.left; viewRect.y += i.top; viewRect.width -= (i.right + viewRect.x); viewRect.height -= (i.bottom + viewRect.y); Rectangle iconRect = new Rectangle(); Rectangle textRect = new Rectangle(); Font f = c.getFont(); g.setFont(f); // layout the text and icon String text = SwingUtilities.layoutCompoundLabel( c, fm, b.getText(), b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, b.getText() == null ? 0 : b.getIconTextGap()); g.setColor(b.getBackground()); if (model.isArmed() && model.isPressed() || model.isSelected()) { paintButtonPressed(g,b); } // Paint the Icon if(b.getIcon() != null) { paintIcon(g, b, iconRect); } // Draw the Text if(text != null && !text.equals("")) { View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g, textRect); } else { paintText(g, b, textRect, text); } } // draw the dashed focus line. if (b.isFocusPainted() && b.hasFocus()) { paintFocus(g, b, viewRect, textRect, iconRect); } } protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) { ButtonModel model = b.getModel(); Icon icon = null; if(!model.isEnabled()) { if(model.isSelected()) { icon = (Icon) b.getDisabledSelectedIcon(); } else { icon = (Icon) b.getDisabledIcon(); } } else if(model.isPressed() && model.isArmed()) { icon = (Icon) b.getPressedIcon(); if(icon == null) { // Use selected icon icon = (Icon) b.getSelectedIcon(); } } else if(model.isSelected()) { if(b.isRolloverEnabled() && model.isRollover()) { icon = (Icon) b.getRolloverSelectedIcon(); if (icon == null) { icon = (Icon) b.getSelectedIcon(); } } else { icon = (Icon) b.getSelectedIcon(); } } else if(b.isRolloverEnabled() && model.isRollover()) { icon = (Icon) b.getRolloverIcon(); } if(icon == null) { icon = (Icon) b.getIcon(); } icon.paintIcon(b, g, iconRect.x, iconRect.y); } /** * Overriden so that the text will not be rendered as shifted for * Toggle buttons and subclasses. */ protected int getTextShiftOffset() { return 0; } }