1 7 8 package javax.swing.plaf.basic; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 13 import javax.swing.*; 14 import javax.swing.border.*; 15 import javax.swing.plaf.*; 16 import javax.swing.text.View ; 17 18 19 20 27 public class BasicToggleButtonUI extends BasicButtonUI { 28 29 private final static BasicToggleButtonUI toggleButtonUI = new BasicToggleButtonUI (); 30 31 private final static String propertyPrefix = "ToggleButton" + "."; 32 33 public static ComponentUI createUI(JComponent b) { 37 return toggleButtonUI; 38 } 39 40 protected String getPropertyPrefix() { 41 return propertyPrefix; 42 } 43 44 45 public void paint(Graphics g, JComponent c) { 49 AbstractButton b = (AbstractButton) c; 50 ButtonModel model = b.getModel(); 51 52 Dimension size = b.getSize(); 53 FontMetrics fm = g.getFontMetrics(); 54 55 Insets i = c.getInsets(); 56 57 Rectangle viewRect = new Rectangle(size); 58 59 viewRect.x += i.left; 60 viewRect.y += i.top; 61 viewRect.width -= (i.right + viewRect.x); 62 viewRect.height -= (i.bottom + viewRect.y); 63 64 Rectangle iconRect = new Rectangle(); 65 Rectangle textRect = new Rectangle(); 66 67 Font f = c.getFont(); 68 g.setFont(f); 69 70 String text = SwingUtilities.layoutCompoundLabel( 72 c, fm, b.getText(), b.getIcon(), 73 b.getVerticalAlignment(), b.getHorizontalAlignment(), 74 b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 75 viewRect, iconRect, textRect, 76 b.getText() == null ? 0 : b.getIconTextGap()); 77 78 g.setColor(b.getBackground()); 79 80 if (model.isArmed() && model.isPressed() || model.isSelected()) { 81 paintButtonPressed(g,b); 82 } 83 84 if(b.getIcon() != null) { 86 paintIcon(g, b, iconRect); 87 } 88 89 if(text != null && !text.equals("")) { 91 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 92 if (v != null) { 93 v.paint(g, textRect); 94 } else { 95 paintText(g, b, textRect, text); 96 } 97 } 98 99 if (b.isFocusPainted() && b.hasFocus()) { 101 paintFocus(g, b, viewRect, textRect, iconRect); 102 } 103 } 104 105 protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) { 106 ButtonModel model = b.getModel(); 107 Icon icon = null; 108 109 if(!model.isEnabled()) { 110 if(model.isSelected()) { 111 icon = (Icon) b.getDisabledSelectedIcon(); 112 } else { 113 icon = (Icon) b.getDisabledIcon(); 114 } 115 } else if(model.isPressed() && model.isArmed()) { 116 icon = (Icon) b.getPressedIcon(); 117 if(icon == null) { 118 icon = (Icon) b.getSelectedIcon(); 120 } 121 } else if(model.isSelected()) { 122 if(b.isRolloverEnabled() && model.isRollover()) { 123 icon = (Icon) b.getRolloverSelectedIcon(); 124 if (icon == null) { 125 icon = (Icon) b.getSelectedIcon(); 126 } 127 } else { 128 icon = (Icon) b.getSelectedIcon(); 129 } 130 } else if(b.isRolloverEnabled() && model.isRollover()) { 131 icon = (Icon) b.getRolloverIcon(); 132 } 133 134 if(icon == null) { 135 icon = (Icon) b.getIcon(); 136 } 137 138 icon.paintIcon(b, g, iconRect.x, iconRect.y); 139 } 140 141 145 protected int getTextShiftOffset() { 146 return 0; 147 } 148 149 } 150 | Popular Tags |