| 1 |
/* |
| 2 |
* @(#)JideButtonUI.java Nov 20, 2002 |
| 3 |
* |
| 4 |
* Copyright 2002 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.plaf.basic; |
| 8 |
|
| 9 |
import com.jidesoft.icons.IconsFactory; |
| 10 |
import com.jidesoft.plaf.JideButtonUI; |
| 11 |
import com.jidesoft.plaf.LookAndFeelFactory; |
| 12 |
import com.jidesoft.plaf.UIDefaultsLookup; |
| 13 |
import com.jidesoft.swing.ComponentStateSupport; |
| 14 |
import com.jidesoft.swing.JideButton; |
| 15 |
import com.jidesoft.swing.JideSwingUtilities; |
| 16 |
import com.jidesoft.utils.SecurityUtils; |
| 17 |
|
| 18 |
import javax.swing.*; |
| 19 |
import javax.swing.plaf.ComponentUI; |
| 20 |
import javax.swing.plaf.InsetsUIResource; |
| 21 |
import javax.swing.plaf.UIResource; |
| 22 |
import javax.swing.plaf.basic.BasicButtonListener; |
| 23 |
import javax.swing.plaf.basic.BasicGraphicsUtils; |
| 24 |
import javax.swing.plaf.basic.BasicHTML; |
| 25 |
import javax.swing.plaf.synth.Region; |
| 26 |
import javax.swing.plaf.synth.SynthContext; |
| 27 |
import javax.swing.plaf.synth.SynthLookAndFeel; |
| 28 |
import javax.swing.plaf.synth.SynthStyle; |
| 29 |
import javax.swing.text.View; |
| 30 |
import java.awt.*; |
| 31 |
|
| 32 |
/** |
| 33 |
* JideButtonUI implementation |
| 34 |
*/ |
| 35 |
public class BasicJideButtonUI extends JideButtonUI { |
| 36 |
// Visual constants |
| 37 |
protected int defaultTextIconGap; |
| 38 |
|
| 39 |
// Offset controlled by set method |
| 40 |
private int shiftOffset = 0; |
| 41 |
protected int defaultTextShiftOffset; |
| 42 |
|
| 43 |
// Has the shared instance defaults been initialized? |
| 44 |
private boolean defaults_initialized = false; |
| 45 |
|
| 46 |
private static final String propertyPrefix = "JideButton" + "."; |
| 47 |
|
| 48 |
protected ThemePainter _painter; |
| 49 |
|
| 50 |
protected Color _shadowColor; |
| 51 |
protected Color _darkShadowColor; |
| 52 |
protected Color _highlight; |
| 53 |
protected Color _lightHighlightColor; |
| 54 |
|
| 55 |
protected Color _focusColor; |
| 56 |
|
| 57 |
protected boolean _isFloatingIcon = false; |
| 58 |
|
| 59 |
// ******************************** |
| 60 |
// Create PLAF |
| 61 |
// ******************************** |
| 62 |
|
| 63 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 64 |
public static ComponentUI createUI(JComponent c) { |
| 65 |
return new BasicJideButtonUI(); |
| 66 |
} |
| 67 |
|
| 68 |
protected String getPropertyPrefix() { |
| 69 |
return propertyPrefix; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
// ******************************** |
| 74 |
// Install PLAF |
| 75 |
// ******************************** |
| 76 |
|
| 77 |
@Override |
| 78 |
public void installUI(JComponent c) { |
| 79 |
installDefaults((AbstractButton) c); |
| 80 |
installListeners((AbstractButton) c); |
| 81 |
installKeyboardActions((AbstractButton) c); |
| 82 |
BasicHTML.updateRenderer(c, ((AbstractButton) c).getText()); |
| 83 |
} |
| 84 |
|
| 85 |
protected void installDefaults(AbstractButton b) { |
| 86 |
// load shared instance defaults |
| 87 |
Object o = UIDefaultsLookup.get("Theme.painter"); |
| 88 |
if (o instanceof ThemePainter) { |
| 89 |
_painter = (ThemePainter) o; |
| 90 |
} |
| 91 |
else { |
| 92 |
_painter = new BasicPainter(); |
| 93 |
} |
| 94 |
|
| 95 |
String pp = getPropertyPrefix(); |
| 96 |
if (!defaults_initialized) { |
| 97 |
defaultTextIconGap = UIDefaultsLookup.getInt(pp + "textIconGap"); |
| 98 |
defaultTextShiftOffset = UIDefaultsLookup.getInt(pp + "textShiftOffset"); |
| 99 |
|
| 100 |
_focusColor = UIDefaultsLookup.getColor("Button.focus"); // use Button.focus since we didn't install JideButton.focus. |
| 101 |
|
| 102 |
// next four lines part of optimized component defaults installation |
| 103 |
/* defaultForeground = UIManagerLookup.getColor(pp + "foreground"); |
| 104 |
defaultBackground = UIManagerLookup.getColor(pp + "background"); |
| 105 |
defaultFont = UIManagerLookup.getFont(pp + "font"); |
| 106 |
defaultBorder = UIManagerLookup.getBorder(pp + "border");*/ |
| 107 |
|
| 108 |
_shadowColor = UIDefaultsLookup.getColor("JideButton.shadow"); |
| 109 |
_darkShadowColor = UIDefaultsLookup.getColor("JideButton.darkShadow"); |
| 110 |
_highlight = UIDefaultsLookup.getColor("JideButton.highlight"); |
| 111 |
_lightHighlightColor = UIDefaultsLookup.getColor("JideButton.light"); |
| 112 |
|
| 113 |
defaults_initialized = true; |
| 114 |
} |
| 115 |
|
| 116 |
// // set the following defaults on the button |
| 117 |
// if (b.isContentAreaFilled()) { |
| 118 |
// b.setOpaque(true); |
| 119 |
// } |
| 120 |
// else { |
| 121 |
// b.setOpaque(false); |
| 122 |
// } |
| 123 |
|
| 124 |
updateMargin(b); |
| 125 |
|
| 126 |
// *** begin optimized defaults install *** |
| 127 |
|
| 128 |
/* Color currentForeground = b.getForeground(); |
| 129 |
Color currentBackground = b.getBackground(); |
| 130 |
Font currentFont = b.getFont(); |
| 131 |
Border currentBorder = b.getBorder(); |
| 132 |
|
| 133 |
if (currentForeground == null || currentForeground instanceof UIResource) { |
| 134 |
b.setForeground(defaultForeground); |
| 135 |
} |
| 136 |
|
| 137 |
if (currentBackground == null || currentBackground instanceof UIResource) { |
| 138 |
b.setBackground(defaultBackground); |
| 139 |
} |
| 140 |
|
| 141 |
if (currentFont == null || currentFont instanceof UIResource) { |
| 142 |
b.setFont(defaultFont); |
| 143 |
} |
| 144 |
|
| 145 |
if (currentBorder == null || currentBorder instanceof UIResource) { |
| 146 |
b.setBorder(defaultBorder); |
| 147 |
} */ |
| 148 |
|
| 149 |
// *** end optimized defaults install *** |
| 150 |
|
| 151 |
// old code below works for component defaults installation, but it is slow |
| 152 |
LookAndFeel.installColorsAndFont(b, pp + "background", pp + "foreground", pp + "font"); |
| 153 |
LookAndFeel.installBorder(b, pp + "border"); |
| 154 |
|
| 155 |
_isFloatingIcon = UIDefaultsLookup.getBoolean("Icon.floating"); |
| 156 |
} |
| 157 |
|
| 158 |
protected void installListeners(AbstractButton b) { |
| 159 |
BasicButtonListener listener = createButtonListener(b); |
| 160 |
if (listener != null) { |
| 161 |
// put the listener in the button's client properties so that |
| 162 |
// we can get at it later |
| 163 |
b.putClientProperty(this, listener); |
| 164 |
|
| 165 |
b.addMouseListener(listener); |
| 166 |
b.addMouseMotionListener(listener); |
| 167 |
b.addFocusListener(listener); |
| 168 |
b.addPropertyChangeListener(listener); |
| 169 |
b.addChangeListener(listener); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
protected void installKeyboardActions(AbstractButton b) { |
| 174 |
BasicButtonListener listener = (BasicButtonListener) b.getClientProperty(this); |
| 175 |
if (listener != null) { |
| 176 |
listener.installKeyboardActions(b); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
// ******************************** |
| 182 |
// Uninstall PLAF |
| 183 |
// ******************************** |
| 184 |
|
| 185 |
@Override |
| 186 |
public void uninstallUI(JComponent c) { |
| 187 |
uninstallKeyboardActions((AbstractButton) c); |
| 188 |
uninstallListeners((AbstractButton) c); |
| 189 |
uninstallDefaults((AbstractButton) c); |
| 190 |
BasicHTML.updateRenderer(c, ""); |
| 191 |
} |
| 192 |
|
| 193 |
protected void uninstallKeyboardActions(AbstractButton b) { |
| 194 |
BasicButtonListener listener = (BasicButtonListener) b.getClientProperty(this); |
| 195 |
if (listener != null) { |
| 196 |
listener.uninstallKeyboardActions(b); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
protected void uninstallListeners(AbstractButton b) { |
| 201 |
BasicButtonListener listener = (BasicButtonListener) b.getClientProperty(this); |
| 202 |
b.putClientProperty(this, null); |
| 203 |
if (listener != null) { |
| 204 |
b.removeMouseListener(listener); |
| 205 |
b.removeMouseMotionListener(listener); |
| 206 |
b.removeFocusListener(listener); |
| 207 |
b.removeChangeListener(listener); |
| 208 |
b.removePropertyChangeListener(listener); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 213 |
protected void uninstallDefaults(AbstractButton b) { |
| 214 |
_painter = null; |
| 215 |
_focusColor = null; |
| 216 |
|
| 217 |
|
| 218 |
_shadowColor = null; |
| 219 |
_highlight = null; |
| 220 |
_lightHighlightColor = null; |
| 221 |
_darkShadowColor = null; |
| 222 |
defaults_initialized = false; |
| 223 |
} |
| 224 |
|
| 225 |
// ******************************** |
| 226 |
// Create Listeners |
| 227 |
// ******************************** |
| 228 |
|
| 229 |
protected BasicButtonListener createButtonListener(AbstractButton b) { |
| 230 |
return new BasicJideButtonListener(b); |
| 231 |
} |
| 232 |
|
| 233 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 234 |
public int getDefaultTextIconGap(AbstractButton b) { |
| 235 |
return defaultTextIconGap; |
| 236 |
} |
| 237 |
|
| 238 |
protected Color getFocusColor() { |
| 239 |
return _focusColor; |
| 240 |
} |
| 241 |
|
| 242 |
/* These rectangles/insets are allocated once for all |
| 243 |
* ButtonUI.paint() calls. Re-using rectangles rather than |
| 244 |
* allocating them in each paint call substantially reduced the time |
| 245 |
* it took paint to run. Obviously, this method can't be re-entered. |
| 246 |
*/ |
| 247 |
protected static Rectangle viewRect = new Rectangle(); |
| 248 |
protected static Rectangle textRect = new Rectangle(); |
| 249 |
protected static Rectangle iconRect = new Rectangle(); |
| 250 |
|
| 251 |
// ******************************** |
| 252 |
// Paint Methods |
| 253 |
// ******************************** |
| 254 |
|
| 255 |
@Override |
| 256 |
public void paint(Graphics g, JComponent c) { |
| 257 |
AbstractButton b = (AbstractButton) c; |
| 258 |
boolean isHorizontal = true; |
| 259 |
|
| 260 |
if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.VERTICAL) { |
| 261 |
isHorizontal = false; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
FontMetrics fm = c.getFontMetrics(c.getFont()); |
| 266 |
|
| 267 |
Insets i = c.getInsets(); |
| 268 |
|
| 269 |
viewRect.x = i.left; |
| 270 |
viewRect.y = i.top; |
| 271 |
viewRect.width = b.getWidth() - (i.right + viewRect.x); |
| 272 |
viewRect.height = b.getHeight() - (i.bottom + viewRect.y); |
| 273 |
|
| 274 |
textRect.x = textRect.y = textRect.width = textRect.height = 0; |
| 275 |
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; |
| 276 |
|
| 277 |
paintBackground(g, b); |
| 278 |
|
| 279 |
Font f = c.getFont(); |
| 280 |
g.setFont(f); |
| 281 |
|
| 282 |
// layout the text and icon |
| 283 |
String text = JideSwingUtilities.layoutCompoundLabel(c, fm, b.getText(), b.getIcon(), |
| 284 |
isHorizontal, |
| 285 |
b.getVerticalAlignment(), b.getHorizontalAlignment(), |
| 286 |
b.getVerticalTextPosition(), b.getHorizontalTextPosition(), |
| 287 |
viewRect, iconRect, textRect, |
| 288 |
// JDK PORTING HINT |
| 289 |
// JDK1.3: getIconTextGap, use defaultTextIconGap |
| 290 |
b.getText() == null ? 0 : b.getIconTextGap()); // use the bigger one of both gaps. Not really the best way. |
| 291 |
|
| 292 |
clearTextShiftOffset(); |
| 293 |
|
| 294 |
paintIcon(b, g); |
| 295 |
|
| 296 |
Boolean highContrast = UIManager.getBoolean("Theme.highContrast"); |
| 297 |
if (highContrast && JideSwingUtilities.getButtonState(b) == ThemePainter.STATE_PRESSED) { |
| 298 |
textRect.x += 1; |
| 299 |
textRect.y += 1; |
| 300 |
} |
| 301 |
if (text != null && !text.equals("")) { |
| 302 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
| 303 |
if (v != null) { |
| 304 |
v.paint(g, textRect); |
| 305 |
} |
| 306 |
else { |
| 307 |
paintText(g, b, textRect, text); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
protected void paintIcon(AbstractButton b, Graphics g) { |
| 313 |
// Paint the Icon |
| 314 |
if (b.getIcon() != null) { |
| 315 |
Icon icon = getIcon(b); |
| 316 |
|
| 317 |
ButtonModel model = b.getModel(); |
| 318 |
if (icon != null) { |
| 319 |
if (isFloatingIcon() && model.isEnabled()) { |
| 320 |
if (model.isRollover() && !model.isPressed() && !model.isSelected()) { |
| 321 |
if (!"true".equals(SecurityUtils.getProperty("shadingtheme", "false")) && b instanceof JideButton && ((JideButton) b).getButtonStyle() == JideButton.TOOLBAR_STYLE) { |
| 322 |
if (icon instanceof ImageIcon) { |
| 323 |
ImageIcon shadow = IconsFactory.createGrayImage(((ImageIcon) icon).getImage()); |
| 324 |
shadow.paintIcon(b, g, iconRect.x + 1, iconRect.y + 1); |
| 325 |
} |
| 326 |
else { |
| 327 |
ImageIcon shadow = IconsFactory.createGrayImage(b, icon); |
| 328 |
shadow.paintIcon(b, g, iconRect.x + 1, iconRect.y + 1); |
| 329 |
} |
| 330 |
icon.paintIcon(b, g, iconRect.x - 1, iconRect.y - 1); |
| 331 |
} |
| 332 |
else { |
| 333 |
icon.paintIcon(b, g, iconRect.x, iconRect.y); |
| 334 |
} |
| 335 |
} |
| 336 |
else { |
| 337 |
icon.paintIcon(b, g, iconRect.x, iconRect.y); |
| 338 |
} |
| 339 |
} |
| 340 |
else { |
| 341 |
icon.paintIcon(b, g, iconRect.x, iconRect.y); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
protected Icon getIcon(AbstractButton b) { |
| 348 |
ButtonModel model = b.getModel(); |
| 349 |
Icon icon = b.getIcon(); |
| 350 |
Icon tmpIcon = null; |
| 351 |
if (!model.isEnabled()) { |
| 352 |
if (model.isSelected()) { |
| 353 |
tmpIcon = b.getDisabledSelectedIcon(); |
| 354 |
} |
| 355 |
else { |
| 356 |
tmpIcon = b.getDisabledIcon(); |
| 357 |
} |
| 358 |
|
| 359 |
// create default disabled icon |
| 360 |
if (tmpIcon == null) { |
| 361 |
if (icon instanceof ImageIcon) { |
| 362 |
icon = IconsFactory.createGrayImage(((ImageIcon) icon).getImage()); |
| 363 |
} |
| 364 |
else { |
| 365 |
icon = IconsFactory.createGrayImage(b, icon); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
else if (model.isPressed() && model.isArmed()) { |
| 370 |
tmpIcon = b.getPressedIcon(); |
| 371 |
if (tmpIcon != null) { |
| 372 |
// revert back to 0 offset |
| 373 |
clearTextShiftOffset(); |
| 374 |
} |
| 375 |
} |
| 376 |
else if (b.isRolloverEnabled() && model.isRollover()) { |
| 377 |
if (model.isSelected()) { |
| 378 |
tmpIcon = b.getRolloverSelectedIcon(); |
| 379 |
} |
| 380 |
else { |
| 381 |
tmpIcon = b.getRolloverIcon(); |
| 382 |
} |
| 383 |
} |
| 384 |
else if (model.isSelected()) { |
| 385 |
tmpIcon = b.getSelectedIcon(); |
| 386 |
} |
| 387 |
|
| 388 |
if (tmpIcon != null) { |
| 389 |
icon = tmpIcon; |
| 390 |
} |
| 391 |
return icon; |
| 392 |
} |
| 393 |
|
| 394 |
protected boolean isFloatingIcon() { |
| 395 |
return _isFloatingIcon; |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* As of Java 2 platform v 1.4 this method should not be used or overridden. Use the paintText method which takes |
| 400 |
* the AbstractButton argument. |
| 401 |
* |
| 402 |
* @param g the Graphics instance |
| 403 |
* @param c the component |
| 404 |
* @param textRect the rectangle of the text |
| 405 |
* @param text the text to paint |
| 406 |
*/ |
| 407 |
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) { |
| 408 |
AbstractButton b = (AbstractButton) c; |
| 409 |
boolean isHorizontal = true; |
| 410 |
if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.VERTICAL) { |
| 411 |
isHorizontal = false; |
| 412 |
} |
| 413 |
|
| 414 |
ButtonModel model = b.getModel(); |
| 415 |
FontMetrics fm = c.getFontMetrics(c.getFont()); |
| 416 |
// JDK PORTING HINT |
| 417 |
// JDK1.3: No getDisplayedMnemonicIndex, use getMnemonic |
| 418 |
int mnemonicIndex = b.getDisplayedMnemonicIndex(); |
| 419 |
|
| 420 |
if (!isHorizontal) { |
| 421 |
Graphics2D g2d = (Graphics2D) g.create(); |
| 422 |
g2d.rotate(Math.PI / 2); |
| 423 |
g2d.translate(0, -c.getWidth() + 1); |
| 424 |
|
| 425 |
/* Draw the Text */ |
| 426 |
if (model.isEnabled()) { |
| 427 |
/*** paint the text normally */ |
| 428 |
g2d.setColor(getForegroundOfState(b)); |
| 429 |
|
| 430 |
// JDK PORTING HINT |
| 431 |
// JDK1.3: No drawStringUnderlineCharAt, draw the string then draw the underline |
| 432 |
JideSwingUtilities.drawStringUnderlineCharAt(b, g2d, text, mnemonicIndex, |
| 433 |
textRect.y + getTextShiftOffset(), |
| 434 |
textRect.x + fm.getAscent() + getTextShiftOffset()); |
| 435 |
} |
| 436 |
else { |
| 437 |
/*** paint the text disabled ***/ |
| 438 |
/*** paint the text disabled ***/ |
| 439 |
Color color = UIDefaultsLookup.getColor("Button.disabledForeground"); |
| 440 |
g2d.setColor(color == null ? b.getBackground().darker() : color); |
| 441 |
|
| 442 |
// JDK PORTING HINT |
| 443 |
// JDK1.3: No drawStringUnderlineCharAt, draw the string then draw the underline |
| 444 |
JideSwingUtilities.drawStringUnderlineCharAt(b, g2d, text, mnemonicIndex, |
| 445 |
textRect.y, textRect.x + fm.getAscent()); |
| 446 |
} |
| 447 |
if (b instanceof JideButton && ((JideButton) b).getButtonStyle() == JideButton.HYPERLINK_STYLE |
| 448 |
&& (((JideButton) b).isAlwaysShowHyperlink() || b.getModel().isRollover())) { |
| 449 |
g.drawLine(textRect.x, textRect.y, textRect.x, textRect.y + textRect.height); |
| 450 |
} |
| 451 |
|
| 452 |
g2d.dispose(); |
| 453 |
} |
| 454 |
else { |
| 455 |
/* Draw the Text */ |
| 456 |
Color old = g.getColor(); |
| 457 |
if (model.isEnabled()) { |
| 458 |
/*** paint the text normally */ |
| 459 |
g.setColor(getForegroundOfState(b)); |
| 460 |
|
| 461 |
// JDK PORTING HINT |
| 462 |
// JDK1.3: No drawStringUnderlineCharAt, draw the string then draw the underline |
| 463 |
JideSwingUtilities.drawStringUnderlineCharAt(b, g, text, mnemonicIndex, |
| 464 |
textRect.x + getTextShiftOffset(), |
| 465 |
textRect.y + fm.getAscent() + getTextShiftOffset()); |
| 466 |
} |
| 467 |
else { |
| 468 |
/*** paint the text disabled ***/ |
| 469 |
Color color = UIDefaultsLookup.getColor("Button.disabledForeground"); |
| 470 |
g.setColor(color == null ? b.getBackground().darker() : color); |
| 471 |
|
| 472 |
// JDK PORTING HINT |
| 473 |
// JDK1.3: No drawStringUnderlineCharAt, draw the string then draw the underline |
| 474 |
JideSwingUtilities.drawStringUnderlineCharAt(b, g, text, mnemonicIndex, |
| 475 |
textRect.x, textRect.y + fm.getAscent()); |
| 476 |
} |
| 477 |
if (b instanceof JideButton && ((JideButton) b).getButtonStyle() == JideButton.HYPERLINK_STYLE |
| 478 |
&& (((JideButton) b).isAlwaysShowHyperlink() || b.getModel().isRollover())) { |
| 479 |
g.drawLine(textRect.x, textRect.y + textRect.height - 2, textRect.x + textRect.width, textRect.y + textRect.height - 2); |
| 480 |
} |
| 481 |
g.setColor(old); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
protected Color getForegroundOfState(AbstractButton b) { |
| 486 |
int state = JideSwingUtilities.getButtonState(b); |
| 487 |
Color foreground = null; |
| 488 |
if (b instanceof ComponentStateSupport) { |
| 489 |
foreground = ((ComponentStateSupport) b).getForegroundOfState(state); |
| 490 |
} |
| 491 |
if (foreground == null || foreground instanceof UIResource) { |
| 492 |
foreground = b.getForeground(); |
| 493 |
} |
| 494 |
return foreground; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Method which renders the text of the current button. |
| 499 |
* <p/> |
| 500 |
* |
| 501 |
* @param g Graphics context |
| 502 |
* @param b Current button to render |
| 503 |
* @param textRect Bounding rectangle to render the text. |
| 504 |
* @param text String to render |
| 505 |
* |
| 506 |
* @since 1.4 |
| 507 |
*/ |
| 508 |
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) { |
| 509 |
paintText(g, (JComponent) b, textRect, text); |
| 510 |
} |
| 511 |
|
| 512 |
protected void paintBackground(Graphics g, AbstractButton b) { |
| 513 |
boolean paintDefaultBorder = true; |
| 514 |
boolean paintBackground; |
| 515 |
Object o = b.getClientProperty("JideButton.paintDefaultBorder"); |
| 516 |
if (o instanceof Boolean) { |
| 517 |
paintDefaultBorder = (Boolean) o; |
| 518 |
} |
| 519 |
o = b.getClientProperty("JideButton.alwaysPaintBackground"); |
| 520 |
if (o instanceof Boolean) { |
| 521 |
paintBackground = (Boolean) o; |
| 522 |
} |
| 523 |
else { |
| 524 |
paintBackground = b.isOpaque(); |
| 525 |
} |
| 526 |
|
| 527 |
if (paintBackground) { |
| 528 |
g.setColor(b.getBackground()); |
| 529 |
g.fillRect(0, 0, b.getWidth(), b.getHeight()); |
| 530 |
} |
| 531 |
|
| 532 |
if (b.isContentAreaFilled()) { |
| 533 |
if (b instanceof JideButton && ((JideButton) b).getButtonStyle() == JideButton.TOOLBAR_STYLE) { |
| 534 |
Rectangle rect = new Rectangle(0, 0, b.getWidth(), b.getHeight()); |
| 535 |
int state = JideSwingUtilities.getButtonState(b); |
| 536 |
if (state != ThemePainter.STATE_DEFAULT) { |
| 537 |
getPainter().paintButtonBackground(b, g, rect, 0, state); |
| 538 |
} |
| 539 |
else { |
| 540 |
if (paintBackground) { |
| 541 |
getPainter().paintButtonBackground(b, g, rect, JideSwingUtilities.getOrientationOf(b), state); |
| 542 |
if ("true".equals(SecurityUtils.getProperty("shadingtheme", "false"))) { |
| 543 |
JideSwingUtilities.fillGradient(g, rect, JideSwingUtilities.getOrientationOf(b)); |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
else if (b instanceof JideButton && ((JideButton) b).getButtonStyle() == JideButton.FLAT_STYLE) { |
| 549 |
paintBackgroundInFlatStyle(g, b, paintBackground); |
| 550 |
} |
| 551 |
else if (b instanceof JideButton && ((JideButton) b).getButtonStyle() == JideButton.TOOLBOX_STYLE) { |
| 552 |
paintBackgroundInToolboxStyle(g, b, paintBackground, paintDefaultBorder); |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
private void paintBackgroundInFlatStyle(Graphics g, AbstractButton b, boolean paintBackground) { |
| 558 |
Rectangle rect = new Rectangle(0, 0, b.getWidth(), b.getHeight()); |
| 559 |
int state = JideSwingUtilities.getButtonState(b); |
| 560 |
switch (state) { |
| 561 |
case ThemePainter.STATE_SELECTED: |
| 562 |
JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight); |
| 563 |
g.setColor(_shadowColor); // inner 3D border |
| 564 |
g.drawLine(rect.x, rect.y, rect.width - 1, rect.y); |
| 565 |
g.drawLine(rect.x, rect.y, rect.x, rect.height - 1); |
| 566 |
|
| 567 |
g.setColor(_lightHighlightColor); // black drop shadow __| |
| 568 |
g.drawLine(rect.x, rect.height - 1, rect.width - 1, rect.height - 1); |
| 569 |
g.drawLine(rect.width - 1, rect.y, rect.width - 1, rect.height - 1); |
| 570 |
break; |
| 571 |
case ThemePainter.STATE_PRESSED: |
| 572 |
JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight); |
| 573 |
g.setColor(_shadowColor); // inner 3D border |
| 574 |
g.drawLine(rect.x, rect.y, rect.width - 1, rect.y); |
| 575 |
g.drawLine(rect.x, rect.y, rect.x, rect.height - 1); |
| 576 |
|
| 577 |
g.setColor(_lightHighlightColor); // black drop shadow __| |
| 578 |
g.drawLine(rect.x, rect.height - 1, rect.width - 1, rect.height - 1); |
| 579 |
g.drawLine(rect.width - 1, rect.y, rect.width - 1, rect.height - 1); |
| 580 |
break; |
| 581 |
case ThemePainter.STATE_ROLLOVER: |
| 582 |
JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight); |
| 583 |
g.setColor(_lightHighlightColor); // inner 3D border |
| 584 |
g.drawLine(rect.x, rect.y, rect.width - 1, rect.y); |
| 585 |
g.drawLine(rect.x, rect.y, rect.x, rect.height - 1); |
| 586 |
|
| 587 |
g.setColor(_shadowColor); // black drop shadow __| |
| 588 |
g.drawLine(rect.x, rect.height - 1, rect.width - 1, rect.height - 1); |
| 589 |
g.drawLine(rect.width - 1, rect.y, rect.width - 1, rect.height - 1); |
| 590 |
break; |
| 591 |
case ThemePainter.STATE_DISABLE: |
| 592 |
case ThemePainter.STATE_DISABLE_SELECTED: |
| 593 |
if (paintBackground) { |
| 594 |
JideSwingUtilities.paintBackground(g, rect, _highlight, _highlight); |
| 595 |
g.setColor(_lightHighlightColor); // inner 3D border |
| 596 |
g.drawLine(rect.x, rect.y, rect.width - 1, rect.y); |
| 597 |
g.drawLine(rect.x, rect.y, rect.x, rect.height - 1); |
| 598 |
|
| 599 |
g.setColor(_shadowColor); // black drop shadow __| |
| 600 |
g.drawLine(rect.x, rect.height - 1, rect.width - 1, rect.height - 1); |
| 601 |
g.drawLine(rect.width - 1, rect.y, rect.width - 1, rect.height - 1); |
| 602 |
} |
| 603 |
break; |
| 604 |
case ThemePainter.STATE_DEFAULT: |
| 605 |
if (paintBackground) { |
| 606 |
getPainter().paintButtonBackground(b, g, rect, JideSwingUtilities.getOrientationOf(b), state); |
| 607 |
} |
| 608 |
break; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
private void paintBackgroundInToolboxStyle(Graphics g, AbstractButton b, boolean paintBackground, boolean paintDefaultBorder) { |
| 613 |
Rectangle rect = new Rectangle(0, 0, b.getWidth(), b.getHeight()); |
| 614 |
if (b.getModel().isPressed()) { |
| 615 |
getPainter().paintButtonBackground(b, g, rect, JideSwingUtilities.getOrientationOf(b), ThemePainter.STATE_PRESSED); |
| 616 |
if (paintDefaultBorder) { |
| 617 |
g.setColor(_darkShadowColor); // inner 3D border |
| 618 |
g.drawLine(0, 0, b.getWidth() - 2, 0); |
| 619 |
g.drawLine(0, 0, 0, b.getHeight() - 2); |
| 620 |
|
| 621 |
g.setColor(_shadowColor); // inner 3D border |
| 622 |
g.drawLine(1, 1, b.getWidth() - 3, 1); |
| 623 |
g.drawLine(1, 1, 1, b.getHeight() - 3); |
| 624 |
|
| 625 |
g.setColor(_lightHighlightColor); // black drop shadow __| |
| 626 |
g.drawLine(0, b.getHeight() - 1, b.getWidth() - 1, b.getHeight() - 1); |
| 627 |
g.drawLine(b.getWidth() - 1, 0, b.getWidth() - 1, b.getHeight() - 1); |
| 628 |
} |
| 629 |
} |
| 630 |
else if (b.getModel().isSelected() && b.getModel().isRollover()) { |
| 631 |
getPainter().paintButtonBackground(b, g, rect, JideSwingUtilities.getOrientationOf(b), ThemePainter.STATE_PRESSED); |
| 632 |
if (paintDefaultBorder) { |
| 633 |
g.setColor(_darkShadowColor); // inner 3D border |
| 634 |
g.drawLine(0, 0, b.getWidth() - 2, 0); |
| 635 |
g.drawLine(0, 0, 0, b.getHeight() - 2); |
| 636 |
|
| 637 |
g.setColor(_shadowColor); // inner 3D border |
| 638 |
g.drawLine(1, 1, b.getWidth() - 3, 1); |
| 639 |
g.drawLine(1, 1, 1, b.getHeight() - 3); |
| 640 |
|
| 641 |
g.setColor(_lightHighlightColor); // black drop shadow __| |
| 642 |
g.drawLine(0, b.getHeight() - 1, b.getWidth() - 1, b.getHeight() - 1); |
| 643 |
g.drawLine(b.getWidth() - 1, 0, b.getWidth() - 1, b.getHeight() - 1); |
| 644 |
} |
| 645 |
} |
| 646 |
else if (b.getModel().isSelected()) { |
| 647 |
getPainter().paintButtonBackground(b, g, rect, JideSwingUtilities.getOrientationOf(b), ThemePainter.STATE_SELECTED); |
| 648 |
if (paintDefaultBorder) { |
| 649 |
g.setColor(_darkShadowColor); // inner 3D border |
| 650 |
g.drawLine(0, 0, b.getWidth() - 2, 0); |
| 651 |
g.drawLine(0, 0, 0, b.getHeight() - 2); |
| 652 |
|
| 653 |
g.setColor(_shadowColor); // inner 3D border |
| 654 |
g.drawLine(1, 1, b.getWidth() - 3, 1); |
| 655 |
g.drawLine(1, 1, 1, b.getHeight() - 3); |
| 656 |
|
| 657 |
g.setColor(_lightHighlightColor); // black drop shadow __| |
| 658 |
g.drawLine(0, b.getHeight() - 1, b.getWidth() - 1, b.getHeight() - 1); |
| 659 |
g.drawLine(b.getWidth() - 1, 0, b.getWidth() - 1, b.getHeight() - 1); |
| 660 |
} |
| 661 |
} |
| 662 |
else if (b.getModel().isRollover() || (b.hasFocus() && b.isFocusPainted())) { |
| 663 |
getPainter().paintButtonBackground(b, g, rect, JideSwingUtilities.getOrientationOf(b), ThemePainter.STATE_ROLLOVER); |
| 664 |
if (paintDefaultBorder) { |
| 665 |
g.setColor(_lightHighlightColor); // inner 3D border |
| 666 |
g.drawLine(0, 0, b.getWidth() - 1, 0); |
| 667 |
g.drawLine(0, 0, 0, b.getHeight() - 1); |
| 668 |
|
| 669 |
g.setColor(_shadowColor); // gray drop shadow __| |
| 670 |
g.drawLine(1, b.getHeight() - 2, b.getWidth() - 2, b.getHeight() - 2); |
| 671 |
g.drawLine(b.getWidth() - 2, 1, b.getWidth() - 2, b.getHeight() - 2); |
| 672 |
|
| 673 |
g.setColor(_darkShadowColor); // black drop shadow __| |
| 674 |
g.drawLine(0, b.getHeight() - 1, b.getWidth() - 1, b.getHeight() - 1); |
| 675 |
g.drawLine(b.getWidth() - 1, 0, b.getWidth() - 1, b.getHeight() - 1); |
| 676 |
} |
| 677 |
} |
| 678 |
else { |
| 679 |
if (paintBackground) { |
| 680 |
getPainter().paintButtonBackground(b, g, rect, JideSwingUtilities.getOrientationOf(b), ThemePainter.STATE_DEFAULT); |
| 681 |
} |
| 682 |
else { |
| 683 |
g.setColor(_lightHighlightColor); // inner 3D border |
| 684 |
g.drawLine(0, 0, b.getWidth() - 1, 0); |
| 685 |
g.drawLine(0, 0, 0, b.getHeight() - 1); |
| 686 |
|
| 687 |
g.setColor(_shadowColor); // black drop shadow __| |
| 688 |
g.drawLine(0, b.getHeight() - 1, b.getWidth() - 1, b.getHeight() - 1); |
| 689 |
g.drawLine(b.getWidth() - 1, 0, b.getWidth() - 1, b.getHeight() - 1); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
if (paintBackground) { |
| 694 |
g.setColor(_lightHighlightColor); // inner 3D border |
| 695 |
g.drawLine(0, 0, b.getWidth() - 1, 0); |
| 696 |
g.drawLine(0, 0, 0, b.getHeight() - 1); |
| 697 |
|
| 698 |
g.setColor(_shadowColor); // black drop shadow __| |
| 699 |
g.drawLine(0, b.getHeight() - 1, b.getWidth() - 1, b.getHeight() - 1); |
| 700 |
g.drawLine(b.getWidth() - 1, 0, b.getWidth() - 1, b.getHeight() - 1); |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
protected void clearTextShiftOffset() { |
| 705 |
this.shiftOffset = 0; |
| 706 |
} |
| 707 |
|
| 708 |
protected void setTextShiftOffset() { |
| 709 |
this.shiftOffset = defaultTextShiftOffset; |
| 710 |
} |
| 711 |
|
| 712 |
protected int getTextShiftOffset() { |
| 713 |
return shiftOffset; |
| 714 |
} |
| 715 |
|
| 716 |
// ******************************** |
| 717 |
// Layout Methods |
| 718 |
// ******************************** |
| 719 |
|
| 720 |
@Override |
| 721 |
public Dimension getMinimumSize(JComponent c) { |
| 722 |
Dimension d = getPreferredSize(c); |
| 723 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
| 724 |
if (v != null) { |
| 725 |
if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.HORIZONTAL) |
| 726 |
d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); |
| 727 |
else // TODO: not sure if this is correct |
| 728 |
d.height -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); |
| 729 |
} |
| 730 |
return d; |
| 731 |
} |
| 732 |
|
| 733 |
@Override |
| 734 |
public Dimension getPreferredSize(JComponent c) { |
| 735 |
AbstractButton b = (AbstractButton) c; |
| 736 |
|
| 737 |
Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, b.getIconTextGap()); |
| 738 |
if (JideSwingUtilities.getOrientationOf(c) == SwingConstants.HORIZONTAL) { |
| 739 |
return d; |
| 740 |
} |
| 741 |
else { |
| 742 |
return new Dimension(d.height, d.width); // swap width and height |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
@Override |
| 747 |
public Dimension getMaximumSize(JComponent c) { |
| 748 |
Dimension d = getPreferredSize(c); |
| 749 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
| 750 |
if (v != null) { |
| 751 |
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); |
| 752 |
} |
| 753 |
return d; |
| 754 |
} |
| 755 |
|
| 756 |
public ThemePainter getPainter() { |
| 757 |
return _painter; |
| 758 |
} |
| 759 |
|
| 760 |
protected void updateMargin(AbstractButton b) { |
| 761 |
String pp = getPropertyPrefix(); |
| 762 |
if (LookAndFeelFactory.isLnfInUse(LookAndFeelFactory.SYNTH_LNF)) { |
| 763 |
SynthStyle ss = SynthLookAndFeel.getStyle(b, Region.BUTTON); |
| 764 |
SynthContext sc = new SynthContext(b, Region.BUTTON, ss, 0); |
| 765 |
Insets insets = ss.getInsets(sc, new InsetsUIResource(0, 0, 0, 0)); |
| 766 |
if (insets != null) { |
| 767 |
b.setMargin(insets); |
| 768 |
} |
| 769 |
} |
| 770 |
else if (b.getMargin() == null || (b.getMargin() instanceof UIResource)) { |
| 771 |
if (shouldWrapText(b)) { |
| 772 |
b.setMargin(UIDefaultsLookup.getInsets(pp + "margin.vertical")); |
| 773 |
} |
| 774 |
else { |
| 775 |
b.setMargin(UIDefaultsLookup.getInsets(pp + "margin")); |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
/** |
| 781 |
* Checks if we should wrap text on a button. If the vertical text position is bottom and horizontal text position |
| 782 |
* is center, we will wrap the text. |
| 783 |
* |
| 784 |
* @param c the component |
| 785 |
* |
| 786 |
* @return true or false. |
| 787 |
*/ |
| 788 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 789 |
public static boolean shouldWrapText(Component c) { |
| 790 |
// return false for now before we support the text wrapping |
| 791 |
return false; |
| 792 |
|
| 793 |
// boolean wrapText = false; |
| 794 |
// if (c instanceof AbstractButton) { |
| 795 |
// if (((AbstractButton) c).getVerticalTextPosition() == SwingConstants.BOTTOM && ((AbstractButton) c).getHorizontalTextPosition() == SwingConstants.CENTER) { |
| 796 |
// wrapText = true; |
| 797 |
// } |
| 798 |
// } |
| 799 |
// return wrapText; |
| 800 |
} |
| 801 |
} |