| 1 |
/* |
| 2 |
* @(#)VsnetMenuItemUI.java |
| 3 |
* |
| 4 |
* Copyright 2002 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.plaf.eclipse; |
| 8 |
|
| 9 |
import com.jidesoft.icons.IconsFactory; |
| 10 |
import com.jidesoft.plaf.LookAndFeelFactory; |
| 11 |
import com.jidesoft.plaf.UIDefaultsLookup; |
| 12 |
import com.jidesoft.plaf.basic.ThemePainter; |
| 13 |
import com.jidesoft.plaf.windows.WindowsGraphicsUtilsPort; |
| 14 |
import com.jidesoft.swing.ButtonStyle; |
| 15 |
import com.jidesoft.swing.JideSwingUtilities; |
| 16 |
import com.jidesoft.swing.TopLevelMenuContainer; |
| 17 |
|
| 18 |
import javax.swing.*; |
| 19 |
import javax.swing.event.*; |
| 20 |
import javax.swing.plaf.*; |
| 21 |
import javax.swing.plaf.basic.BasicHTML; |
| 22 |
import javax.swing.text.View; |
| 23 |
import java.awt.*; |
| 24 |
import java.awt.event.ActionEvent; |
| 25 |
import java.awt.event.InputEvent; |
| 26 |
import java.awt.event.KeyEvent; |
| 27 |
import java.awt.event.MouseEvent; |
| 28 |
import java.awt.geom.AffineTransform; |
| 29 |
import java.beans.PropertyChangeEvent; |
| 30 |
import java.beans.PropertyChangeListener; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* MenuItem UI implementation |
| 35 |
*/ |
| 36 |
public class EclipseMenuItemUI extends MenuItemUI { |
| 37 |
protected JMenuItem menuItem = null; |
| 38 |
protected Color selectionBackground; |
| 39 |
protected Color selectionForeground; |
| 40 |
protected Color disabledForeground; |
| 41 |
protected Color acceleratorForeground; |
| 42 |
protected Color acceleratorSelectionForeground; |
| 43 |
private String acceleratorDelimiter; |
| 44 |
|
| 45 |
protected int defaultTextIconGap; |
| 46 |
protected Font acceleratorFont; |
| 47 |
|
| 48 |
protected MouseInputListener mouseInputListener; |
| 49 |
protected MenuDragMouseListener menuDragMouseListener; |
| 50 |
protected MenuKeyListener menuKeyListener; |
| 51 |
private PropertyChangeListener propertyChangeListener; |
| 52 |
|
| 53 |
protected Icon arrowIcon = null; |
| 54 |
protected Icon checkIcon = null; |
| 55 |
|
| 56 |
protected boolean oldBorderPainted; |
| 57 |
|
| 58 |
/** |
| 59 |
* Used for accelerator binding, lazily created. |
| 60 |
*/ |
| 61 |
InputMap windowInputMap; |
| 62 |
|
| 63 |
private static final boolean DEBUG = false; // show bad params, misc. |
| 64 |
|
| 65 |
// added for VsnetMenuItemUI |
| 66 |
protected Color shadowColor; |
| 67 |
protected int defaultAccelEndGap; |
| 68 |
protected int defaultShadowWidth; |
| 69 |
private Color borderColor; |
| 70 |
private Color backgroundColor; |
| 71 |
// end added for VsnetMenuItemUI |
| 72 |
|
| 73 |
/* Client Property keys for text and accelerator text widths */ |
| 74 |
static final String MAX_TEXT_WIDTH = "maxTextWidth"; |
| 75 |
static final String MAX_ACC_WIDTH = "maxAccWidth"; |
| 76 |
|
| 77 |
protected ThemePainter _painter; |
| 78 |
|
| 79 |
public static ComponentUI createUI(JComponent c) { |
| 80 |
return new EclipseMenuItemUI(); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void installUI(JComponent c) { |
| 85 |
menuItem = (JMenuItem) c; |
| 86 |
|
| 87 |
installDefaults(); |
| 88 |
installComponents(menuItem); |
| 89 |
installListeners(); |
| 90 |
installKeyboardActions(); |
| 91 |
} |
| 92 |
|
| 93 |
protected void installDefaults() { |
| 94 |
_painter = (ThemePainter) UIDefaultsLookup.get("Theme.painter"); |
| 95 |
String prefix = getPropertyPrefix(); |
| 96 |
|
| 97 |
acceleratorFont = UIDefaultsLookup.getFont("MenuItem.acceleratorFont"); |
| 98 |
if (acceleratorFont == null) { |
| 99 |
acceleratorFont = UIManager.getFont("MenuItem.font"); |
| 100 |
} |
| 101 |
|
| 102 |
menuItem.setOpaque(true); |
| 103 |
if (menuItem.getMargin() == null || |
| 104 |
(menuItem.getMargin() instanceof UIResource)) { |
| 105 |
// if(useCheckAndArrow()) |
| 106 |
// menuItem.setMargin(UIManagerLookup.getInsets("MenuItem.margin")); |
| 107 |
// else |
| 108 |
menuItem.setMargin(UIDefaultsLookup.getInsets(prefix + ".margin")); |
| 109 |
} |
| 110 |
|
| 111 |
// added for VsnetMenuItemUI |
| 112 |
defaultTextIconGap = UIDefaultsLookup.getInt("MenuItem.textIconGap"); |
| 113 |
defaultAccelEndGap = UIDefaultsLookup.getInt("MenuItem.accelEndGap"); |
| 114 |
defaultShadowWidth = UIDefaultsLookup.getInt("MenuItem.shadowWidth"); |
| 115 |
// end add |
| 116 |
|
| 117 |
borderColor = UIDefaultsLookup.getColor("MenuItem.selectionBorderColor"); |
| 118 |
backgroundColor = UIDefaultsLookup.getColor("MenuItem.background"); |
| 119 |
shadowColor = UIDefaultsLookup.getColor("MenuItem.shadowColor"); |
| 120 |
|
| 121 |
LookAndFeel.installBorder(menuItem, prefix + ".border"); |
| 122 |
oldBorderPainted = menuItem.isBorderPainted(); |
| 123 |
Object value = UIDefaultsLookup.get(prefix + ".borderPainted"); |
| 124 |
menuItem.setBorderPainted(value instanceof Boolean ? (Boolean) value : false); |
| 125 |
LookAndFeel.installColorsAndFont(menuItem, |
| 126 |
prefix + ".background", |
| 127 |
prefix + ".foreground", |
| 128 |
prefix + ".font"); |
| 129 |
|
| 130 |
// MenuItem specific defaults |
| 131 |
if (selectionBackground == null || |
| 132 |
selectionBackground instanceof UIResource) { |
| 133 |
selectionBackground = |
| 134 |
UIDefaultsLookup.getColor(prefix + ".selectionBackground"); |
| 135 |
} |
| 136 |
if (selectionForeground == null || |
| 137 |
selectionForeground instanceof UIResource) { |
| 138 |
selectionForeground = |
| 139 |
UIDefaultsLookup.getColor(prefix + ".selectionForeground"); |
| 140 |
} |
| 141 |
if (disabledForeground == null || |
| 142 |
disabledForeground instanceof UIResource) { |
| 143 |
disabledForeground = |
| 144 |
UIDefaultsLookup.getColor(prefix + ".disabledForeground"); |
| 145 |
} |
| 146 |
if (acceleratorForeground == null || |
| 147 |
acceleratorForeground instanceof UIResource) { |
| 148 |
acceleratorForeground = |
| 149 |
UIDefaultsLookup.getColor(prefix + ".acceleratorForeground"); |
| 150 |
} |
| 151 |
if (acceleratorSelectionForeground == null || |
| 152 |
acceleratorSelectionForeground instanceof UIResource) { |
| 153 |
acceleratorSelectionForeground = |
| 154 |
UIDefaultsLookup.getColor(prefix + ".acceleratorSelectionForeground"); |
| 155 |
} |
| 156 |
// Get accelerator delimiter |
| 157 |
acceleratorDelimiter = |
| 158 |
UIDefaultsLookup.getString("MenuItem.acceleratorDelimiter"); |
| 159 |
if (acceleratorDelimiter == null) { |
| 160 |
acceleratorDelimiter = "+"; |
| 161 |
} |
| 162 |
// Icons |
| 163 |
if (arrowIcon == null || |
| 164 |
arrowIcon instanceof UIResource) { |
| 165 |
arrowIcon = UIDefaultsLookup.getIcon(prefix + ".arrowIcon"); |
| 166 |
} |
| 167 |
if (checkIcon == null || |
| 168 |
checkIcon instanceof UIResource) { |
| 169 |
checkIcon = UIDefaultsLookup.getIcon(prefix + ".checkIcon"); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @param menuItem the menu item |
| 175 |
* @since 1.3 |
| 176 |
*/ |
| 177 |
protected void installComponents(JMenuItem menuItem) { |
| 178 |
BasicHTML.updateRenderer(menuItem, menuItem.getText()); |
| 179 |
} |
| 180 |
|
| 181 |
protected String getPropertyPrefix() { |
| 182 |
return "MenuItem"; |
| 183 |
} |
| 184 |
|
| 185 |
protected void installListeners() { |
| 186 |
if ((mouseInputListener = createMouseInputListener(menuItem)) != null) { |
| 187 |
menuItem.addMouseListener(mouseInputListener); |
| 188 |
menuItem.addMouseMotionListener(mouseInputListener); |
| 189 |
} |
| 190 |
if ((menuDragMouseListener = createMenuDragMouseListener(menuItem)) != null) { |
| 191 |
menuItem.addMenuDragMouseListener(menuDragMouseListener); |
| 192 |
} |
| 193 |
if ((menuKeyListener = createMenuKeyListener(menuItem)) != null) { |
| 194 |
menuItem.addMenuKeyListener(menuKeyListener); |
| 195 |
} |
| 196 |
if ((propertyChangeListener = createPropertyChangeListener(menuItem)) != null) { |
| 197 |
menuItem.addPropertyChangeListener(propertyChangeListener); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
protected void installKeyboardActions() { |
| 202 |
ActionMap actionMap = getActionMap(); |
| 203 |
|
| 204 |
SwingUtilities.replaceUIActionMap(menuItem, actionMap); |
| 205 |
updateAcceleratorBinding(); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public void uninstallUI(JComponent c) { |
| 210 |
menuItem = (JMenuItem) c; |
| 211 |
uninstallDefaults(); |
| 212 |
uninstallComponents(menuItem); |
| 213 |
uninstallListeners(); |
| 214 |
uninstallKeyboardActions(); |
| 215 |
|
| 216 |
//Remove the textWidth and accWidth values from the parent's Client Properties. |
| 217 |
Container parent = menuItem.getParent(); |
| 218 |
if ((parent != null && parent instanceof JComponent) && |
| 219 |
!(menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu())) { |
| 220 |
JComponent p = (JComponent) parent; |
| 221 |
p.putClientProperty(EclipseMenuItemUI.MAX_ACC_WIDTH, null); |
| 222 |
p.putClientProperty(EclipseMenuItemUI.MAX_TEXT_WIDTH, null); |
| 223 |
} |
| 224 |
|
| 225 |
menuItem = null; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
protected void uninstallDefaults() { |
| 230 |
_painter = null; |
| 231 |
LookAndFeel.uninstallBorder(menuItem); |
| 232 |
menuItem.setBorderPainted(oldBorderPainted); |
| 233 |
if (menuItem.getMargin() instanceof UIResource) |
| 234 |
menuItem.setMargin(null); |
| 235 |
if (arrowIcon instanceof UIResource) |
| 236 |
arrowIcon = null; |
| 237 |
if (checkIcon instanceof UIResource) |
| 238 |
checkIcon = null; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* @param menuItem the menu item |
| 243 |
* @since 1.3 |
| 244 |
*/ |
| 245 |
protected void uninstallComponents(JMenuItem menuItem) { |
| 246 |
BasicHTML.updateRenderer(menuItem, ""); |
| 247 |
} |
| 248 |
|
| 249 |
protected void uninstallListeners() { |
| 250 |
if (mouseInputListener != null) { |
| 251 |
menuItem.removeMouseListener(mouseInputListener); |
| 252 |
menuItem.removeMouseMotionListener(mouseInputListener); |
| 253 |
} |
| 254 |
if (menuDragMouseListener != null) { |
| 255 |
menuItem.removeMenuDragMouseListener(menuDragMouseListener); |
| 256 |
} |
| 257 |
if (menuKeyListener != null) { |
| 258 |
menuItem.removeMenuKeyListener(menuKeyListener); |
| 259 |
} |
| 260 |
if (propertyChangeListener != null) { |
| 261 |
menuItem.removePropertyChangeListener(propertyChangeListener); |
| 262 |
} |
| 263 |
|
| 264 |
mouseInputListener = null; |
| 265 |
menuDragMouseListener = null; |
| 266 |
menuKeyListener = null; |
| 267 |
propertyChangeListener = null; |
| 268 |
} |
| 269 |
|
| 270 |
protected void uninstallKeyboardActions() { |
| 271 |
SwingUtilities.replaceUIActionMap(menuItem, null); |
| 272 |
if (windowInputMap != null) { |
| 273 |
SwingUtilities.replaceUIInputMap(menuItem, JComponent. |
| 274 |
WHEN_IN_FOCUSED_WINDOW, null); |
| 275 |
windowInputMap = null; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
protected MouseInputListener createMouseInputListener(JComponent c) { |
| 280 |
return new MouseInputHandler(); |
| 281 |
} |
| 282 |
|
| 283 |
protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { |
| 284 |
return new MenuDragMouseHandler(); |
| 285 |
} |
| 286 |
|
| 287 |
protected MenuKeyListener createMenuKeyListener(JComponent c) { |
| 288 |
return new MenuKeyHandler(); |
| 289 |
} |
| 290 |
|
| 291 |
private PropertyChangeListener createPropertyChangeListener(JComponent c) { |
| 292 |
return new PropertyChangeHandler(); |
| 293 |
} |
| 294 |
|
| 295 |
protected ActionMap getActionMap() { |
| 296 |
String propertyPrefix = getPropertyPrefix(); |
| 297 |
String uiKey = propertyPrefix + ".actionMap"; |
| 298 |
ActionMap am = (ActionMap) UIDefaultsLookup.get(uiKey); |
| 299 |
if (am == null) { |
| 300 |
am = createActionMap(); |
| 301 |
UIManager.getLookAndFeelDefaults().put(uiKey, am); |
| 302 |
} |
| 303 |
return am; |
| 304 |
} |
| 305 |
|
| 306 |
protected ActionMap createActionMap() { |
| 307 |
ActionMap map = new ActionMapUIResource(); |
| 308 |
map.put("doClick", new ClickAction()); |
| 309 |
|
| 310 |
// removed for VsnetMenuItem. it's protected method |
| 311 |
// Set the ActionMap's parent to the Auditory Feedback Action Map |
| 312 |
// BasicLookAndFeel lf = (BasicLookAndFeel) UIManager.getLookAndFeel(); |
| 313 |
// ActionMap audioMap = lf.getAudioActionMap(); |
| 314 |
// map.setParent(audioMap); |
| 315 |
|
| 316 |
return map; |
| 317 |
} |
| 318 |
|
| 319 |
protected InputMap createInputMap(int condition) { |
| 320 |
if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { |
| 321 |
return new ComponentInputMapUIResource(menuItem); |
| 322 |
} |
| 323 |
return null; |
| 324 |
} |
| 325 |
|
| 326 |
void updateAcceleratorBinding() { |
| 327 |
KeyStroke accelerator = menuItem.getAccelerator(); |
| 328 |
|
| 329 |
if (windowInputMap != null) { |
| 330 |
windowInputMap.clear(); |
| 331 |
} |
| 332 |
if (accelerator != null) { |
| 333 |
if (windowInputMap == null) { |
| 334 |
windowInputMap = createInputMap(JComponent. |
| 335 |
WHEN_IN_FOCUSED_WINDOW); |
| 336 |
SwingUtilities.replaceUIInputMap(menuItem, |
| 337 |
JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap); |
| 338 |
} |
| 339 |
windowInputMap.put(accelerator, "doClick"); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
@Override |
| 344 |
public Dimension getMinimumSize(JComponent c) { |
| 345 |
Dimension d = null; |
| 346 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
| 347 |
if (v != null) { |
| 348 |
d = getPreferredSize(c); |
| 349 |
d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); |
| 350 |
} |
| 351 |
return d; |
| 352 |
} |
| 353 |
|
| 354 |
@Override |
| 355 |
public Dimension getPreferredSize(JComponent c) { |
| 356 |
return getPreferredMenuItemSize(c, |
| 357 |
checkIcon, |
| 358 |
arrowIcon, |
| 359 |
defaultTextIconGap); |
| 360 |
} |
| 361 |
|
| 362 |
@Override |
| 363 |
public Dimension getMaximumSize(JComponent c) { |
| 364 |
Dimension d = null; |
| 365 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
| 366 |
if (v != null) { |
| 367 |
d = getPreferredSize(c); |
| 368 |
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); |
| 369 |
} |
| 370 |
return d; |
| 371 |
} |
| 372 |
|
| 373 |
// these rects are used for painting and preferred size calculations. |
| 374 |
// they used to be regenerated constantly. Now they are reused. |
| 375 |
static Rectangle zeroRect = new Rectangle(0, 0, 0, 0); |
| 376 |
static Rectangle iconRect = new Rectangle(); |
| 377 |
static Rectangle textRect = new Rectangle(); |
| 378 |
static Rectangle acceleratorRect = new Rectangle(); |
| 379 |
static Rectangle checkIconRect = new Rectangle(); |
| 380 |
static Rectangle arrowIconRect = new Rectangle(); |
| 381 |
static Rectangle viewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE); |
| 382 |
static Rectangle r = new Rectangle(); |
| 383 |
|
| 384 |
private void resetRects() { |
| 385 |
iconRect.setBounds(zeroRect); |
| 386 |
textRect.setBounds(zeroRect); |
| 387 |
acceleratorRect.setBounds(zeroRect); |
| 388 |
checkIconRect.setBounds(zeroRect); |
| 389 |
arrowIconRect.setBounds(zeroRect); |
| 390 |
viewRect.setBounds(0, 0, Short.MAX_VALUE, Short.MAX_VALUE); |
| 391 |
r.setBounds(zeroRect); |
| 392 |
} |
| 393 |
|
| 394 |
protected Dimension getPreferredMenuItemSize(JComponent c, |
| 395 |
Icon checkIcon, |
| 396 |
Icon arrowIcon, |
| 397 |
int textIconGap) { |
| 398 |
JMenuItem b = (JMenuItem) c; |
| 399 |
Icon icon = b.getIcon(); |
| 400 |
String text = b.getText(); |
| 401 |
KeyStroke accelerator = b.getAccelerator(); |
| 402 |
String acceleratorText = ""; |
| 403 |
|
| 404 |
if (accelerator != null) { |
| 405 |
int modifiers = accelerator.getModifiers(); |
| 406 |
if (modifiers > 0) { |
| 407 |
acceleratorText = KeyEvent.getKeyModifiersText(modifiers); |
| 408 |
//acceleratorText += "-"; |
| 409 |
acceleratorText += acceleratorDelimiter; |
| 410 |
} |
| 411 |
int keyCode = accelerator.getKeyCode(); |
| 412 |
if (keyCode != 0) { |
| 413 |
acceleratorText += KeyEvent.getKeyText(keyCode); |
| 414 |
} |
| 415 |
else { |
| 416 |
acceleratorText += accelerator.getKeyChar(); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
Font font = b.getFont(); |
| 421 |
FontMetrics fm = b.getFontMetrics(font); |
| 422 |
FontMetrics fmAccel = b.getFontMetrics(acceleratorFont); |
| 423 |
|
| 424 |
resetRects(); |
| 425 |
|
| 426 |
layoutMenuItem(fm, text, fmAccel, acceleratorText, icon, checkIcon, arrowIcon, |
| 427 |
b.getVerticalAlignment(), b.getHorizontalAlignment(), |
| 428 |
b.getVerticalTextPosition(), b.getHorizontalTextPosition(), |
| 429 |
viewRect, iconRect, textRect, acceleratorRect, checkIconRect, arrowIconRect, |
| 430 |
text == null ? 0 : textIconGap, |
| 431 |
defaultAccelEndGap); |
| 432 |
// find the union of the icon and text rects |
| 433 |
r.setBounds(textRect); |
| 434 |
if (!iconRect.isEmpty()) { |
| 435 |
r = SwingUtilities.computeUnion(iconRect.x, |
| 436 |
iconRect.y, |
| 437 |
iconRect.width, |
| 438 |
iconRect.height, |
| 439 |
r); |
| 440 |
} |
| 441 |
// To make the accelerator texts appear in a column, find the widest MenuItem text |
| 442 |
// and the widest accelerator text. |
| 443 |
|
| 444 |
//Get the parent, which stores the information. |
| 445 |
Container parent = menuItem.getParent(); |
| 446 |
|
| 447 |
//Check the parent, and see that it is not a top-level menu. |
| 448 |
if (parent != null && parent instanceof JComponent && |
| 449 |
!(menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu())) { |
| 450 |
JComponent p = (JComponent) parent; |
| 451 |
|
| 452 |
//Get widest text so far from parent, if no one exists null is returned. |
| 453 |
Integer maxTextWidth = (Integer) p.getClientProperty(EclipseMenuItemUI.MAX_TEXT_WIDTH); |
| 454 |
Integer maxAccWidth = (Integer) p.getClientProperty(EclipseMenuItemUI.MAX_ACC_WIDTH); |
| 455 |
|
| 456 |
int maxTextValue = maxTextWidth != null ? maxTextWidth : 0; |
| 457 |
int maxAccValue = maxAccWidth != null ? maxAccWidth : 0; |
| 458 |
|
| 459 |
//Compare the text widths, and adjust the r.width to the widest. |
| 460 |
if (r.width < maxTextValue) { |
| 461 |
r.width = maxTextValue; |
| 462 |
} |
| 463 |
else { |
| 464 |
p.putClientProperty(EclipseMenuItemUI.MAX_TEXT_WIDTH, r.width); |
| 465 |
} |
| 466 |
|
| 467 |
//Compare the accelerator widths. |
| 468 |
if (acceleratorRect.width > maxAccValue) { |
| 469 |
maxAccValue = acceleratorRect.width; |
| 470 |
p.putClientProperty(EclipseMenuItemUI.MAX_ACC_WIDTH, acceleratorRect.width); |
| 471 |
} |
| 472 |
|
| 473 |
//Add on the widest accelerator |
| 474 |
r.width += maxAccValue; |
| 475 |
r.width += textIconGap; |
| 476 |
r.width += defaultAccelEndGap; |
| 477 |
} |
| 478 |
|
| 479 |
if (icon != null) |
| 480 |
r.width += textIconGap; |
| 481 |
|
| 482 |
Insets insets = b.getInsets(); |
| 483 |
if (useCheckAndArrow()) { |
| 484 |
insets = UIDefaultsLookup.getInsets("MenuItem.margin"); |
| 485 |
r.width += 5; |
| 486 |
} |
| 487 |
if (insets != null) { |
| 488 |
r.width += insets.left + insets.right; |
| 489 |
r.height += insets.top + insets.bottom; |
| 490 |
} |
| 491 |
|
| 492 |
// if the width is even, bump it up one. This is critical |
| 493 |
// for the focus dash line to draw properly |
| 494 |
if (r.width % 2 == 0) { |
| 495 |
r.width++; |
| 496 |
} |
| 497 |
|
| 498 |
// if the height is even, bump it up one. This is critical |
| 499 |
// for the text to center properly |
| 500 |
if (r.height % 2 == 0) { |
| 501 |
r.height++; |
| 502 |
} |
| 503 |
|
| 504 |
if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) { |
| 505 |
return r.getSize(); |
| 506 |
} |
| 507 |
else { |
| 508 |
return new Dimension(r.height, r.width); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* We draw the background in paintMenuItem() so override update (which fills the background of opaque components by |
| 514 |
* default) to just call paint(). |
| 515 |
*/ |
| 516 |
@Override |
| 517 |
public void update(Graphics g, JComponent c) { |
| 518 |
paint(g, c); |
| 519 |
} |
| 520 |
|
| 521 |
@Override |
| 522 |
public void paint(Graphics g, JComponent c) { |
| 523 |
paintMenuItem(g, c, checkIcon, arrowIcon, |
| 524 |
selectionBackground, selectionForeground, |
| 525 |
defaultTextIconGap); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
protected void paintMenuItem(Graphics g, JComponent c, |
| 530 |
Icon checkIcon, Icon arrowIcon, |
| 531 |
Color background, Color foreground, |
| 532 |
int defaultTextIconGap) { |
| 533 |
JMenuItem b = (JMenuItem) c; |
| 534 |
ButtonModel model = b.getModel(); |
| 535 |
|
| 536 |
int menuWidth; |
| 537 |
int menuHeight; |
| 538 |
|
| 539 |
if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) { |
| 540 |
// Dimension size = b.getSize(); |
| 541 |
menuWidth = b.getWidth(); |
| 542 |
menuHeight = b.getHeight(); |
| 543 |
} |
| 544 |
else { |
| 545 |
// Dimension size = b.getSize(); |
| 546 |
menuWidth = b.getHeight(); |
| 547 |
menuHeight = b.getWidth(); |
| 548 |
Graphics2D g2d = (Graphics2D) g; |
| 549 |
AffineTransform oldAt = g2d.getTransform(); |
| 550 |
g2d.rotate(Math.PI / 2); |
| 551 |
g2d.translate(0, -menuHeight + 1); |
| 552 |
} |
| 553 |
|
| 554 |
Insets i = c.getInsets(); |
| 555 |
|
| 556 |
resetRects(); |
| 557 |
|
| 558 |
viewRect.setBounds(0, 0, menuWidth, menuHeight); |
| 559 |
|
| 560 |
viewRect.x += i.left; |
| 561 |
viewRect.y += i.top; |
| 562 |
viewRect.width -= (i.right + viewRect.x); |
| 563 |
viewRect.height -= (i.bottom + viewRect.y); |
| 564 |
|
| 565 |
|
| 566 |
Font holdf = g.getFont(); |
| 567 |
Font f = c.getFont(); |
| 568 |
g.setFont(f); |
| 569 |
FontMetrics fm = g.getFontMetrics(f); |
| 570 |
FontMetrics fmAccel = g.getFontMetrics(acceleratorFont); |
| 571 |
|
| 572 |
// get Accelerator text |
| 573 |
KeyStroke accelerator = b.getAccelerator(); |
| 574 |
String acceleratorText = ""; |
| 575 |
if (accelerator != null) { |
| 576 |
int modifiers = accelerator.getModifiers(); |
| 577 |
if (modifiers > 0) { |
| 578 |
acceleratorText = KeyEvent.getKeyModifiersText(modifiers); |
| 579 |
//acceleratorText += "-"; |
| 580 |
acceleratorText += acceleratorDelimiter; |
| 581 |
} |
| 582 |
|
| 583 |
int keyCode = accelerator.getKeyCode(); |
| 584 |
if (keyCode != 0) { |
| 585 |
acceleratorText += KeyEvent.getKeyText(keyCode); |
| 586 |
} |
| 587 |
else { |
| 588 |
acceleratorText += accelerator.getKeyChar(); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
// layout the text and icon |
| 593 |
String text = layoutMenuItem(fm, b.getText(), fmAccel, acceleratorText, b.getIcon(), |
| 594 |
checkIcon, arrowIcon, |
| 595 |
b.getVerticalAlignment(), b.getHorizontalAlignment(), |
| 596 |
b.getVerticalTextPosition(), b.getHorizontalTextPosition(), |
| 597 |
viewRect, iconRect, textRect, acceleratorRect, |
| 598 |
checkIconRect, arrowIconRect, |
| 599 |
b.getText() == null ? 0 : defaultTextIconGap, |
| 600 |
defaultTextIconGap); |
| 601 |
|
| 602 |
// Paint background |
| 603 |
paintBackground(g, b, background); |
| 604 |
|
| 605 |
Color holdc = g.getColor(); |
| 606 |
|
| 607 |
// Paint the Check |
| 608 |
if ((c.getUIClassID().indexOf("CheckBoxMenu") >= 0 || c.getUIClassID().indexOf("RadioButtonMenu") >= 0) && checkIcon != null) { |
| 609 |
paintCheckBox(b, g, checkIcon); |
| 610 |
g.setColor(holdc); |
| 611 |
} |
| 612 |
|
| 613 |
paintIcon(b, g); |
| 614 |
|
| 615 |
// Draw the Text |
| 616 |
if (text != null) { |
| 617 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
| 618 |
if (v != null) { |
| 619 |
v.paint(g, textRect); |
| 620 |
} |
| 621 |
else { |
| 622 |
paintText(g, b, textRect, text); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
// Draw the Accelerator Text |
| 627 |
if (acceleratorText != null && !acceleratorText.equals("")) { |
| 628 |
|
| 629 |
//Get the maxAccWidth from the parent to calculate the offset. |
| 630 |
int accOffset = 0; |
| 631 |
Container parent = menuItem.getParent(); |
| 632 |
if (parent != null && parent instanceof JComponent) { |
| 633 |
JComponent p = (JComponent) parent; |
| 634 |
Integer maxValueInt = (Integer) p.getClientProperty(EclipseMenuItemUI.MAX_ACC_WIDTH); |
| 635 |
int maxValue = maxValueInt != null ? |
| 636 |
maxValueInt : acceleratorRect.width; |
| 637 |
|
| 638 |
//Calculate the offset, with which the accelerator texts will be drawn with. |
| 639 |
accOffset = maxValue - acceleratorRect.width; |
| 640 |
} |
| 641 |
|
| 642 |
g.setFont(acceleratorFont); |
| 643 |
if (!model.isEnabled()) { |
| 644 |
// *** paint the acceleratorText disabled |
| 645 |
if (disabledForeground != null) { |
| 646 |
g.setColor(disabledForeground); |
| 647 |
JideSwingUtilities.drawString(menuItem, g, acceleratorText, |
| 648 |
acceleratorRect.x - accOffset, |
| 649 |
acceleratorRect.y + fmAccel.getAscent()); |
| 650 |
} |
| 651 |
else { |
| 652 |
g.setColor(b.getBackground().brighter()); |
| 653 |
JideSwingUtilities.drawString(menuItem, g, acceleratorText, |
| 654 |
acceleratorRect.x - accOffset, |
| 655 |
acceleratorRect.y + fmAccel.getAscent()); |
| 656 |
g.setColor(b.getBackground().darker()); |
| 657 |
JideSwingUtilities.drawString(menuItem, g, acceleratorText, |
| 658 |
acceleratorRect.x - accOffset - 1, |
| 659 |
acceleratorRect.y + fmAccel.getAscent() - 1); |
| 660 |
} |
| 661 |
} |
| 662 |
else { |
| 663 |
// *** paint the acceleratorText normally |
| 664 |
if (model.isArmed() || (c instanceof JMenu && model.isSelected())) { |
| 665 |
g.setColor(acceleratorSelectionForeground); |
| 666 |
} |
| 667 |
else { |
| 668 |
g.setColor(acceleratorForeground); |
| 669 |
} |
| 670 |
JideSwingUtilities.drawString(menuItem, g, acceleratorText, |
| 671 |
acceleratorRect.x - accOffset, |
| 672 |
acceleratorRect.y + fmAccel.getAscent()); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
// Paint the Arrow |
| 677 |
if (arrowIcon != null) { |
| 678 |
if (model.isArmed() || (c instanceof JMenu && model.isSelected())) |
| 679 |
g.setColor(foreground); |
| 680 |
if (useCheckAndArrow()) |
| 681 |
arrowIcon.paintIcon(c, g, arrowIconRect.x, arrowIconRect.y); |
| 682 |
} |
| 683 |
g.setColor(holdc); |
| 684 |
g.setFont(holdf); |
| 685 |
} |
| 686 |
|
| 687 |
private void paintCheckBox(JMenuItem b, Graphics g, Icon checkIcon) { |
| 688 |
boolean selected = false; |
| 689 |
ButtonModel model = b.getModel(); |
| 690 |
if (b instanceof JCheckBoxMenuItem) |
| 691 |
selected = b.isSelected(); |
| 692 |
else if (b instanceof JRadioButtonMenuItem) |
| 693 |
selected = b.isSelected(); |
| 694 |
if (selected) { |
| 695 |
if (b.getIcon() == null) { |
| 696 |
if (model.isArmed()) { |
| 697 |
if (checkIcon instanceof ImageIcon) { |
| 698 |
ImageIcon image = IconsFactory.createMaskImage(b, checkIcon, Color.BLACK, selectionForeground); |
| 699 |
image.paintIcon(b, g, checkIconRect.x, checkIconRect.y); |
| 700 |
} |
| 701 |
else { |
| 702 |
ImageIcon image = IconsFactory.createNegativeImage(b, checkIcon); |
| 703 |
image.paintIcon(b, g, checkIconRect.x, checkIconRect.y); |
| 704 |
} |
| 705 |
} |
| 706 |
else { |
| 707 |
if (checkIcon instanceof ImageIcon) { |
| 708 |
ImageIcon image = IconsFactory.createMaskImage(b, checkIcon, Color.BLACK, b.getForeground()); |
| 709 |
image.paintIcon(b, g, checkIconRect.x, checkIconRect.y); |
| 710 |
} |
| 711 |
else { |
| 712 |
checkIcon.paintIcon(b, g, checkIconRect.x, checkIconRect.y); |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
private void paintIcon(JMenuItem b, Graphics g) { |
| 720 |
ButtonModel model = b.getModel(); |
| 721 |
// Paint the Icon |
| 722 |
if (b.getIcon() != null) { |
| 723 |
Icon icon; |
| 724 |
if (!model.isEnabled()) { |
| 725 |
icon = b.getDisabledIcon(); |
| 726 |
if (icon == null) { |
| 727 |
icon = b.getIcon(); |
| 728 |
if (icon instanceof ImageIcon) { |
| 729 |
icon = IconsFactory.createGrayImage(((ImageIcon) icon).getImage()); |
| 730 |
} |
| 731 |
else { |
| 732 |
icon = IconsFactory.createGrayImage(b, icon); |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
else if (model.isPressed() && model.isArmed()) { |
| 737 |
icon = b.getPressedIcon(); |
| 738 |
if (icon == null) { |
| 739 |
// Use default icon |
| 740 |
icon = b.getIcon(); |
| 741 |
} |
| 742 |
} |
| 743 |
else { |
| 744 |
icon = b.getIcon(); |
| 745 |
} |
| 746 |
|
| 747 |
if (icon != null) { |
| 748 |
icon.paintIcon(b, g, iconRect.x, iconRect.y); |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
/** |
| 754 |
* Draws the background of the menu item. |
| 755 |
* |
| 756 |
* @param g the paint graphics |
| 757 |
* @param menuItem menu item to be painted |
| 758 |
* @param bgColor selection background color |
| 759 |
* @since 1.4 |
| 760 |
*/ |
| 761 |
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) { |
| 762 |
ButtonModel model = menuItem.getModel(); |
| 763 |
Color oldColor = g.getColor(); |
| 764 |
int menuWidth; |
| 765 |
int menuHeight; |
| 766 |
|
| 767 |
if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) { |
| 768 |
menuWidth = menuItem.getWidth(); |
| 769 |
menuHeight = menuItem.getHeight(); |
| 770 |
} |
| 771 |
else { |
| 772 |
menuWidth = menuItem.getHeight(); |
| 773 |
menuHeight = menuItem.getWidth(); |
| 774 |
} |
| 775 |
|
| 776 |
if (menuItem.isOpaque()) { |
| 777 |
if (menuItem.getBackground() instanceof UIResource) { |
| 778 |
g.setColor(backgroundColor); |
| 779 |
} |
| 780 |
else { |
| 781 |
g.setColor(menuItem.getBackground()); |
| 782 |
} |
| 783 |
g.fillRect(0, 0, menuWidth, menuHeight); |
| 784 |
|
| 785 |
if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) { |
| 786 |
g.setColor(bgColor); |
| 787 |
g.fillRect(1, 1, menuWidth - 2, menuHeight - 2); |
| 788 |
} |
| 789 |
g.setColor(oldColor); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
/** |
| 794 |
* Method which renders the text of the current menu item. |
| 795 |
* <p/> |
| 796 |
* |
| 797 |
* @param g Graphics context |
| 798 |
* @param menuItem Current menu item to render |
| 799 |
* @param textRect Bounding rectangle to render the text. |
| 800 |
* @param text String to render |
| 801 |
*/ |
| 802 |
protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) { |
| 803 |
// Note: This method is almost identical to the same method in WindowsMenuUI |
| 804 |
ButtonModel model = menuItem.getModel(); |
| 805 |
|
| 806 |
if (!model.isEnabled()) { |
| 807 |
// *** paint the text disabled |
| 808 |
WindowsGraphicsUtilsPort.paintText(g, menuItem, textRect, text, 0); |
| 809 |
} |
| 810 |
else { |
| 811 |
FontMetrics fm = g.getFontMetrics(); |
| 812 |
int mnemonicIndex = menuItem.getDisplayedMnemonicIndex(); |
| 813 |
// W2K Feature: Check to see if the Underscore should be rendered. |
| 814 |
if (LookAndFeelFactory.isMnemonicHidden()) { |
| 815 |
mnemonicIndex = -1; |
| 816 |
} |
| 817 |
|
| 818 |
Color oldColor = g.getColor(); |
| 819 |
|
| 820 |
// *** paint the text normally |
| 821 |
if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) { |
| 822 |
g.setColor(selectionForeground); // Uses protected field. |
| 823 |
} |
| 824 |
JideSwingUtilities.drawStringUnderlineCharAt(menuItem, g, text, |
| 825 |
mnemonicIndex, |
| 826 |
textRect.x, |
| 827 |
textRect.y + fm.getAscent()); |
| 828 |
g.setColor(oldColor); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
/* |
| 833 |
* Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly |
| 834 |
* clipped version of the compound labels string. Locations are computed relative to the viewRect rectangle. |
| 835 |
*/ |
| 836 |
|
| 837 |
private String layoutMenuItem(FontMetrics fm, |
| 838 |
String text, |
| 839 |
FontMetrics fmAccel, |
| 840 |
String acceleratorText, |
| 841 |
Icon icon, |
| 842 |
Icon checkIcon, |
| 843 |
Icon arrowIcon, |
| 844 |
int verticalAlignment, |
| 845 |
int horizontalAlignment, |
| 846 |
int verticalTextPosition, |
| 847 |
int horizontalTextPosition, |
| 848 |
Rectangle viewRect, |
| 849 |
Rectangle iconRect, |
| 850 |
Rectangle textRect, |
| 851 |
Rectangle acceleratorRect, |
| 852 |
Rectangle checkIconRect, |
| 853 |
Rectangle arrowIconRect, |
| 854 |
int textIconGap, |
| 855 |
int menuItemGap) { |
| 856 |
|
| 857 |
SwingUtilities.layoutCompoundLabel(menuItem, fm, text, icon, verticalAlignment, |
| 858 |
horizontalAlignment, verticalTextPosition, |
| 859 |
horizontalTextPosition, viewRect, iconRect, textRect, |
| 860 |
textIconGap); |
| 861 |
|
| 862 |
// get viewRect which is the bounds of menu item |
| 863 |
viewRect.x = viewRect.y = 0; |
| 864 |
if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) { |
| 865 |
// Dimension size = b.getSize(); |
| 866 |
viewRect.height = menuItem.getHeight(); |
| 867 |
viewRect.width = menuItem.getWidth(); |
| 868 |
} |
| 869 |
else { |
| 870 |
// Dimension size = b.getSize(); |
| 871 |
viewRect.height = menuItem.getWidth(); |
| 872 |
viewRect.width = menuItem.getHeight(); |
| 873 |
} |
| 874 |
|
| 875 |
/* Initialize the acceelratorText bounds rectangle textRect. If a null |
| 876 |
* or and empty String was specified we substitute "" here |
| 877 |
* and use 0,0,0,0 for acceleratorTextRect. |
| 878 |
*/ |
| 879 |
if ((acceleratorText == null) || acceleratorText.equals("")) { |
| 880 |
acceleratorRect.width = acceleratorRect.height = 0; |
| 881 |
acceleratorText = ""; |
| 882 |
} |
| 883 |
else { |
| 884 |
acceleratorRect.width = SwingUtilities.computeStringWidth(fmAccel, acceleratorText); |
| 885 |
acceleratorRect.height = fmAccel.getHeight(); |
| 886 |
} |
| 887 |
|
| 888 |
if ((text == null) || text.equals("")) { |
| 889 |
textRect.width = textRect.height = 0; |
| 890 |
text = ""; |
| 891 |
} |
| 892 |
else { |
| 893 |
boolean textIsEmpty = (text == null) || text.equals(""); |
| 894 |
int lsb = 0; |
| 895 |
|
| 896 |
View v = null; |
| 897 |
v = (menuItem != null) ? (View) menuItem.getClientProperty("html") : null; |
| 898 |
if (v != null) { |
| 899 |
textRect.width = (int) v.getPreferredSpan(View.X_AXIS); |
| 900 |
textRect.height = (int) v.getPreferredSpan(View.Y_AXIS); |
| 901 |
} |
| 902 |
else { |
| 903 |
textRect.width = SwingUtilities.computeStringWidth(fm, text); |
| 904 |
textRect.height = fm.getHeight(); |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
if (icon == null) { |
| 909 |
if (useCheckAndArrow()) |
| 910 |
iconRect.width = iconRect.height = 16; |
| 911 |
else |
| 912 |
iconRect.width = iconRect.height = 0; |
| 913 |
} |
| 914 |
else { |
| 915 |
iconRect.width = icon.getIconWidth(); |
| 916 |
iconRect.height = icon.getIconHeight(); |
| 917 |
} |
| 918 |
|
| 919 |
if (arrowIcon == null) { |
| 920 |
arrowIconRect.width = arrowIconRect.height = 0; |
| 921 |
} |
| 922 |
else { |
| 923 |
arrowIconRect.width = arrowIcon.getIconWidth(); |
| 924 |
arrowIconRect.height = arrowIcon.getIconHeight(); |
| 925 |
} |
| 926 |
|
| 927 |
if (checkIcon == null) { |
| 928 |
checkIconRect.width = checkIconRect.height = 0; |
| 929 |
} |
| 930 |
else { |
| 931 |
checkIconRect.width = checkIcon.getIconWidth(); |
| 932 |
checkIconRect.height = checkIcon.getIconHeight(); |
| 933 |
} |
| 934 |
|
| 935 |
if (menuItem.getComponentOrientation().isLeftToRight()) { |
| 936 |
// left a shadow for non-top level menu |
| 937 |
if (useCheckAndArrow()) { |
| 938 |
iconRect.x = (defaultShadowWidth - iconRect.width) >> 1; |
| 939 |
textRect.x = defaultShadowWidth + textIconGap; |
| 940 |
} |
| 941 |
else { |
| 942 |
if (icon != null) { |
| 943 |
iconRect.x = menuItem.getInsets().left; |
| 944 |
textRect.x = iconRect.x + iconRect.width + textIconGap; |
| 945 |
} |
| 946 |
else { |
| 947 |
textRect.x = menuItem.getInsets().left; |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
// Position the Accelerator text rect |
| 952 |
acceleratorRect.x = viewRect.x + viewRect.width - defaultAccelEndGap |
| 953 |
- acceleratorRect.width; |
| 954 |
// // Position the Check and Arrow Icons |
| 955 |
if (useCheckAndArrow()) { |
| 956 |
checkIconRect.x = (defaultShadowWidth - checkIconRect.width) >> 1; |
| 957 |
arrowIconRect.x = viewRect.x + viewRect.width - menuItemGap |
| 958 |
- arrowIconRect.width; |
| 959 |
} |
| 960 |
} |
| 961 |
else { |
| 962 |
// isLeftToRight is false |
| 963 |
} |
| 964 |
|
| 965 |
if (verticalTextPosition == SwingConstants.CENTER && verticalAlignment == SwingConstants.CENTER) { |
| 966 |
// put it in the middle |
| 967 |
textRect.y = ((viewRect.height - textRect.height) >> 1) + 1; |
| 968 |
iconRect.y = ((viewRect.height - iconRect.height) >> 1) + 1; |
| 969 |
} |
| 970 |
|
| 971 |
Rectangle labelRect = iconRect.union(textRect); |
| 972 |
|
| 973 |
// Align the accelerator text and the check and arrow icons vertically |
| 974 |
// with the center of the label rect. |
| 975 |
acceleratorRect.y = labelRect.y + (labelRect.height >> 1) - (acceleratorRect.height >> 1); |
| 976 |
|
| 977 |
if (useCheckAndArrow()) { |
| 978 |
arrowIconRect.y = ((viewRect.height - arrowIconRect.height) >> 1) + 1;// |
| 979 |
checkIconRect.y = ((viewRect.height - checkIconRect.height) >> 1) + 1;//labelRect.y + (labelRect.height / 2) - (checkIconRect.height / 2); |
| 980 |
} |
| 981 |
// |
| 982 |
/* |
| 983 |
System.out.println("Layout: text=" + menuItem.getText() + "\n\tv=" |
| 984 |
+ viewRect + "\n\tc=" + checkIconRect + "\n\ti=" |
| 985 |
+ iconRect + "\n\tt=" + textRect + "\n\tacc=" |
| 986 |
+ acceleratorRect + "\n\ta=" + arrowIconRect + "\n"); |
| 987 |
*/ |
| 988 |
|
| 989 |
return text; |
| 990 |
} |
| 991 |
|
| 992 |
/* |
| 993 |
* Returns false if the component is a JMenu and it is a top |
| 994 |
* level menu (on the menubar). |
| 995 |
*/ |
| 996 |
private boolean useCheckAndArrow() { |
| 997 |
boolean b = true; |
| 998 |
if ((menuItem instanceof JMenu) && |
| 999 |
(((JMenu) menuItem).isTopLevelMenu())) { |
| 1000 |
b = false; |
| 1001 |
} |
| 1002 |
return b; |
| 1003 |
} |
| 1004 |
|
| 1005 |
public MenuElement[] getPath() { |
| 1006 |
MenuSelectionManager m = MenuSelectionManager.defaultManager(); |
| 1007 |
MenuElement oldPath[] = m.getSelectedPath(); |
| 1008 |
MenuElement newPath[]; |
| 1009 |
int i = oldPath.length; |
| 1010 |
if (i == 0) |
| 1011 |
return new MenuElement[0]; |
| 1012 |
Component parent = menuItem.getParent(); |
| 1013 |
if (oldPath[i - 1].getComponent() == parent) { |
| 1014 |
// The parent popup menu is the last so far |
| 1015 |
newPath = new MenuElement[i + 1]; |
| 1016 |
System.arraycopy(oldPath, 0, newPath, 0, i); |
| 1017 |
newPath[i] = menuItem; |
| 1018 |
} |
| 1019 |
else { |
| 1020 |
// A sibling menu item is the current selection |
| 1021 |
// |
| 1022 |
// This probably needs to handle 'exit sub-menu into |
| 1023 |
// a menu item. Search backwards along the current |
| 1024 |
// selection until you find the parent popup menu, |
| 1025 |
// then copy up to that and add yourself... |
| 1026 |
int j; |
| 1027 |
for (j = oldPath.length - 1; j >= 0; j--) { |
| 1028 |
if (oldPath[j].getComponent() == parent) |
| 1029 |
break; |
| 1030 |
} |
| 1031 |
newPath = new MenuElement[j + 2]; |
| 1032 |
System.arraycopy(oldPath, 0, newPath, 0, j + 1); |
| 1033 |
newPath[j + 1] = menuItem; |
| 1034 |
/* |
| 1035 |
System.out.println("Sibling condition -- "); |
| 1036 |
System.out.println("Old array : "); |
| 1037 |
printMenuElementArray(oldPath, false); |
| 1038 |
System.out.println("New array : "); |
| 1039 |
printMenuElementArray(newPath, false); |
| 1040 |
*/ |
| 1041 |
} |
| 1042 |
return newPath; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* |
| 1046 |
void printMenuElementArray(MenuElement path[], boolean dumpStack) { |
| 1047 |
System.out.println("Path is("); |
| 1048 |
int i, j; |
| 1049 |
for (i = 0, j = path.length; i < j; i++) { |
| 1050 |
for (int k = 0; k <= i; k++) |
| 1051 |
System.out.print(" "); |
| 1052 |
MenuElement me = (MenuElement) path[i]; |
| 1053 |
if (me instanceof JMenuItem) |
| 1054 |
System.out.println(((JMenuItem) me).getText() + ", "); |
| 1055 |
else if (me == null) |
| 1056 |
System.out.println("NULL , "); |
| 1057 |
else |
| 1058 |
System.out.println("" + me + ", "); |
| 1059 |
} |
| 1060 |
System.out.println(")"); |
| 1061 |
|
| 1062 |
if (dumpStack == true) |
| 1063 |
Thread.dumpStack(); |
| 1064 |
} |
| 1065 |
|
| 1066 |
*/ |
| 1067 |
|
| 1068 |
protected class MouseInputHandler implements MouseInputListener { |
| 1069 |
public void mouseClicked(MouseEvent e) { |
| 1070 |
} |
| 1071 |
|
| 1072 |
public void mousePressed(MouseEvent e) { |
| 1073 |
} |
| 1074 |
|
| 1075 |
public void mouseReleased(MouseEvent e) { |
| 1076 |
if (!SwingUtilities.isLeftMouseButton(e)) { |
| 1077 |
return; |
| 1078 |
} |
| 1079 |
|
| 1080 |
if (menuItem != null && menuItem.isEnabled()) { |
| 1081 |
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
| 1082 |
Point p = e.getPoint(); |
| 1083 |
if (p.x >= 0 && p.x < menuItem.getWidth() && |
| 1084 |
p.y >= 0 && p.y < menuItem.getHeight()) { |
| 1085 |
doClick(manager); |
| 1086 |
} |
| 1087 |
else { |
| 1088 |
manager.processMouseEvent(e); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
public void mouseEntered(MouseEvent e) { |
| 1094 |
if (menuItem != null && menuItem.isEnabled()) { |
| 1095 |
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
| 1096 |
int modifiers = e.getModifiers(); |
| 1097 |
// 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2 |
| 1098 |
if ((modifiers & (InputEvent.BUTTON1_MASK | |
| 1099 |
InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) != 0) { |
| 1100 |
MenuSelectionManager.defaultManager().processMouseEvent(e); |
| 1101 |
} |
| 1102 |
else { |
| 1103 |
manager.setSelectedPath(getPath()); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
public void mouseExited(MouseEvent e) { |
| 1109 |
if (menuItem != null && menuItem.isEnabled()) { |
| 1110 |
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
| 1111 |
|
| 1112 |
int modifiers = e.getModifiers(); |
| 1113 |
// 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2 |
| 1114 |
if ((modifiers & (InputEvent.BUTTON1_MASK | |
| 1115 |
InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) != 0) { |
| 1116 |
MenuSelectionManager.defaultManager().processMouseEvent(e); |
| 1117 |
} |
| 1118 |
else { |
| 1119 |
|
| 1120 |
MenuElement path[] = manager.getSelectedPath(); |
| 1121 |
if (path.length > 1) { |
| 1122 |
MenuElement newPath[] = new MenuElement[path.length - 1]; |
| 1123 |
int i, c; |
| 1124 |
for (i = 0, c = path.length - 1; i < c; i++) |
| 1125 |
newPath[i] = path[i]; |
| 1126 |
manager.setSelectedPath(newPath); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
public void mouseDragged(MouseEvent e) { |
| 1133 |
MenuSelectionManager.defaultManager().processMouseEvent(e); |
| 1134 |
} |
| 1135 |
|
| 1136 |
public void mouseMoved(MouseEvent e) { |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
private class MenuDragMouseHandler implements MenuDragMouseListener { |
| 1142 |
public void menuDragMouseEntered(MenuDragMouseEvent e) { |
| 1143 |
} |
| 1144 |
|
| 1145 |
public void menuDragMouseDragged(MenuDragMouseEvent e) { |
| 1146 |
if (menuItem != null && menuItem.isEnabled()) { |
| 1147 |
MenuSelectionManager manager = e.getMenuSelectionManager(); |
| 1148 |
MenuElement path[] = e.getPath(); |
| 1149 |
manager.setSelectedPath(path); |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
public void menuDragMouseExited(MenuDragMouseEvent e) { |
| 1154 |
} |
| 1155 |
|
| 1156 |
public void menuDragMouseReleased(MenuDragMouseEvent e) { |
| 1157 |
if (menuItem != null && menuItem.isEnabled()) { |
| 1158 |
MenuSelectionManager manager = e.getMenuSelectionManager(); |
| 1159 |
Point p = e.getPoint(); |
| 1160 |
if (p.x >= 0 && p.x < menuItem.getWidth() && |
| 1161 |
p.y >= 0 && p.y < menuItem.getHeight()) { |
| 1162 |
doClick(manager); |
| 1163 |
} |
| 1164 |
else { |
| 1165 |
manager.clearSelectedPath(); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
private class MenuKeyHandler implements MenuKeyListener { |
| 1172 |
|
| 1173 |
/** |
| 1174 |
* Handles the mnemonic key typed in the MenuItem if this menuItem is in a standalone popup menu. This |
| 1175 |
* invocation normally handled in BasicMenuUI.MenuKeyHandler.menuKeyPressed. Ideally, the MenuKeyHandlers for |
| 1176 |
* both BasicMenuItemUI and BasicMenuUI can be consolidated into BasicPopupMenuUI but that would require an |
| 1177 |
* semantic change. This would result in a performance win since we can shortcut a lot of the needless |
| 1178 |
* processing from MenuSelectionManager.processKeyEvent(). See 4670831. |
| 1179 |
*/ |
| 1180 |
public void menuKeyTyped(MenuKeyEvent e) { |
| 1181 |
if (menuItem != null && menuItem.isEnabled()) { |
| 1182 |
int key = menuItem.getMnemonic(); |
| 1183 |
if (key == 0 || e.getPath().length != 2) // Hack! Only proceed if in a JPopupMenu |
| 1184 |
return; |
| 1185 |
if (lower((char) key) == lower(e.getKeyChar())) { |
| 1186 |
MenuSelectionManager manager = |
| 1187 |
e.getMenuSelectionManager(); |
| 1188 |
doClick(manager); |
| 1189 |
e.consume(); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
public void menuKeyPressed(MenuKeyEvent e) { |
| 1195 |
} |
| 1196 |
|
| 1197 |
public void menuKeyReleased(MenuKeyEvent e) { |
| 1198 |
} |
| 1199 |
|
| 1200 |
private char lower(char keyChar) { |
| 1201 |
return Character.toLowerCase(keyChar); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
private class PropertyChangeHandler implements PropertyChangeListener { |
| 1206 |
public void propertyChange(PropertyChangeEvent e) { |
| 1207 |
String name = e.getPropertyName(); |
| 1208 |
|
| 1209 |
if (name.equals("labelFor") || name.equals("displayedMnemonic") || |
| 1210 |
name.equals("accelerator")) { |
| 1211 |
updateAcceleratorBinding(); |
| 1212 |
} |
| 1213 |
else if (ButtonStyle.BUTTON_STYLE_PROPERTY.equals(name) |
| 1214 |
|| "opaque".equals(name) |
| 1215 |
|| AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY.equals(name) |
| 1216 |
) { |
| 1217 |
if (e.getSource() instanceof JMenuItem) { |
| 1218 |
JMenuItem lbl = ((JMenuItem) e.getSource()); |
| 1219 |
lbl.repaint(); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
else if (name.equals("text") || "font".equals(name) || |
| 1223 |
"foreground".equals(name)) { |
| 1224 |
// remove the old html view client property if one |
| 1225 |
// existed, and install a new one if the text installed |
| 1226 |
// into the JLabel is html source. |
| 1227 |
JMenuItem lbl = ((JMenuItem) e.getSource()); |
| 1228 |
String text = lbl.getText(); |
| 1229 |
BasicHTML.updateRenderer(lbl, text); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
private static class ClickAction extends AbstractAction { |
| 1235 |
private static final long serialVersionUID = -3086149185380600185L; |
| 1236 |
|
| 1237 |
public void actionPerformed(ActionEvent e) { |
| 1238 |
JMenuItem mi = (JMenuItem) e.getSource(); |
| 1239 |
MenuSelectionManager.defaultManager().clearSelectedPath(); |
| 1240 |
mi.doClick(); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
/** |
| 1245 |
* Call this method when a menu item is to be activated. This method handles some of the details of menu item |
| 1246 |
* activation such as clearing the selected path and messaging the JMenuItem's doClick() method. |
| 1247 |
* |
| 1248 |
* @param msm A MenuSelectionManager. The visual feedback and internal bookkeeping tasks are delegated to this |
| 1249 |
* MenuSelectionManager. If <code>null</code> is passed as this argument, the |
| 1250 |
* <code>MenuSelectionManager.defaultManager</code> is used. |
| 1251 |
* @see MenuSelectionManager |
| 1252 |
* @see JMenuItem#doClick(int) |
| 1253 |
* @since 1.4 |
| 1254 |
*/ |
| 1255 |
protected void doClick(MenuSelectionManager msm) { |
| 1256 |
// Auditory cue |
| 1257 |
// if (!isInternalFrameSystemMenu()) { |
| 1258 |
// ActionMap map = menuItem.getActionMap(); |
| 1259 |
// if (map != null) { |
| 1260 |
// Action audioAction = map.get(getPropertyPrefix() + |
| 1261 |
// ".commandSound"); |
| 1262 |
// if (audioAction != null) { |
| 1263 |
// pass off firing the Action to a utility method |
| 1264 |
// BasicLookAndFeel lf = (BasicLookAndFeel) |
| 1265 |
// UIManager.getLookAndFeel(); |
| 1266 |
// lf.playSound(audioAction); |
| 1267 |
// } |
| 1268 |
// } |
| 1269 |
// } |
| 1270 |
// // Visual feedback |
| 1271 |
if (msm == null) { |
| 1272 |
msm = MenuSelectionManager.defaultManager(); |
| 1273 |
} |
| 1274 |
msm.clearSelectedPath(); |
| 1275 |
menuItem.doClick(0); |
| 1276 |
} |
| 1277 |
|
| 1278 |
public ThemePainter getPainter() { |
| 1279 |
return _painter; |
| 1280 |
} |
| 1281 |
|
| 1282 |
protected boolean isDownArrowVisible(Container c) { |
| 1283 |
if (c instanceof TopLevelMenuContainer && ((TopLevelMenuContainer) c).isMenuBar()) { |
| 1284 |
return false; |
| 1285 |
} |
| 1286 |
else if (c instanceof TopLevelMenuContainer && !((TopLevelMenuContainer) c).isMenuBar()) { |
| 1287 |
return true; |
| 1288 |
} |
| 1289 |
else if (c instanceof JMenuBar) { |
| 1290 |
return false; |
| 1291 |
} |
| 1292 |
else { |
| 1293 |
return true; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
} |