home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.AbstractButton;
- import com.sun.java.swing.Icon;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JMenu;
- import com.sun.java.swing.JMenuBar;
- import com.sun.java.swing.JMenuItem;
- import com.sun.java.swing.JPopupMenu;
- import com.sun.java.swing.KeyStroke;
- import com.sun.java.swing.LookAndFeel;
- import com.sun.java.swing.MenuElement;
- import com.sun.java.swing.MenuSelectionManager;
- import com.sun.java.swing.Timer;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.event.ChangeListener;
- import com.sun.java.swing.plaf.ComponentUI;
- import com.sun.java.swing.plaf.MenuUI;
- import com.sun.java.swing.plaf.UIResource;
- import java.awt.AWTEvent;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.Point;
- import java.awt.event.ComponentEvent;
- import java.awt.event.InputEvent;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.io.Serializable;
-
- public class BasicMenuUI extends MenuUI implements Serializable {
- protected JMenu menu;
- protected MouseListener mouseListener;
- protected MouseMotionListener dragListener;
- protected ChangeListener menuChangeListener;
- protected static Color pressedBackground;
- protected static Color pressedForeground;
- private int lastMnemonic = 0;
- protected static final int defaultTextIconGap = 4;
- protected Icon menuArrow = null;
- protected Icon checkIcon = null;
- protected boolean oldBorderPainted;
-
- protected void addListeners(JComponent c) {
- ((Component)c).addMouseListener(this.mouseListener);
- ((Component)c).addMouseMotionListener(this.dragListener);
- ((JMenu)c).addChangeListener(this.menuChangeListener);
- }
-
- protected ChangeListener createMenuChangeListener(JComponent c) {
- return new MenuChangeListener((JMenu)c, this);
- }
-
- protected MouseListener createMouseListener(JComponent c) {
- return new MouseListener((JMenu)c);
- }
-
- protected MouseMotionListener createMouseMotionListener(JComponent c) {
- return new BasicMenuMouseMotionListener();
- }
-
- public static ComponentUI createUI(JComponent x) {
- return new BasicMenuUI();
- }
-
- public Insets getDefaultMargin(AbstractButton c) {
- return new Insets(2, 2, 2, 2);
- }
-
- public Dimension getMaximumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getMinimumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getPreferredSize(JComponent c) {
- this.installDefaultIcons();
- return BasicGraphicsUtils.getPreferredMenuItemSize(c, this.checkIcon, this.menuArrow, 4);
- }
-
- protected void initListeners(JComponent c) {
- this.mouseListener = this.createMouseListener(c);
- this.dragListener = this.createMouseMotionListener(c);
- this.menuChangeListener = this.createMenuChangeListener(c);
- }
-
- protected void installDefaultIcons() {
- if (this.menu != null && this.menu.getParent() != null && !(this.menu.getParent() instanceof JMenuBar)) {
- if (this.menuArrow == null || this.menuArrow instanceof UIResource) {
- this.menuArrow = UIManager.getIcon("Menu.arrowIcon");
- }
-
- if (this.checkIcon == null || this.checkIcon instanceof UIResource) {
- this.checkIcon = UIManager.getIcon("MenuItem.checkIcon");
- }
- }
-
- }
-
- public void installUI(JComponent c) {
- this.menu = (JMenu)c;
- this.menu.setDelay(200);
- this.initListeners(c);
- this.addListeners(c);
- c.setOpaque(true);
- LookAndFeel.installBorder(c, "Menu.border");
- this.oldBorderPainted = this.menu.isBorderPainted();
- this.menu.setBorderPainted((Boolean)UIManager.get("MenuItem.borderPainted"));
- LookAndFeel.installColorsAndFont(c, "Menu.background", "Menu.foreground", "Menu.font");
- this.validateKeyboardAccelerator();
- if (pressedBackground == null || pressedBackground instanceof UIResource) {
- pressedBackground = UIManager.getColor("Menu.pressedBackground");
- }
-
- if (pressedForeground == null || pressedForeground instanceof UIResource) {
- pressedForeground = UIManager.getColor("Menu.pressedForeground");
- }
-
- }
-
- private int lower(int ascii) {
- return ascii >= 65 && ascii <= 90 ? ascii + 97 - 65 : ascii;
- }
-
- public void menuCanceled(JMenu m) {
- MenuSelectionManager manager = MenuSelectionManager.defaultManager();
- if (manager.isComponentPartOfCurrentMenu(m)) {
- MenuSelectionManager.defaultManager().clearSelectedPath();
- }
-
- }
-
- public void paint(Graphics g, JComponent c) {
- this.installDefaultIcons();
- ((AbstractButton)c).getModel();
- BasicGraphicsUtils.paintMenuItem(g, c, this.checkIcon, this.menuArrow, pressedBackground, pressedForeground, 4);
- }
-
- public void processKeyEvent(JMenuItem item, KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
- int key = ((AbstractButton)item).getMnemonic();
- if (key != 0) {
- if (((AWTEvent)e).getID() == 401 && this.lower(key) == this.lower(e.getKeyChar())) {
- JPopupMenu popupMenu = ((JMenu)item).getPopupMenu();
- MenuElement[] sub = popupMenu.getSubElements();
- if (sub.length > 0) {
- MenuElement[] newPath = new MenuElement[path.length + 2];
- System.arraycopy(path, 0, newPath, 0, path.length);
- newPath[path.length] = popupMenu;
- newPath[path.length + 1] = sub[0];
- manager.setSelectedPath(newPath);
- }
-
- ((InputEvent)e).consume();
- }
-
- }
- }
-
- public void processMouseEvent(JMenuItem item, MouseEvent e, MenuElement[] path, MenuSelectionManager manager) {
- Point p = e.getPoint();
- if (((Component)item).isEnabled()) {
- if (p.x >= 0 && p.x < ((JComponent)item).getWidth() && p.y >= 0 && p.y < ((JComponent)item).getHeight()) {
- JMenu menu = (JMenu)item;
- MenuElement[] selectedPath = manager.getSelectedPath();
- if (selectedPath.length <= 0 || selectedPath[selectedPath.length - 1] != menu.getPopupMenu()) {
- if (!menu.isTopLevelMenu() && menu.getDelay() != 0 && ((AWTEvent)e).getID() != 506) {
- manager.setSelectedPath(path);
- this.setupPostTimer(menu);
- } else {
- MenuElement[] newPath = new MenuElement[path.length + 1];
- System.arraycopy(path, 0, newPath, 0, path.length);
- newPath[path.length] = menu.getPopupMenu();
- manager.setSelectedPath(newPath);
- }
- }
- } else if (((AWTEvent)e).getID() == 502) {
- Component c = manager.componentForPoint(((ComponentEvent)e).getComponent(), e.getPoint());
- if (c == null) {
- manager.clearSelectedPath();
- }
- }
-
- }
- }
-
- protected void removeListeners(JComponent c) {
- ((Component)c).removeMouseListener(this.mouseListener);
- ((Component)c).removeMouseMotionListener(this.dragListener);
- ((JMenu)c).removeChangeListener(this.menuChangeListener);
- }
-
- protected void setupPostTimer(JMenu menu) {
- Timer timer = new Timer(menu.getDelay(), new PostAction(menu, false));
- timer.setRepeats(false);
- timer.start();
- }
-
- public void uninstallUI(JComponent c) {
- this.menu.setArmed(false);
- this.menu.setSelected(false);
- this.removeListeners(c);
- c.resetKeyboardActions();
- LookAndFeel.uninstallBorder(c);
- ((JMenu)c).setBorderPainted(this.oldBorderPainted);
- if (this.menuArrow instanceof UIResource) {
- this.menuArrow = null;
- }
-
- if (this.checkIcon instanceof UIResource) {
- this.checkIcon = null;
- }
-
- }
-
- public void update(Graphics g, JComponent c) {
- this.paint(g, c);
- }
-
- protected void validateKeyboardAccelerator() {
- if (this.menu.getModel().getMnemonic() != this.lastMnemonic) {
- this.menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(this.lastMnemonic, 8, false));
- this.lastMnemonic = this.menu.getModel().getMnemonic();
- this.menu.registerKeyboardAction(new PostAction(this.menu, true), KeyStroke.getKeyStroke(this.lastMnemonic, 8, false), 2);
- }
-
- }
- }
-