35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
|
|
|
38 |
package javax.swing.plaf.basic; |
package javax.swing.plaf.basic; |
39 |
|
|
40 |
import java.awt.Color; |
import java.awt.Color; |
151 |
protected Color selectionBackground; |
protected Color selectionBackground; |
152 |
|
|
153 |
/** |
/** |
154 |
* Color of the background that is used when menu item is selected. |
* Color of the text that is used when menu item is selected. |
155 |
*/ |
*/ |
156 |
protected Color selectionForeground; |
protected Color selectionForeground; |
157 |
|
|
275 |
public MenuElement[] getPath() |
public MenuElement[] getPath() |
276 |
{ |
{ |
277 |
ArrayList path = new ArrayList(); |
ArrayList path = new ArrayList(); |
278 |
|
|
279 |
|
// Path to menu should also include its popup menu. |
280 |
|
if (menuItem instanceof JMenu) |
281 |
|
path.add(((JMenu) menuItem).getPopupMenu()); |
282 |
|
|
283 |
Component c = menuItem; |
Component c = menuItem; |
284 |
while (c instanceof MenuElement) |
while (c instanceof MenuElement) |
285 |
{ |
{ |
310 |
Icon arrowIcon, |
Icon arrowIcon, |
311 |
int defaultTextIconGap) |
int defaultTextIconGap) |
312 |
{ |
{ |
|
// FIXME: Need to implement. |
|
|
return null; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Returns preferred size of the given component |
|
|
* |
|
|
* @param c component for which to return preferred size |
|
|
* |
|
|
* @return $Dimension$ preferred size for the given component |
|
|
*/ |
|
|
public Dimension getPreferredSize(JComponent c) |
|
|
{ |
|
313 |
JMenuItem m = (JMenuItem) c; |
JMenuItem m = (JMenuItem) c; |
314 |
Dimension d = BasicGraphicsUtils.getPreferredButtonSize(m, |
Dimension d = BasicGraphicsUtils.getPreferredButtonSize(m, |
315 |
defaultTextIconGap); |
defaultTextIconGap); |
352 |
} |
} |
353 |
|
|
354 |
/** |
/** |
355 |
* DOCUMENT ME! |
* Returns preferred size of the given component |
356 |
|
* |
357 |
|
* @param c component for which to return preferred size |
358 |
* |
* |
359 |
* @return $returnType$ DOCUMENT ME! |
* @return $Dimension$ preferred size for the given component |
360 |
*/ |
*/ |
361 |
|
public Dimension getPreferredSize(JComponent c) |
362 |
|
{ |
363 |
|
return getPreferredMenuItemSize(c, checkIcon, arrowIcon, defaultTextIconGap); |
364 |
|
} |
365 |
|
|
366 |
protected String getPropertyPrefix() |
protected String getPropertyPrefix() |
367 |
{ |
{ |
368 |
return null; |
return null; |
414 |
protected void installListeners() |
protected void installListeners() |
415 |
{ |
{ |
416 |
menuItem.addMouseListener(mouseInputListener); |
menuItem.addMouseListener(mouseInputListener); |
417 |
|
menuItem.addMouseMotionListener(mouseInputListener); |
418 |
menuItem.addMenuDragMouseListener(menuDragMouseListener); |
menuItem.addMenuDragMouseListener(menuDragMouseListener); |
419 |
menuItem.addMenuKeyListener(menuKeyListener); |
menuItem.addMenuKeyListener(menuKeyListener); |
420 |
menuItem.addPropertyChangeListener(propertyChangeListener); |
menuItem.addPropertyChangeListener(propertyChangeListener); |
432 |
super.installUI(c); |
super.installUI(c); |
433 |
menuItem = (JMenuItem) c; |
menuItem = (JMenuItem) c; |
434 |
installDefaults(); |
installDefaults(); |
435 |
|
installComponents(menuItem); |
436 |
installListeners(); |
installListeners(); |
437 |
} |
} |
438 |
|
|
516 |
{ |
{ |
517 |
if (m.isContentAreaFilled()) |
if (m.isContentAreaFilled()) |
518 |
{ |
{ |
519 |
g.setColor(m.getBackground().darker()); |
g.setColor(selectionBackground); |
520 |
g.fillRect(br.x, br.y, br.width, br.height); |
g.fillRect(br.x, br.y, br.width, br.height); |
521 |
} |
} |
522 |
} |
} |
529 |
} |
} |
530 |
} |
} |
531 |
|
|
532 |
|
// If this menu item is a JCheckBoxMenuItem then paint check icon |
533 |
if (checkIcon != null) |
if (checkIcon != null) |
534 |
{ |
{ |
535 |
SwingUtilities.layoutCompoundLabel(m, fm, null, checkIcon, vertAlign, |
SwingUtilities.layoutCompoundLabel(m, fm, null, checkIcon, vertAlign, |
544 |
vr.x = cr.x + cr.width + defaultTextIconGap; |
vr.x = cr.x + cr.width + defaultTextIconGap; |
545 |
} |
} |
546 |
|
|
547 |
|
// if this is a submenu, then paint arrow icon to indicate it. |
548 |
if (arrowIcon != null && (c instanceof JMenu)) |
if (arrowIcon != null && (c instanceof JMenu)) |
549 |
{ |
{ |
550 |
if (! ((JMenu) c).isTopLevelMenu()) |
if (! ((JMenu) c).isTopLevelMenu()) |
557 |
} |
} |
558 |
} |
} |
559 |
|
|
|
// paint text and user menu icon if it exists |
|
|
SwingUtilities.layoutCompoundLabel(c, fm, m.getText(), m.getIcon(), |
|
|
vertAlign, horAlign, vertTextPos, |
|
|
horTextPos, vr, ir, tr, |
|
|
defaultTextIconGap); |
|
|
|
|
|
paintText(g, m, tr, m.getText()); |
|
|
|
|
560 |
// paint icon |
// paint icon |
561 |
// FIXME: should paint different icon at different button state's. |
// FIXME: should paint different icon at different button state's. |
562 |
// i.e disabled icon when button is disabled.. etc. |
// i.e disabled icon when button is disabled.. etc. |
|
|
|
|
/* |
|
563 |
Icon i = m.getIcon(); |
Icon i = m.getIcon(); |
564 |
if (i != null) |
if (i != null) |
565 |
{ |
{ |
566 |
int x = ir.x; |
i.paintIcon(c, g, vr.x, vr.y); |
567 |
int y = ir.y; |
|
568 |
i.paintIcon(c, g, x, y); |
// Adjust view rectangle, s.t text would be drawn after menu item's icon. |
569 |
|
vr.x += i.getIconWidth() + defaultTextIconGap; |
570 |
} |
} |
571 |
*/ |
|
572 |
|
// paint text and user menu icon if it exists |
573 |
|
SwingUtilities.layoutCompoundLabel(c, fm, m.getText(), m.getIcon(), |
574 |
|
vertAlign, horAlign, vertTextPos, |
575 |
|
horTextPos, vr, ir, tr, |
576 |
|
defaultTextIconGap); |
577 |
|
|
578 |
|
paintText(g, m, tr, m.getText()); |
579 |
|
|
580 |
// paint accelerator |
// paint accelerator |
581 |
String acceleratorText = ""; |
String acceleratorText = ""; |
612 |
Font f = menuItem.getFont(); |
Font f = menuItem.getFont(); |
613 |
g.setFont(f); |
g.setFont(f); |
614 |
FontMetrics fm = g.getFontMetrics(f); |
FontMetrics fm = g.getFontMetrics(f); |
|
g.setColor(menuItem.getForeground()); |
|
615 |
|
|
616 |
|
if (text != null && ! text.equals("")) |
617 |
|
{ |
618 |
|
if (menuItem.isEnabled()) |
619 |
|
g.setColor(menuItem.getForeground()); |
620 |
|
else |
621 |
|
// FIXME: should fix this to use 'disabledForeground', but its |
622 |
|
// default value in BasicLookAndFeel is null. |
623 |
|
g.setColor(Color.gray); |
624 |
|
|
625 |
|
int mnemonicIndex = menuItem.getDisplayedMnemonicIndex(); |
626 |
|
|
627 |
|
if (mnemonicIndex != -1) |
628 |
|
BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemonicIndex, |
629 |
|
textRect.x, |
630 |
|
textRect.y |
631 |
|
+ fm.getAscent()); |
632 |
|
else |
633 |
BasicGraphicsUtils.drawString(g, text, 0, textRect.x, |
BasicGraphicsUtils.drawString(g, text, 0, textRect.x, |
634 |
textRect.y + fm.getAscent()); |
textRect.y + fm.getAscent()); |
635 |
} |
} |
636 |
|
} |
637 |
|
|
638 |
/** |
/** |
639 |
* This method uninstalls the components for this {@link JMenuItem}. |
* This method uninstalls the components for this {@link JMenuItem}. |
699 |
{ |
{ |
700 |
uninstallListeners(); |
uninstallListeners(); |
701 |
uninstallDefaults(); |
uninstallDefaults(); |
702 |
|
uninstallComponents(menuItem); |
703 |
menuItem = null; |
menuItem = null; |
704 |
} |
} |
705 |
|
|
769 |
{ |
{ |
770 |
g.setFont(acceleratorFont); |
g.setFont(acceleratorFont); |
771 |
FontMetrics fm = g.getFontMetrics(acceleratorFont); |
FontMetrics fm = g.getFontMetrics(acceleratorFont); |
772 |
|
|
773 |
|
if (menuItem.isEnabled()) |
774 |
g.setColor(acceleratorForeground); |
g.setColor(acceleratorForeground); |
775 |
|
else |
776 |
|
// FIXME: should fix this to use 'disabledForeground', but its |
777 |
|
// default value in BasicLookAndFeel is null. |
778 |
|
g.setColor(Color.gray); |
779 |
|
|
780 |
BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x, |
BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x, |
781 |
acceleratorRect.y + fm.getAscent()); |
acceleratorRect.y + fm.getAscent()); |
782 |
} |
} |
884 |
*/ |
*/ |
885 |
public void mouseReleased(MouseEvent e) |
public void mouseReleased(MouseEvent e) |
886 |
{ |
{ |
887 |
Rectangle size = menuItem.getBounds(); //this.getParent().getSize(); |
Rectangle size = menuItem.getBounds(); |
888 |
|
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
889 |
if (e.getX() > 0 && e.getX() < size.width && e.getY() > 0 |
if (e.getX() > 0 && e.getX() < size.width && e.getY() > 0 |
890 |
&& e.getY() < size.height) |
&& e.getY() < size.height) |
891 |
{ |
{ |
|
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
|
892 |
manager.clearSelectedPath(); |
manager.clearSelectedPath(); |
893 |
menuItem.doClick(0); |
menuItem.doClick(); |
894 |
} |
} |
895 |
|
|
896 |
|
else |
897 |
|
manager.processMouseEvent(e); |
898 |
} |
} |
899 |
} |
} |
900 |
|
|
910 |
*/ |
*/ |
911 |
public void menuDragMouseDragged(MenuDragMouseEvent e) |
public void menuDragMouseDragged(MenuDragMouseEvent e) |
912 |
{ |
{ |
913 |
|
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
914 |
|
manager.setSelectedPath(e.getPath()); |
915 |
} |
} |
916 |
|
|
917 |
/** |
/** |
922 |
*/ |
*/ |
923 |
public void menuDragMouseEntered(MenuDragMouseEvent e) |
public void menuDragMouseEntered(MenuDragMouseEvent e) |
924 |
{ |
{ |
925 |
|
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
926 |
|
manager.setSelectedPath(e.getPath()); |
927 |
} |
} |
928 |
|
|
929 |
/** |
/** |
944 |
*/ |
*/ |
945 |
public void menuDragMouseReleased(MenuDragMouseEvent e) |
public void menuDragMouseReleased(MenuDragMouseEvent e) |
946 |
{ |
{ |
947 |
|
MenuElement[] path = e.getPath(); |
948 |
|
|
949 |
|
if (path[path.length - 1] instanceof JMenuItem) |
950 |
|
((JMenuItem) path[path.length - 1]).doClick(); |
951 |
|
|
952 |
|
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
953 |
|
manager.clearSelectedPath(); |
954 |
} |
} |
955 |
} |
} |
956 |
|
|
1002 |
*/ |
*/ |
1003 |
public void propertyChange(PropertyChangeEvent evt) |
public void propertyChange(PropertyChangeEvent evt) |
1004 |
{ |
{ |
1005 |
|
menuItem.revalidate(); |
1006 |
|
menuItem.repaint(); |
1007 |
} |
} |
1008 |
} |
} |
1009 |
} |
} |