| Method from javax.swing.plaf.basic.BasicMenuItemUI Detail: |
InputMap createInputMap(int condition) {
if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
return new ComponentInputMapUIResource(menuItem);
}
return null;
}
|
protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) {
return getHandler();
}
|
protected MenuKeyListener createMenuKeyListener(JComponent c) {
return null;
}
|
protected MouseInputListener createMouseInputListener(JComponent c) {
return getHandler();
}
|
protected PropertyChangeListener createPropertyChangeListener(JComponent c) {
return getHandler();
}
Creates a PropertyChangeListener which will be added to
the menu item.
If this method returns null then it will not be added to the menu item. |
public static ComponentUI createUI(JComponent c) {
return new BasicMenuItemUI();
}
|
protected void doClick(MenuSelectionManager msm) {
// Auditory cue
if (! isInternalFrameSystemMenu()) {
BasicLookAndFeel.playSound(menuItem, getPropertyPrefix() +
".commandSound");
}
// Visual feedback
if (msm == null) {
msm = MenuSelectionManager.defaultManager();
}
msm.clearSelectedPath();
menuItem.doClick(0);
}
Call this method when a menu item is to be activated.
This method handles some of the details of menu item activation
such as clearing the selected path and messaging the
JMenuItem's doClick() method. |
BasicMenuItemUI.Handler getHandler() {
if (handler == null) {
handler = new Handler();
}
return handler;
}
|
public Dimension getMaximumSize(JComponent c) {
Dimension d = null;
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
d = getPreferredSize(c);
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
}
return d;
}
|
public Dimension getMinimumSize(JComponent c) {
Dimension d = null;
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
d = getPreferredSize(c);
d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
}
return d;
}
|
public MenuElement[] getPath() {
MenuSelectionManager m = MenuSelectionManager.defaultManager();
MenuElement oldPath[] = m.getSelectedPath();
MenuElement newPath[];
int i = oldPath.length;
if (i == 0)
return new MenuElement[0];
Component parent = menuItem.getParent();
if (oldPath[i-1].getComponent() == parent) {
// The parent popup menu is the last so far
newPath = new MenuElement[i+1];
System.arraycopy(oldPath, 0, newPath, 0, i);
newPath[i] = menuItem;
} else {
// A sibling menuitem is the current selection
//
// This probably needs to handle 'exit submenu into
// a menu item. Search backwards along the current
// selection until you find the parent popup menu,
// then copy up to that and add yourself...
int j;
for (j = oldPath.length-1; j >= 0; j--) {
if (oldPath[j].getComponent() == parent)
break;
}
newPath = new MenuElement[j+2];
System.arraycopy(oldPath, 0, newPath, 0, j+1);
newPath[j+1] = menuItem;
/*
System.out.println("Sibling condition -- ");
System.out.println("Old array : ");
printMenuElementArray(oldPath, false);
System.out.println("New array : ");
printMenuElementArray(newPath, false);
*/
}
return newPath;
}
|
protected Dimension getPreferredMenuItemSize(JComponent c,
Icon checkIcon,
Icon arrowIcon,
int defaultTextIconGap) {
// The method also determines the preferred width of the
// parent popup menu (through DefaultMenuLayout class).
// The menu width equals to the maximal width
// among child menu items.
// Menu item width will be a sum of the widest check icon, label,
// arrow icon and accelerator text among neighbor menu items.
// For the latest menu item we will know the maximal widths exactly.
// It will be the widest menu item and it will determine
// the width of the parent popup menu.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// There is a conceptual problem: if user sets preferred size manually
// for a menu item, this method won't be called for it
// (see JComponent.getPreferredSize()),
// maximal widths won't be calculated, other menu items won't be able
// to take them into account and will be layouted in such a way,
// as there is no the item with manual preferred size.
// But after the first paint() method call, all maximal widths
// will be correctly calculated and layout of some menu items
// can be changed. For example, it can cause a shift of
// the icon and text when user points a menu item by mouse.
JMenuItem mi = (JMenuItem) c;
LayoutInfo li = getLayoutInfo(mi, checkIcon, arrowIcon,
createMaxViewRect(), defaultTextIconGap, acceleratorDelimiter,
BasicGraphicsUtils.isLeftToRight(mi), acceleratorFont,
useCheckAndArrow(), getPropertyPrefix());
Dimension result = new Dimension();
// Calculate the result width
result.width = li.leadingGap;
addWidth(li.maxCheckWidth, li.afterCheckIconGap, result);
// Take into account mimimal text offset.
if ((!li.isTopLevelMenu)
&& (li.minTextOffset > 0)
&& (result.width < li.minTextOffset)) {
result.width = li.minTextOffset;
}
addWidth(li.maxLabelWidth, li.gap, result);
addWidth(li.maxAccWidth, li.gap, result);
addWidth(li.maxArrowWidth, li.gap, result);
// Calculate the result height
result.height = max(li.checkRect.height, li.labelRect.height,
li.accRect.height, li.arrowRect.height);
// Take into account menu item insets
Insets insets = li.mi.getInsets();
if(insets != null) {
result.width += insets.left + insets.right;
result.height += insets.top + insets.bottom;
}
// if the width is even, bump it up one. This is critical
// for the focus dash line to draw properly
if(result.width%2 == 0) {
result.width++;
}
// if the height is even, bump it up one. This is critical
// for the text to center properly
if(result.height%2 == 0
&& Boolean.TRUE !=
UIManager.get(getPropertyPrefix() + ".evenHeight")) {
result.height++;
}
li.clear();
return result;
}
|
public Dimension getPreferredSize(JComponent c) {
return getPreferredMenuItemSize(c,
checkIcon,
arrowIcon,
defaultTextIconGap);
}
|
protected String getPropertyPrefix() {
return "MenuItem";
}
|
protected void installComponents(JMenuItem menuItem) {
BasicHTML.updateRenderer(menuItem, menuItem.getText());
}
|
protected void installDefaults() {
String prefix = getPropertyPrefix();
acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
Object opaque = UIManager.get(getPropertyPrefix() + ".opaque");
if (opaque != null) {
LookAndFeel.installProperty(menuItem, "opaque", opaque);
}
else {
LookAndFeel.installProperty(menuItem, "opaque", Boolean.TRUE);
}
if(menuItem.getMargin() == null ||
(menuItem.getMargin() instanceof UIResource)) {
menuItem.setMargin(UIManager.getInsets(prefix + ".margin"));
}
LookAndFeel.installProperty(menuItem, "iconTextGap", Integer.valueOf(4));
defaultTextIconGap = menuItem.getIconTextGap();
LookAndFeel.installBorder(menuItem, prefix + ".border");
oldBorderPainted = menuItem.isBorderPainted();
LookAndFeel.installProperty(menuItem, "borderPainted",
UIManager.get(prefix + ".borderPainted"));
LookAndFeel.installColorsAndFont(menuItem,
prefix + ".background",
prefix + ".foreground",
prefix + ".font");
// MenuItem specific defaults
if (selectionBackground == null ||
selectionBackground instanceof UIResource) {
selectionBackground =
UIManager.getColor(prefix + ".selectionBackground");
}
if (selectionForeground == null ||
selectionForeground instanceof UIResource) {
selectionForeground =
UIManager.getColor(prefix + ".selectionForeground");
}
if (disabledForeground == null ||
disabledForeground instanceof UIResource) {
disabledForeground =
UIManager.getColor(prefix + ".disabledForeground");
}
if (acceleratorForeground == null ||
acceleratorForeground instanceof UIResource) {
acceleratorForeground =
UIManager.getColor(prefix + ".acceleratorForeground");
}
if (acceleratorSelectionForeground == null ||
acceleratorSelectionForeground instanceof UIResource) {
acceleratorSelectionForeground =
UIManager.getColor(prefix + ".acceleratorSelectionForeground");
}
// Get accelerator delimiter
acceleratorDelimiter =
UIManager.getString("MenuItem.acceleratorDelimiter");
if (acceleratorDelimiter == null) { acceleratorDelimiter = "+"; }
// Icons
if (arrowIcon == null ||
arrowIcon instanceof UIResource) {
arrowIcon = UIManager.getIcon(prefix + ".arrowIcon");
}
if (checkIcon == null ||
checkIcon instanceof UIResource) {
checkIcon = UIManager.getIcon(prefix + ".checkIcon");
//In case of column layout, .checkIconFactory is defined for this UI,
//the icon is compatible with it and useCheckAndArrow() is true,
//then the icon is handled by the checkIcon.
boolean isColumnLayout = LayoutInfo.isColumnLayout(
BasicGraphicsUtils.isLeftToRight(menuItem), menuItem);
if (isColumnLayout) {
MenuItemCheckIconFactory iconFactory =
(MenuItemCheckIconFactory) UIManager.get(prefix
+ ".checkIconFactory");
if (iconFactory != null && useCheckAndArrow()
&& iconFactory.isCompatible(checkIcon, prefix)) {
checkIcon = iconFactory.getIcon(menuItem);
}
}
}
}
|
protected void installKeyboardActions() {
installLazyActionMap();
updateAcceleratorBinding();
}
|
void installLazyActionMap() {
LazyActionMap.installLazyActionMap(menuItem, BasicMenuItemUI.class,
getPropertyPrefix() + ".actionMap");
}
|
protected void installListeners() {
if ((mouseInputListener = createMouseInputListener(menuItem)) != null) {
menuItem.addMouseListener(mouseInputListener);
menuItem.addMouseMotionListener(mouseInputListener);
}
if ((menuDragMouseListener = createMenuDragMouseListener(menuItem)) != null) {
menuItem.addMenuDragMouseListener(menuDragMouseListener);
}
if ((menuKeyListener = createMenuKeyListener(menuItem)) != null) {
menuItem.addMenuKeyListener(menuKeyListener);
}
if ((propertyChangeListener = createPropertyChangeListener(menuItem)) != null) {
menuItem.addPropertyChangeListener(propertyChangeListener);
}
}
|
public void installUI(JComponent c) {
menuItem = (JMenuItem) c;
installDefaults();
installComponents(menuItem);
installListeners();
installKeyboardActions();
}
|
static void loadActionMap(LazyActionMap map) {
// NOTE: BasicMenuUI also calls into this method.
map.put(new Actions(Actions.CLICK));
BasicLookAndFeel.installAudioActionMap(map);
}
|
public void paint(Graphics g,
JComponent c) {
paintMenuItem(g, c, checkIcon, arrowIcon,
selectionBackground, selectionForeground,
defaultTextIconGap);
}
|
protected void paintBackground(Graphics g,
JMenuItem menuItem,
Color bgColor) {
ButtonModel model = menuItem.getModel();
Color oldColor = g.getColor();
int menuWidth = menuItem.getWidth();
int menuHeight = menuItem.getHeight();
if(menuItem.isOpaque()) {
if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) {
g.setColor(bgColor);
g.fillRect(0,0, menuWidth, menuHeight);
} else {
g.setColor(menuItem.getBackground());
g.fillRect(0,0, menuWidth, menuHeight);
}
g.setColor(oldColor);
}
else if (model.isArmed() || (menuItem instanceof JMenu &&
model.isSelected())) {
g.setColor(bgColor);
g.fillRect(0,0, menuWidth, menuHeight);
g.setColor(oldColor);
}
}
Draws the background of the menu item. |
protected void paintMenuItem(Graphics g,
JComponent c,
Icon checkIcon,
Icon arrowIcon,
Color background,
Color foreground,
int defaultTextIconGap) {
// Save original graphics font and color
Font holdf = g.getFont();
Color holdc = g.getColor();
JMenuItem mi = (JMenuItem) c;
g.setFont(mi.getFont());
Rectangle viewRect = new Rectangle(0, 0, mi.getWidth(), mi.getHeight());
applyInsets(viewRect, mi.getInsets());
LayoutInfo li = getLayoutInfo(mi, checkIcon, arrowIcon,
viewRect, defaultTextIconGap, acceleratorDelimiter,
BasicGraphicsUtils.isLeftToRight(mi), acceleratorFont,
useCheckAndArrow(), getPropertyPrefix());
layoutMenuItem(li);
paintBackground(g, mi, background);
paintCheckIcon(g, li, holdc, foreground);
paintIcon(g, li, holdc);
paintText(g, li);
paintAccText(g, li);
paintArrowIcon(g, li, foreground);
// Restore original graphics font and color
g.setColor(holdc);
g.setFont(holdf);
li.clear();
}
|
protected void paintText(Graphics g,
JMenuItem menuItem,
Rectangle textRect,
String text) {
ButtonModel model = menuItem.getModel();
FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g);
int mnemIndex = menuItem.getDisplayedMnemonicIndex();
if(!model.isEnabled()) {
// *** paint the text disabled
if ( UIManager.get("MenuItem.disabledForeground") instanceof Color ) {
g.setColor( UIManager.getColor("MenuItem.disabledForeground") );
SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
mnemIndex, textRect.x, textRect.y + fm.getAscent());
} else {
g.setColor(menuItem.getBackground().brighter());
SwingUtilities2.drawStringUnderlineCharAt(menuItem, g, text,
mnemIndex, textRect.x, textRect.y + fm.getAscent());
g.setColor(menuItem.getBackground().darker());
SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
mnemIndex, textRect.x - 1, textRect.y +
fm.getAscent() - 1);
}
} else {
// *** paint the text normally
if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) {
g.setColor(selectionForeground); // Uses protected field.
}
SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
mnemIndex, textRect.x, textRect.y + fm.getAscent());
}
}
Renders the text of the current menu item.
|
void printMenuElementArray(MenuElement[] path,
boolean dumpStack) {
System.out.println("Path is(");
int i, j;
for(i=0,j=path.length; i< j ;i++){
for (int k=0; k< =i; k++)
System.out.print(" ");
MenuElement me = (MenuElement) path[i];
if(me instanceof JMenuItem)
System.out.println(((JMenuItem)me).getText() + ", ");
else if (me == null)
System.out.println("NULL , ");
else
System.out.println("" + me + ", ");
}
System.out.println(")");
if (dumpStack == true)
Thread.dumpStack();
}
|
protected void uninstallComponents(JMenuItem menuItem) {
BasicHTML.updateRenderer(menuItem, "");
}
|
protected void uninstallDefaults() {
LookAndFeel.uninstallBorder(menuItem);
LookAndFeel.installProperty(menuItem, "borderPainted", oldBorderPainted);
if (menuItem.getMargin() instanceof UIResource)
menuItem.setMargin(null);
if (arrowIcon instanceof UIResource)
arrowIcon = null;
if (checkIcon instanceof UIResource)
checkIcon = null;
}
|
protected void uninstallKeyboardActions() {
SwingUtilities.replaceUIActionMap(menuItem, null);
SwingUtilities.replaceUIInputMap(menuItem, JComponent.
WHEN_IN_FOCUSED_WINDOW, null);
}
|
protected void uninstallListeners() {
if (mouseInputListener != null) {
menuItem.removeMouseListener(mouseInputListener);
menuItem.removeMouseMotionListener(mouseInputListener);
}
if (menuDragMouseListener != null) {
menuItem.removeMenuDragMouseListener(menuDragMouseListener);
}
if (menuKeyListener != null) {
menuItem.removeMenuKeyListener(menuKeyListener);
}
if (propertyChangeListener != null) {
menuItem.removePropertyChangeListener(propertyChangeListener);
}
mouseInputListener = null;
menuDragMouseListener = null;
menuKeyListener = null;
propertyChangeListener = null;
handler = null;
}
|
public void uninstallUI(JComponent c) {
menuItem = (JMenuItem)c;
uninstallDefaults();
uninstallComponents(menuItem);
uninstallListeners();
uninstallKeyboardActions();
// Remove values from the parent's Client Properties.
JComponent p = getMenuItemParent(menuItem);
if(p != null) {
p.putClientProperty(BasicMenuItemUI.MAX_ARROW_WIDTH, null );
p.putClientProperty(BasicMenuItemUI.MAX_CHECK_WIDTH, null );
p.putClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH, null );
p.putClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH, null );
p.putClientProperty(BasicMenuItemUI.MAX_ICON_WIDTH, null );
p.putClientProperty(BasicMenuItemUI.MAX_LABEL_WIDTH, null );
p.putClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET, null );
}
menuItem = null;
}
|
public void update(Graphics g,
JComponent c) {
paint(g, c);
}
We draw the background in paintMenuItem()
so override update (which fills the background of opaque
components by default) to just call paint(). |
void updateAcceleratorBinding() {
KeyStroke accelerator = menuItem.getAccelerator();
InputMap windowInputMap = SwingUtilities.getUIInputMap(
menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW);
if (windowInputMap != null) {
windowInputMap.clear();
}
if (accelerator != null) {
if (windowInputMap == null) {
windowInputMap = createInputMap(JComponent.
WHEN_IN_FOCUSED_WINDOW);
SwingUtilities.replaceUIInputMap(menuItem,
JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap);
}
windowInputMap.put(accelerator, "doClick");
}
}
|