1 7 8 package javax.swing.plaf.basic; 9 10 import com.sun.java.swing.SwingUtilities2; 11 import java.awt.*; 12 import java.awt.event.*; 13 import java.io.Serializable ; 14 import javax.swing.*; 15 import javax.swing.border.*; 16 import java.awt.*; 17 import java.awt.event.*; 18 import javax.swing.plaf.ButtonUI ; 19 import javax.swing.plaf.UIResource ; 20 import javax.swing.plaf.ComponentUI ; 21 import javax.swing.text.View ; 22 23 29 public class BasicButtonUI extends ButtonUI { 30 private final static BasicButtonUI buttonUI = new BasicButtonUI (); 32 33 protected int defaultTextIconGap; 37 38 private int shiftOffset = 0; 41 protected int defaultTextShiftOffset; 44 45 private final static String propertyPrefix = "Button" + "."; 46 47 public static ComponentUI createUI(JComponent c) { 51 return buttonUI; 52 } 53 54 protected String getPropertyPrefix() { 55 return propertyPrefix; 56 } 57 58 59 public void installUI(JComponent c) { 63 installDefaults((AbstractButton) c); 64 installListeners((AbstractButton) c); 65 installKeyboardActions((AbstractButton) c); 66 BasicHTML.updateRenderer(c, ((AbstractButton) c).getText()); 67 } 68 69 protected void installDefaults(AbstractButton b) { 70 String pp = getPropertyPrefix(); 72 73 defaultTextShiftOffset = UIManager.getInt(pp + "textShiftOffset"); 74 75 if (b.isContentAreaFilled()) { 77 LookAndFeel.installProperty(b, "opaque", Boolean.TRUE); 78 } else { 79 LookAndFeel.installProperty(b, "opaque", Boolean.FALSE); 80 } 81 82 if(b.getMargin() == null || (b.getMargin() instanceof UIResource )) { 83 b.setMargin(UIManager.getInsets(pp + "margin")); 84 } 85 86 LookAndFeel.installColorsAndFont(b, pp + "background", 87 pp + "foreground", pp + "font"); 88 LookAndFeel.installBorder(b, pp + "border"); 89 90 Object rollover = UIManager.get(pp + "rollover"); 91 if (rollover != null) { 92 LookAndFeel.installProperty(b, "rolloverEnabled", rollover); 93 } 94 } 95 96 protected void installListeners(AbstractButton b) { 97 BasicButtonListener listener = createButtonListener(b); 98 if(listener != null) { 99 b.addMouseListener(listener); 100 b.addMouseMotionListener(listener); 101 b.addFocusListener(listener); 102 b.addPropertyChangeListener(listener); 103 b.addChangeListener(listener); 104 } 105 } 106 107 protected void installKeyboardActions(AbstractButton b){ 108 BasicButtonListener listener = getButtonListener(b); 109 110 if(listener != null) { 111 listener.installKeyboardActions(b); 112 } 113 } 114 115 116 public void uninstallUI(JComponent c) { 120 uninstallKeyboardActions((AbstractButton) c); 121 uninstallListeners((AbstractButton) c); 122 uninstallDefaults((AbstractButton) c); 123 BasicHTML.updateRenderer(c, ""); 124 } 125 126 protected void uninstallKeyboardActions(AbstractButton b) { 127 BasicButtonListener listener = getButtonListener(b); 128 if(listener != null) { 129 listener.uninstallKeyboardActions(b); 130 } 131 } 132 133 protected void uninstallListeners(AbstractButton b) { 134 BasicButtonListener listener = getButtonListener(b); 135 if(listener != null) { 136 b.removeMouseListener(listener); 137 b.removeMouseListener(listener); 138 b.removeMouseMotionListener(listener); 139 b.removeFocusListener(listener); 140 b.removeChangeListener(listener); 141 b.removePropertyChangeListener(listener); 142 } 143 } 144 145 protected void uninstallDefaults(AbstractButton b) { 146 LookAndFeel.uninstallBorder(b); 147 } 148 149 protected BasicButtonListener createButtonListener(AbstractButton b) { 153 return new BasicButtonListener (b); 154 } 155 156 public int getDefaultTextIconGap(AbstractButton b) { 157 return defaultTextIconGap; 158 } 159 160 165 private static Rectangle viewRect = new Rectangle(); 166 private static Rectangle textRect = new Rectangle(); 167 private static Rectangle iconRect = new Rectangle(); 168 169 173 public void paint(Graphics g, JComponent c) 174 { 175 AbstractButton b = (AbstractButton) c; 176 ButtonModel model = b.getModel(); 177 178 FontMetrics fm = SwingUtilities2.getFontMetrics(b, g); 179 180 Insets i = c.getInsets(); 181 182 viewRect.x = i.left; 183 viewRect.y = i.top; 184 viewRect.width = b.getWidth() - (i.right + viewRect.x); 185 viewRect.height = b.getHeight() - (i.bottom + viewRect.y); 186 187 textRect.x = textRect.y = textRect.width = textRect.height = 0; 188 iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; 189 190 Font f = c.getFont(); 191 g.setFont(f); 192 193 String text = SwingUtilities.layoutCompoundLabel( 195 c, fm, b.getText(), b.getIcon(), 196 b.getVerticalAlignment(), b.getHorizontalAlignment(), 197 b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 198 viewRect, iconRect, textRect, 199 b.getText() == null ? 0 : b.getIconTextGap()); 200 201 clearTextShiftOffset(); 202 203 if (model.isArmed() && model.isPressed()) { 205 paintButtonPressed(g,b); 206 } 207 208 if(b.getIcon() != null) { 210 paintIcon(g,c,iconRect); 211 } 212 213 if (text != null && !text.equals("")){ 214 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 215 if (v != null) { 216 v.paint(g, textRect); 217 } else { 218 paintText(g, b, textRect, text); 219 } 220 } 221 222 if (b.isFocusPainted() && b.hasFocus()) { 223 paintFocus(g,b,viewRect,textRect,iconRect); 225 } 226 } 227 228 protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){ 229 AbstractButton b = (AbstractButton) c; 230 ButtonModel model = b.getModel(); 231 Icon icon = b.getIcon(); 232 Icon tmpIcon = null; 233 234 if(icon == null) { 235 return; 236 } 237 238 if(!model.isEnabled()) { 239 if(model.isSelected()) { 240 tmpIcon = (Icon) b.getDisabledSelectedIcon(); 241 } else { 242 tmpIcon = (Icon) b.getDisabledIcon(); 243 } 244 } else if(model.isPressed() && model.isArmed()) { 245 tmpIcon = (Icon) b.getPressedIcon(); 246 if(tmpIcon != null) { 247 clearTextShiftOffset(); 249 } 250 } else if(b.isRolloverEnabled() && model.isRollover()) { 251 if(model.isSelected()) { 252 tmpIcon = (Icon) b.getRolloverSelectedIcon(); 253 } else { 254 tmpIcon = (Icon) b.getRolloverIcon(); 255 } 256 } else if(model.isSelected()) { 257 tmpIcon = (Icon) b.getSelectedIcon(); 258 } 259 260 if(tmpIcon != null) { 261 icon = tmpIcon; 262 } 263 264 if(model.isPressed() && model.isArmed()) { 265 icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(), 266 iconRect.y + getTextShiftOffset()); 267 } else { 268 icon.paintIcon(c, g, iconRect.x, iconRect.y); 269 } 270 271 } 272 273 277 protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) { 278 AbstractButton b = (AbstractButton) c; 279 ButtonModel model = b.getModel(); 280 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g); 281 int mnemonicIndex = b.getDisplayedMnemonicIndex(); 282 283 284 if(model.isEnabled()) { 285 286 g.setColor(b.getForeground()); 287 SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex, 288 textRect.x + getTextShiftOffset(), 289 textRect.y + fm.getAscent() + getTextShiftOffset()); 290 } 291 else { 292 293 g.setColor(b.getBackground().brighter()); 294 SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex, 295 textRect.x, textRect.y + fm.getAscent()); 296 g.setColor(b.getBackground().darker()); 297 SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex, 298 textRect.x - 1, textRect.y + fm.getAscent() - 1); 299 } 300 } 301 302 311 protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) { 312 paintText(g, (JComponent)b, textRect, text); 313 } 314 315 protected void paintFocus(Graphics g, AbstractButton b, 318 Rectangle viewRect, Rectangle textRect, Rectangle iconRect){ 319 } 320 321 322 323 protected void paintButtonPressed(Graphics g, AbstractButton b){ 324 } 325 326 protected void clearTextShiftOffset(){ 327 this.shiftOffset = 0; 328 } 329 330 protected void setTextShiftOffset(){ 331 this.shiftOffset = defaultTextShiftOffset; 332 } 333 334 protected int getTextShiftOffset() { 335 return shiftOffset; 336 } 337 338 public Dimension getMinimumSize(JComponent c) { 342 Dimension d = getPreferredSize(c); 343 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 344 if (v != null) { 345 d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); 346 } 347 return d; 348 } 349 350 public Dimension getPreferredSize(JComponent c) { 351 AbstractButton b = (AbstractButton)c; 352 return BasicGraphicsUtils.getPreferredButtonSize(b, b.getIconTextGap()); 353 } 354 355 public Dimension getMaximumSize(JComponent c) { 356 Dimension d = getPreferredSize(c); 357 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 358 if (v != null) { 359 d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); 360 } 361 return d; 362 } 363 364 368 private BasicButtonListener getButtonListener(AbstractButton b) { 369 MouseMotionListener[] listeners = b.getMouseMotionListeners(); 370 371 if (listeners != null) { 372 for (int counter = 0; counter < listeners.length; counter++) { 373 if (listeners[counter] instanceof BasicButtonListener ) { 374 return (BasicButtonListener )listeners[counter]; 375 } 376 } 377 } 378 return null; 379 } 380 381 } 382 | Popular Tags |