1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84: import ;
85: import ;
86: import ;
87: import ;
88: import ;
89: import ;
90: import ;
91: import ;
92: import ;
93:
94:
97: public class BasicMenuItemUI extends MenuItemUI
98: {
99:
102: protected Font acceleratorFont;
103:
104:
107: protected Color acceleratorForeground;
108:
109:
113: protected Color acceleratorSelectionForeground;
114:
115:
119: protected Icon arrowIcon;
120:
121:
125: protected Icon checkIcon;
126:
127:
130: protected int defaultTextIconGap = 4;
131:
132:
135: protected Color disabledForeground;
136:
137:
140: protected MenuDragMouseListener menuDragMouseListener;
141:
142:
145: protected JMenuItem menuItem;
146:
147:
150: protected MenuKeyListener menuKeyListener;
151:
152:
155: protected MouseInputListener mouseInputListener;
156:
157:
160: protected boolean oldBorderPainted;
161:
162:
165: protected Color selectionBackground;
166:
167:
170: protected Color selectionForeground;
171:
172:
175: private String acceleratorDelimiter;
176:
177:
180: private ItemListener itemListener;
181:
182:
185: private int defaultAcceleratorLabelGap = 10;
186:
187:
190: private int MenuGap = 10;
191:
192:
193: PropertyChangeHandler propertyChangeListener;
194:
195:
198: private Rectangle viewRect;
199:
200:
203: private Rectangle textRect;
204:
205:
208: private Rectangle accelRect;
209:
210:
213: private Rectangle iconRect;
214:
215:
218: private Rectangle arrowIconRect;
219:
220:
223: private Rectangle checkIconRect;
224:
225:
229: private Rectangle cachedRect;
230:
231:
235: class PropertyChangeHandler implements PropertyChangeListener
236: {
237:
244: public void propertyChange(PropertyChangeEvent e)
245: {
246: String property = e.getPropertyName();
247: if (property.equals("accelerator"))
248: {
249: InputMap map = SwingUtilities.getUIInputMap(menuItem,
250: JComponent.WHEN_IN_FOCUSED_WINDOW);
251: if (map != null)
252: map.remove((KeyStroke) e.getOldValue());
253: else
254: map = new ComponentInputMapUIResource(menuItem);
255:
256: KeyStroke accelerator = (KeyStroke) e.getNewValue();
257: if (accelerator != null)
258: map.put(accelerator, "doClick");
259: }
260:
261: else if ((property.equals(AbstractButton.TEXT_CHANGED_PROPERTY)
262: || property.equals("font"))
263: && SystemProperties.getProperty("gnu.javax.swing.noGraphics2D")
264: == null)
265:
266: {
267: AbstractButton b = (AbstractButton) e.getSource();
268: String text = b.getText();
269: if (text == null)
270: text = "";
271: FontRenderContext frc = new FontRenderContext(new AffineTransform(),
272: false, false);
273: TextLayout layout = new TextLayout(text, b.getFont(), frc);
274: b.putClientProperty(BasicGraphicsUtils.CACHED_TEXT_LAYOUT, layout);
275: }
276: }
277: }
278:
279:
285: class ClickAction extends AbstractAction
286: {
287:
291: public void actionPerformed(ActionEvent event)
292: {
293: doClick(MenuSelectionManager.defaultManager());
294: }
295: }
296:
297:
300: public BasicMenuItemUI()
301: {
302: mouseInputListener = createMouseInputListener(menuItem);
303: menuDragMouseListener = createMenuDragMouseListener(menuItem);
304: menuKeyListener = createMenuKeyListener(menuItem);
305: itemListener = new ItemHandler();
306: propertyChangeListener = new PropertyChangeHandler();
307:
308:
309: viewRect = new Rectangle();
310: textRect = new Rectangle();
311: iconRect = new Rectangle();
312: arrowIconRect = new Rectangle();
313: checkIconRect = new Rectangle();
314: accelRect = new Rectangle();
315: cachedRect = new Rectangle();
316: }
317:
318:
325: protected MenuDragMouseListener createMenuDragMouseListener(JComponent c)
326: {
327: return new MenuDragMouseHandler();
328: }
329:
330:
338: protected MenuKeyListener createMenuKeyListener(JComponent c)
339: {
340: return new MenuKeyHandler();
341: }
342:
343:
350: protected MouseInputListener createMouseInputListener(JComponent c)
351: {
352: return new MouseInputHandler();
353: }
354:
355:
363: public static ComponentUI createUI(JComponent c)
364: {
365: return new BasicMenuItemUI();
366: }
367:
368:
374: protected void doClick(MenuSelectionManager msm)
375: {
376: menuItem.doClick();
377: msm.clearSelectedPath();
378: }
379:
380:
387: public Dimension getMaximumSize(JComponent c)
388: {
389: return null;
390: }
391:
392:
399: public Dimension getMinimumSize(JComponent c)
400: {
401: return null;
402: }
403:
404:
410: public MenuElement[] getPath()
411: {
412: ArrayList path = new ArrayList();
413:
414:
415: if (menuItem instanceof JMenu)
416: path.add(((JMenu) menuItem).getPopupMenu());
417:
418: Component c = menuItem;
419: while (c instanceof MenuElement)
420: {
421: path.add(0, (MenuElement) c);
422:
423: if (c instanceof JPopupMenu)
424: c = ((JPopupMenu) c).getInvoker();
425: else
426: c = c.getParent();
427: }
428:
429: MenuElement[] pathArray = new MenuElement[path.size()];
430: path.toArray(pathArray);
431: return pathArray;
432: }
433:
434:
447: protected Dimension getPreferredMenuItemSize(JComponent c, Icon checkIcon,
448: Icon arrowIcon,
449: int defaultTextIconGap)
450: {
451: JMenuItem m = (JMenuItem) c;
452: String accelText = getAcceleratorString(m);
453:
454:
455:
456: layoutMenuItem(m, accelText);
457:
458:
459: cachedRect.setBounds(textRect);
460: Rectangle pref = SwingUtilities.computeUnion(iconRect.x, iconRect.y,
461: iconRect.width,
462: iconRect.height,
463: cachedRect);
464:
465:
466:
467:
468:
469: Container parent = m.getParent();
470: if (parent != null && parent instanceof JComponent
471: && !(m instanceof JMenu && ((JMenu) m).isTopLevelMenu()))
472: {
473: JComponent p = (JComponent) parent;
474:
475:
476: Integer maxTextWidth = (Integer) p.getClientProperty("maxTextWidth");
477: int maxTextValue = maxTextWidth == null ? 0 : maxTextWidth.intValue();
478: if (pref.width < maxTextValue)
479: pref.width = maxTextValue;
480: else
481: p.putClientProperty("maxTextWidth", new Integer(pref.width));
482:
483:
484: Integer maxAccelWidth = (Integer) p.getClientProperty("maxAccelWidth");
485: int maxAccelValue = maxAccelWidth == null ? 0
486: : maxAccelWidth.intValue();
487: if (accelRect.width > maxAccelValue)
488: {
489: maxAccelValue = accelRect.width;
490: p.putClientProperty("maxAccelWidth", new Integer(accelRect.width));
491: }
492: pref.width += maxAccelValue;
493: pref.width += defaultTextIconGap;
494: }
495:
496:
497: if (! (m instanceof JMenu && ((JMenu) m).isTopLevelMenu()))
498: {
499: pref.width += checkIconRect.width;
500: pref.width += defaultTextIconGap;
501: pref.width += arrowIconRect.width;
502: pref.width += defaultTextIconGap;
503: }
504:
505:
506: pref.width += 2 * defaultTextIconGap;
507:
508:
509: Insets i = m.getInsets();
510: pref.width += i.left + i.right;
511: pref.height += i.top + i.bottom;
512:
513:
514: return pref.getSize();
515: }
516:
517:
524: public Dimension getPreferredSize(JComponent c)
525: {
526: return getPreferredMenuItemSize(c, checkIcon, arrowIcon,
527: defaultTextIconGap);
528: }
529:
530:
535: protected String getPropertyPrefix()
536: {
537: return "MenuItem";
538: }
539:
540:
546: protected void installComponents(JMenuItem menuItem)
547: {
548:
549: }
550:
551:
555: protected void installDefaults()
556: {
557: String prefix = getPropertyPrefix();
558: LookAndFeel.installBorder(menuItem, prefix + ".border");
559: LookAndFeel.installColorsAndFont(menuItem, prefix + ".background",
560: prefix + ".foreground", prefix + ".font");
561: menuItem.setMargin(UIManager.getInsets(prefix + ".margin"));
562: acceleratorFont = UIManager.getFont(prefix + ".acceleratorFont");
563: acceleratorForeground = UIManager.getColor(prefix
564: + ".acceleratorForeground");
565: acceleratorSelectionForeground = UIManager.getColor(prefix
566: + ".acceleratorSelectionForeground");
567: selectionBackground = UIManager.getColor(prefix + ".selectionBackground");
568: selectionForeground = UIManager.getColor(prefix + ".selectionForeground");
569: acceleratorDelimiter = UIManager.getString(prefix + ".acceleratorDelimiter");
570: checkIcon = UIManager.getIcon(prefix + ".checkIcon");
571:
572: menuItem.setHorizontalTextPosition(SwingConstants.TRAILING);
573: menuItem.setHorizontalAlignment(SwingConstants.LEADING);
574: }
575:
576:
579: protected void installKeyboardActions()
580: {
581: InputMap focusedWindowMap = SwingUtilities.getUIInputMap(menuItem,
582: JComponent.WHEN_IN_FOCUSED_WINDOW);
583: if (focusedWindowMap == null)
584: focusedWindowMap = new ComponentInputMapUIResource(menuItem);
585: KeyStroke accelerator = menuItem.getAccelerator();
586: if (accelerator != null)
587: focusedWindowMap.put(accelerator, "doClick");
588: SwingUtilities.replaceUIInputMap(menuItem,
589: JComponent.WHEN_IN_FOCUSED_WINDOW, focusedWindowMap);
590:
591: ActionMap UIActionMap = SwingUtilities.getUIActionMap(menuItem);
592: if (UIActionMap == null)
593: UIActionMap = new ActionMapUIResource();
594: UIActionMap.put("doClick", new ClickAction());
595: SwingUtilities.replaceUIActionMap(menuItem, UIActionMap);
596: }
597:
598:
601: protected void installListeners()
602: {
603: menuItem.addMouseListener(mouseInputListener);
604: menuItem.addMouseMotionListener(mouseInputListener);
605: menuItem.addMenuDragMouseListener(menuDragMouseListener);
606: menuItem.addMenuKeyListener(menuKeyListener);
607: menuItem.addItemListener(itemListener);
608: menuItem.addPropertyChangeListener(propertyChangeListener);
609: }
610:
611:
619: public void installUI(JComponent c)
620: {
621: super.installUI(c);
622: menuItem = (JMenuItem) c;
623: installDefaults();
624: installComponents(menuItem);
625: installListeners();
626: installKeyboardActions();
627: }
628:
629:
637: public void paint(Graphics g, JComponent c)
638: {
639: paintMenuItem(g, c, checkIcon, arrowIcon, selectionBackground,
640: c.getForeground(), defaultTextIconGap);
641: }
642:
643:
653: protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor)
654: {
655:
656:
657: ButtonModel mod = menuItem.getModel();
658: Color saved = g.getColor();
659: if (mod.isArmed() || ((menuItem instanceof JMenu) && mod.isSelected()))
660: {
661: g.setColor(bgColor);
662: g.fillRect(0, 0, menuItem.getWidth(), menuItem.getHeight());
663: }
664: else if (menuItem.isOpaque())
665: {
666: g.setColor(menuItem.getBackground());
667: g.fillRect(0, 0, menuItem.getWidth(), menuItem.getHeight());
668: }
669: g.setColor(saved);
670: }
671:
672:
690: protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon,
691: Icon arrowIcon, Color background,
692: Color foreground, int defaultTextIconGap)
693: {
694: JMenuItem m = (JMenuItem) c;
695:
696:
697: Font oldFont = g.getFont();
698: Font font = c.getFont();
699: g.setFont(font);
700: FontMetrics accelFm = m.getFontMetrics(acceleratorFont);
701:
702:
703: String accelText = getAcceleratorString(m);
704:
705:
706:
707: layoutMenuItem(m, accelText);
708:
709:
710: paintBackground(g, m, background);
711:
712: Color oldColor = g.getColor();
713:
714:
715: if (checkIcon != null)
716: {
717: checkIcon.paintIcon(m, g, checkIconRect.x, checkIconRect.y);
718: }
719:
720:
721: ButtonModel model = m.getModel();
722: if (m.getIcon() != null)
723: {
724:
725:
726: Icon icon;
727: if (! m.isEnabled())
728: {
729: icon = m.getDisabledIcon();
730: }
731: else if (model.isPressed() && model.isArmed())
732: {
733: icon = m.getPressedIcon();
734: if (icon == null)
735: {
736: icon = m.getIcon();
737: }
738: }
739: else
740: {
741: icon = m.getIcon();
742: }
743:
744: if (icon != null)
745: {
746: icon.paintIcon(m, g, iconRect.x, iconRect.y);
747: }
748: }
749:
750:
751: String text = m.getText();
752: if (text != null)
753: {
754:
755: View html = (View) m.getClientProperty(BasicHTML.propertyKey);
756: if (html != null)
757: {
758: html.paint(g, textRect);
759: }
760: else
761: {
762: paintText(g, m, textRect, text);
763: }
764: }
765:
766:
767: if (! accelText.equals(""))
768: {
769:
770:
771:
772:
773: int accelOffset = 0;
774: Container parent = m.getParent();
775: if (parent != null && parent instanceof JComponent)
776: {
777: JComponent p = (JComponent) parent;
778: Integer maxAccelWidth =
779: (Integer) p.getClientProperty("maxAccelWidth");
780: int maxAccelValue = maxAccelWidth == null ? 0
781: : maxAccelWidth.intValue();
782: accelOffset = maxAccelValue - accelRect.width;
783: }
784:
785: g.setFont(acceleratorFont);
786: if (! m.isEnabled())
787: {
788:
789: g.setColor(disabledForeground);
790: }
791: else
792: {
793: if (m.isArmed() || (m instanceof JMenu && m.isSelected()))
794: g.setColor(acceleratorSelectionForeground);
795: else
796: g.setColor(acceleratorForeground);
797: }
798: g.drawString(accelText, accelRect.x - accelOffset,
799: accelRect.y + accelFm.getAscent());
800: }
801:
802:
803: if (arrowIcon != null
804: && ! (m instanceof JMenu && ((JMenu) m).isTopLevelMenu()))
805: {
806: arrowIcon.paintIcon(m, g, arrowIconRect.x, arrowIconRect.y);
807: }
808:
809: g.setFont(oldFont);
810: g.setColor(oldColor);
811:
812: }
813:
814:
827: protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect,
828: String text)
829: {
830: Font f = menuItem.getFont();
831: g.setFont(f);
832: FontMetrics fm = g.getFontMetrics(f);
833:
834: if (text != null && !text.equals(""))
835: {
836: if (menuItem.isEnabled())
837: {
838:
839:
840: ButtonModel mod = menuItem.getModel();
841: if ((menuItem.isSelected() && checkIcon == null)
842: || (mod != null && mod.isArmed())
843: && (menuItem.getParent() instanceof MenuElement))
844: g.setColor(selectionForeground);
845: else
846: g.setColor(menuItem.getForeground());
847: }
848: else
849:
850:
851:
852:
853:
854: g.setColor(Color.gray);
855:
856: int mnemonicIndex = menuItem.getDisplayedMnemonicIndex();
857:
858: if (mnemonicIndex != -1)
859: BasicGraphicsUtils.drawStringUnderlineCharAt(menuItem, g, text,
860: mnemonicIndex,
861: textRect.x,
862: textRect.y
863: + fm.getAscent());
864: else
865: BasicGraphicsUtils.drawString(menuItem, g, text, 0, textRect.x,
866: textRect.y + fm.getAscent());
867: }
868: }
869:
870:
876: protected void uninstallComponents(JMenuItem menuItem)
877: {
878:
879: }
880:
881:
885: protected void uninstallDefaults()
886: {
887: menuItem.setForeground(null);
888: menuItem.setBackground(null);
889: menuItem.setBorder(null);
890: menuItem.setMargin(null);
891: menuItem.setBackground(null);
892: menuItem.setBorder(null);
893: menuItem.setFont(null);
894: menuItem.setForeground(null);
895: menuItem.setMargin(null);
896: acceleratorFont = null;
897: acceleratorForeground = null;
898: acceleratorSelectionForeground = null;
899: arrowIcon = null;
900: selectionBackground = null;
901: selectionForeground = null;
902: acceleratorDelimiter = null;
903: }
904:
905:
908: protected void uninstallKeyboardActions()
909: {
910: SwingUtilities.replaceUIInputMap(menuItem,
911: JComponent.WHEN_IN_FOCUSED_WINDOW, null);
912: }
913:
914:
917: protected void uninstallListeners()
918: {
919: menuItem.removeMouseListener(mouseInputListener);
920: menuItem.removeMenuDragMouseListener(menuDragMouseListener);
921: menuItem.removeMenuKeyListener(menuKeyListener);
922: menuItem.removeItemListener(itemListener);
923: menuItem.removePropertyChangeListener(propertyChangeListener);
924: }
925:
926:
934: public void uninstallUI(JComponent c)
935: {
936: uninstallListeners();
937: uninstallDefaults();
938: uninstallComponents(menuItem);
939: menuItem = null;
940: }
941:
942:
950: public void update(Graphics g, JComponent c)
951: {
952: paint(g, c);
953: }
954:
955:
962: private String getAcceleratorText(KeyStroke accelerator)
963: {
964:
965: String modifiersText = "";
966: int modifiers = accelerator.getModifiers();
967: char keyChar = accelerator.getKeyChar();
968: int keyCode = accelerator.getKeyCode();
969:
970: if (modifiers != 0)
971: modifiersText = KeyEvent.getKeyModifiersText(modifiers)
972: + acceleratorDelimiter;
973:
974: if (keyCode == KeyEvent.VK_UNDEFINED)
975: return modifiersText + keyChar;
976: else
977: return modifiersText + KeyEvent.getKeyText(keyCode);
978: }
979:
980:
989: private Rectangle getAcceleratorRect(KeyStroke accelerator, FontMetrics fm)
990: {
991: int width = fm.stringWidth(getAcceleratorText(accelerator));
992: int height = fm.getHeight();
993: return new Rectangle(0, 0, width, height);
994: }
995:
996:
1001: protected class MouseInputHandler implements MouseInputListener
1002: {
1003:
1006: protected MouseInputHandler()
1007: {
1008:
1009: }
1010:
1011:
1018: public void mouseClicked(MouseEvent e)
1019: {
1020: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1021: manager.processMouseEvent(e);
1022: }
1023:
1024:
1031: public void mouseDragged(MouseEvent e)
1032: {
1033: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1034: manager.processMouseEvent(e);
1035: }
1036:
1037:
1046: public void mouseEntered(MouseEvent e)
1047: {
1048: Component source = (Component) e.getSource();
1049: if (source.getParent() instanceof MenuElement)
1050: {
1051: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1052: manager.setSelectedPath(getPath());
1053: manager.processMouseEvent(e);
1054: }
1055: }
1056:
1057:
1064: public void mouseExited(MouseEvent e)
1065: {
1066: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1067: manager.processMouseEvent(e);
1068: }
1069:
1070:
1077: public void mouseMoved(MouseEvent e)
1078: {
1079: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1080: manager.processMouseEvent(e);
1081: }
1082:
1083:
1090: public void mousePressed(MouseEvent e)
1091: {
1092: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1093: manager.processMouseEvent(e);
1094: }
1095:
1096:
1104: public void mouseReleased(MouseEvent e)
1105: {
1106: Rectangle size = menuItem.getBounds();
1107: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1108: if (e.getX() > 0 && e.getX() < size.width && e.getY() > 0
1109: && e.getY() < size.height)
1110: {
1111: manager.clearSelectedPath();
1112: menuItem.doClick();
1113: }
1114:
1115: else
1116: manager.processMouseEvent(e);
1117: }
1118: }
1119:
1120:
1123: private class MenuDragMouseHandler implements MenuDragMouseListener
1124: {
1125:
1131: public void menuDragMouseDragged(MenuDragMouseEvent e)
1132: {
1133: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1134: manager.setSelectedPath(e.getPath());
1135: }
1136:
1137:
1144: public void menuDragMouseEntered(MenuDragMouseEvent e)
1145: {
1146: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1147: manager.setSelectedPath(e.getPath());
1148: }
1149:
1150:
1156: public void menuDragMouseExited(MenuDragMouseEvent e)
1157: {
1158:
1159: }
1160:
1161:
1168: public void menuDragMouseReleased(MenuDragMouseEvent e)
1169: {
1170: MenuElement[] path = e.getPath();
1171:
1172: if (path[path.length - 1] instanceof JMenuItem)
1173: ((JMenuItem) path[path.length - 1]).doClick();
1174:
1175: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
1176: manager.clearSelectedPath();
1177: }
1178: }
1179:
1180:
1184: private class MenuKeyHandler implements MenuKeyListener
1185: {
1186:
1192: public void menuKeyPressed(MenuKeyEvent e)
1193: {
1194:
1195: }
1196:
1197:
1203: public void menuKeyReleased(MenuKeyEvent e)
1204: {
1205:
1206: }
1207:
1208:
1215: public void menuKeyTyped(MenuKeyEvent e)
1216: {
1217:
1218: }
1219: }
1220:
1221:
1225: private class ItemHandler implements ItemListener
1226: {
1227:
1232: public void itemStateChanged(ItemEvent evt)
1233: {
1234: boolean state = false;
1235: if (menuItem instanceof JCheckBoxMenuItem)
1236: {
1237: if (evt.getStateChange() == ItemEvent.SELECTED)
1238: state = true;
1239: ((JCheckBoxMenuItem) menuItem).setState(state);
1240: }
1241: menuItem.revalidate();
1242: menuItem.repaint();
1243: }
1244: }
1245:
1246:
1255: private String getAcceleratorString(JMenuItem m)
1256: {
1257:
1258: KeyStroke accel = m.getAccelerator();
1259: String accelText = "";
1260: if (accel != null)
1261: {
1262: int mods = accel.getModifiers();
1263: if (mods > 0)
1264: {
1265: accelText = KeyEvent.getKeyModifiersText(mods);
1266: accelText += acceleratorDelimiter;
1267: }
1268: int keycode = accel.getKeyCode();
1269: if (keycode != 0)
1270: accelText += KeyEvent.getKeyText(keycode);
1271: else
1272: accelText += accel.getKeyChar();
1273: }
1274: return accelText;
1275: }
1276:
1277:
1284: private void layoutMenuItem(JMenuItem m, String accelText)
1285: {
1286: int width = m.getWidth();
1287: int height = m.getHeight();
1288:
1289:
1290: iconRect.setBounds(0, 0, 0, 0);
1291: textRect.setBounds(0, 0, 0, 0);
1292: accelRect.setBounds(0, 0, 0, 0);
1293: checkIconRect.setBounds(0, 0, 0, 0);
1294: arrowIconRect.setBounds(0, 0, 0, 0);
1295: viewRect.setBounds(0, 0, width, height);
1296:
1297:
1298: Insets insets = m.getInsets();
1299: viewRect.x += insets.left;
1300: viewRect.y += insets.top;
1301: viewRect.width -= insets.left + insets.right;
1302: viewRect.height -= insets.top + insets.bottom;
1303:
1304:
1305: Font font = m.getFont();
1306: FontMetrics fm = m.getFontMetrics(font);
1307: FontMetrics accelFm = m.getFontMetrics(acceleratorFont);
1308:
1309: String text = m.getText();
1310: SwingUtilities.layoutCompoundLabel(m, fm, text, m.getIcon(),
1311: m.getVerticalAlignment(),
1312: m.getHorizontalAlignment(),
1313: m.getVerticalTextPosition(),
1314: m.getHorizontalTextPosition(),
1315: viewRect, iconRect, textRect,
1316: defaultTextIconGap);
1317:
1318:
1319: if (! accelText.equals(""))
1320: {
1321: accelRect.width = accelFm.stringWidth(accelText);
1322: accelRect.height = accelFm.getHeight();
1323: }
1324:
1325:
1326: if (! (m instanceof JMenu && ((JMenu) m).isTopLevelMenu()))
1327: {
1328: if (checkIcon != null)
1329: {
1330: checkIconRect.width = checkIcon.getIconWidth();
1331: checkIconRect.height = checkIcon.getIconHeight();
1332: }
1333: if (arrowIcon != null)
1334: {
1335: arrowIconRect.width = arrowIcon.getIconWidth();
1336: arrowIconRect.height = arrowIcon.getIconHeight();
1337: }
1338: }
1339:
1340:
1341: cachedRect.setBounds(textRect);
1342: Rectangle labelRect = SwingUtilities.computeUnion(iconRect.x,
1343: iconRect.y,
1344: iconRect.width,
1345: iconRect.height,
1346: cachedRect);
1347: textRect.x += defaultTextIconGap;
1348: iconRect.x += defaultTextIconGap;
1349:
1350:
1351: accelRect.x = viewRect.x + viewRect.width - arrowIconRect.width
1352: - defaultTextIconGap - accelRect.width;
1353:
1354: if (! (m instanceof JMenu && ((JMenu) m).isTopLevelMenu()))
1355: {
1356: checkIconRect.x = viewRect.x + defaultTextIconGap;
1357: textRect.x += defaultTextIconGap + checkIconRect.width;
1358: iconRect.x += defaultTextIconGap + checkIconRect.width;
1359: arrowIconRect.x = viewRect.x + viewRect.width - defaultTextIconGap
1360: - arrowIconRect.width;
1361: }
1362:
1363:
1364:
1365: accelRect.y = labelRect.y + (labelRect.height / 2)
1366: - (accelRect.height / 2);
1367: if (! (m instanceof JMenu && ((JMenu) m).isTopLevelMenu()))
1368: {
1369: arrowIconRect.y = labelRect.y + (labelRect.height / 2)
1370: - (arrowIconRect.height / 2);
1371: checkIconRect.y = labelRect.y + (labelRect.height / 2)
1372: - (checkIconRect.height / 2);
1373: }
1374: }
1375: }