BasicMenuItemUI.java [plain text]
package javax.swing.plaf.basic;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.event.MenuDragMouseEvent;
import javax.swing.event.MenuDragMouseListener;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.MenuItemUI;
public class BasicMenuItemUI extends MenuItemUI
{
protected Font acceleratorFont;
protected Color acceleratorForeground;
protected Color acceleratorSelectionForeground;
protected Icon arrowIcon;
protected Icon checkIcon;
protected int defaultTextIconGap = 4;
protected Color disabledForeground;
protected MenuDragMouseListener menuDragMouseListener;
protected JMenuItem menuItem;
protected MenuKeyListener menuKeyListener;
protected MouseInputListener mouseInputListener;
protected boolean oldBorderPainted;
protected Color selectionBackground;
protected Color selectionForeground;
private String acceleratorDelimiter;
private int defaultAcceleratorLabelGap = 4;
BasicMenuItemUI()
{
mouseInputListener = createMouseInputListener(menuItem);
menuDragMouseListener = createMenuDragMouseListener(menuItem);
menuKeyListener = createMenuKeyListener(menuItem);
}
protected MenuDragMouseListener createMenuDragMouseListener(JComponent c)
{
return new MenuDragMouseHandler();
}
protected MenuKeyListener createMenuKeyListener(JComponent c)
{
return new MenuKeyHandler();
}
protected MouseInputListener createMouseInputListener(JComponent c)
{
return new MouseInputHandler();
}
public static ComponentUI createUI(JComponent c)
{
return new BasicMenuItemUI();
}
protected void doClick(MenuSelectionManager msm)
{
}
public Dimension getMaximumSize(JComponent c)
{
return null;
}
public Dimension getMinimumSize(JComponent c)
{
return null;
}
public MenuElement[] getPath()
{
return null;
}
protected Dimension getPreferredMenuItemSize(JComponent c, Icon checkIcon,
Icon arrowIcon,
int defaultTextIconGap)
{
return null;
}
public Dimension getPreferredSize(JComponent c)
{
AbstractButton b = (AbstractButton) c;
Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b,
defaultTextIconGap);
KeyStroke accelerator = ((JMenuItem) c).getAccelerator();
Rectangle rect;
if (accelerator != null)
{
rect = getAcceleratorRect(accelerator,
b.getToolkit().getFontMetrics(acceleratorFont));
d.width = d.width + rect.width + defaultAcceleratorLabelGap;
if (d.height < rect.height)
d.height = rect.height;
}
if (checkIcon != null)
{
d.width = d.width + checkIcon.getIconWidth() + defaultTextIconGap;
if (checkIcon.getIconHeight() > d.height)
d.height = checkIcon.getIconHeight();
}
return d;
}
protected String getPropertyPrefix()
{
return null;
}
protected void installComponents(JMenuItem menuItem)
{
}
protected void installDefaults()
{
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
menuItem.setBackground(defaults.getColor("MenuItem.background"));
menuItem.setBorder(defaults.getBorder("MenuItem.border"));
menuItem.setFont(defaults.getFont("MenuItem.font"));
menuItem.setForeground(defaults.getColor("MenuItem.foreground"));
menuItem.setMargin(defaults.getInsets("MenuItem.margin"));
acceleratorFont = defaults.getFont("MenuItem.acceleratorFont");
acceleratorForeground = defaults.getColor("MenuItem.acceleratorForeground");
acceleratorSelectionForeground = defaults.getColor("MenuItem.acceleratorSelectionForeground");
arrowIcon = defaults.getIcon("MenuItem.arrowIcon");
selectionBackground = defaults.getColor("MenuItem.selectionBackground");
selectionForeground = defaults.getColor("MenuItem.selectionForeground");
acceleratorDelimiter = defaults.getString("MenuItem.acceleratorDelimiter");
}
protected void installKeyboardActions()
{
}
protected void installListeners()
{
menuItem.addMouseListener(mouseInputListener);
menuItem.addMenuDragMouseListener(menuDragMouseListener);
menuItem.addMenuKeyListener(menuKeyListener);
}
public void installUI(JComponent c)
{
super.installUI(c);
menuItem = (JMenuItem) c;
installDefaults();
installListeners();
}
public void paint(Graphics g, JComponent c)
{
paintMenuItem(g, c, checkIcon, arrowIcon, c.getBackground(),
c.getForeground(), defaultTextIconGap);
}
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor)
{
Dimension size = getPreferredSize(menuItem);
Color foreground = g.getColor();
g.setColor(bgColor);
g.drawRect(0, 0, size.width, size.height);
g.setColor(foreground);
}
protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon,
Icon arrowIcon, Color background,
Color foreground, int defaultTextIconGap)
{
AbstractButton b = (AbstractButton) c;
Rectangle tr = new Rectangle(); Rectangle ir = new Rectangle(); Rectangle vr = new Rectangle(); Rectangle br = new Rectangle(); Rectangle ar = new Rectangle(); Rectangle cr = new Rectangle();
int vertAlign = b.getVerticalAlignment();
int horAlign = b.getHorizontalAlignment();
int vertTextPos = b.getVerticalTextPosition();
int horTextPos = b.getHorizontalTextPosition();
Font f = c.getFont();
g.setFont(f);
FontMetrics fm = g.getFontMetrics(f);
SwingUtilities.calculateInnerArea(b, br);
SwingUtilities.calculateInsetArea(br, b.getMargin(), vr);
paintBackground(g, (JMenuItem) c, c.getBackground());
if ((b.getModel().isArmed() && b.getModel().isPressed()))
{
if (((AbstractButton) b).isContentAreaFilled())
{
g.setColor(b.getBackground().darker());
g.fillRect(br.x, br.y, br.width, br.height);
}
}
else
{
if (((AbstractButton) b).isContentAreaFilled())
{
g.setColor(b.getBackground());
g.fillRect(br.x, br.y, br.width, br.height);
}
}
if (checkIcon != null)
{
SwingUtilities.layoutCompoundLabel(c, fm, null, checkIcon, vertAlign,
horAlign, vertTextPos, horTextPos,
vr, cr, tr, defaultTextIconGap);
checkIcon.paintIcon(c, g, cr.x, cr.y);
vr.x = cr.x + cr.width + defaultTextIconGap;
}
if (arrowIcon != null)
{
}
SwingUtilities.layoutCompoundLabel(c, fm, b.getText(), b.getIcon(),
vertAlign, horAlign, vertTextPos,
horTextPos, vr, ir, tr,
defaultTextIconGap);
paintText(g, (JMenuItem) c, tr, b.getText());
String acceleratorText = "";
if (((JMenuItem) c).getAccelerator() != null)
{
acceleratorText = getAcceleratorText(((JMenuItem) c).getAccelerator());
fm = g.getFontMetrics(acceleratorFont);
ar.width = fm.stringWidth(acceleratorText);
ar.x = br.width - ar.width;
vr.x = br.width - ar.width;
SwingUtilities.layoutCompoundLabel(c, fm, acceleratorText, null,
vertAlign, horAlign, vertTextPos,
horTextPos, vr, ir, ar,
defaultTextIconGap);
paintAccelerator(g, (JMenuItem) c, ar, acceleratorText);
}
}
protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect,
String text)
{
Font f = menuItem.getFont();
g.setFont(f);
FontMetrics fm = g.getFontMetrics(f);
g.setColor(menuItem.getForeground());
BasicGraphicsUtils.drawString(g, text, 0, textRect.x,
textRect.y + fm.getAscent());
}
protected void uninstallComponents(JMenuItem menuItem)
{
}
protected void uninstallDefaults()
{
menuItem.setForeground(null);
menuItem.setBackground(null);
menuItem.setBorder(null);
menuItem.setMargin(null);
menuItem.setBackground(null);
menuItem.setBorder(null);
menuItem.setFont(null);
menuItem.setForeground(null);
menuItem.setMargin(null);
acceleratorFont = null;
acceleratorForeground = null;
acceleratorSelectionForeground = null;
arrowIcon = null;
selectionBackground = null;
selectionForeground = null;
acceleratorDelimiter = null;
}
protected void uninstallKeyboardActions()
{
}
protected void uninstallListeners()
{
menuItem.removeMouseListener(mouseInputListener);
menuItem.removeMenuDragMouseListener(menuDragMouseListener);
menuItem.removeMenuKeyListener(menuKeyListener);
}
public void uninstallUI(JComponent c)
{
uninstallListeners();
uninstallDefaults();
menuItem = null;
}
public void update(Graphics g, JComponent c)
{
}
private String getAcceleratorText(KeyStroke accelerator)
{
String modifiersText = "";
int modifiers = accelerator.getModifiers();
char keyChar = accelerator.getKeyChar();
int keyCode = accelerator.getKeyCode();
if (modifiers != 0)
modifiersText = KeyEvent.getKeyModifiersText(modifiers) +
acceleratorDelimiter;
if (keyCode == KeyEvent.VK_UNDEFINED)
return modifiersText + keyChar;
else
return modifiersText + KeyEvent.getKeyText(keyCode);
}
private Rectangle getAcceleratorRect(KeyStroke accelerator, FontMetrics fm)
{
int width = fm.stringWidth(getAcceleratorText(accelerator));
int height = fm.getHeight();
return new Rectangle(0, 0, width, height);
}
private void paintAccelerator(Graphics g, JMenuItem menuItem, Rectangle acceleratorRect,
String acceleratorText)
{
g.setFont(acceleratorFont);
FontMetrics fm = g.getFontMetrics(acceleratorFont);
g.setColor(acceleratorForeground);
BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x,
acceleratorRect.y + fm.getAscent());
}
protected class MouseInputHandler implements MouseInputListener
{
protected MouseInputHandler()
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
if (e.getSource() instanceof AbstractButton)
{
AbstractButton button = (AbstractButton) e.getSource();
ButtonModel model = button.getModel();
if (button.isRolloverEnabled())
{
model.setRollover(true);
}
if (model.isPressed() &&
((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0))
{
model.setArmed(true);
}
else
{
model.setArmed(false);
}
}
}
public void mouseExited(MouseEvent e)
{
if (e.getSource() instanceof AbstractButton)
{
AbstractButton button = (AbstractButton) e.getSource();
ButtonModel model = button.getModel();
if (button.isRolloverEnabled())
{
model.setRollover(false);
}
model.setArmed(false);
}
}
public void mouseMoved(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
if (e.getSource() instanceof AbstractButton)
{
AbstractButton button = (AbstractButton) e.getSource();
ButtonModel model = button.getModel();
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
{
model.setArmed(true);
model.setPressed(true);
}
}
}
public void mouseReleased(MouseEvent e)
{
if (e.getSource() instanceof AbstractButton)
{
AbstractButton button = (AbstractButton) e.getSource();
ButtonModel model = button.getModel();
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
{
model.setPressed(false);
model.setArmed(false);
}
}
}
}
protected class MenuDragMouseHandler implements MenuDragMouseListener
{
public void menuDragMouseDragged(MenuDragMouseEvent e)
{
}
public void menuDragMouseEntered(MenuDragMouseEvent e)
{
}
public void menuDragMouseExited(MenuDragMouseEvent e)
{
}
public void menuDragMouseReleased(MenuDragMouseEvent e)
{
}
}
protected class MenuKeyHandler implements MenuKeyListener
{
public void menuKeyPressed(MenuKeyEvent e)
{
}
public void menuKeyReleased(MenuKeyEvent e)
{
}
public void menuKeyTyped(MenuKeyEvent e)
{
}
}
protected class PropertyChangeHandler
{
public void propertyChange(PropertyChangeEvent evt)
{
}
}
}