home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / plaf / basic / BasicMenuUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  9.0 KB  |  237 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.AbstractButton;
  4. import com.sun.java.swing.Icon;
  5. import com.sun.java.swing.JComponent;
  6. import com.sun.java.swing.JMenu;
  7. import com.sun.java.swing.JMenuBar;
  8. import com.sun.java.swing.JMenuItem;
  9. import com.sun.java.swing.JPopupMenu;
  10. import com.sun.java.swing.KeyStroke;
  11. import com.sun.java.swing.LookAndFeel;
  12. import com.sun.java.swing.MenuElement;
  13. import com.sun.java.swing.MenuSelectionManager;
  14. import com.sun.java.swing.Timer;
  15. import com.sun.java.swing.UIManager;
  16. import com.sun.java.swing.event.ChangeListener;
  17. import com.sun.java.swing.plaf.ComponentUI;
  18. import com.sun.java.swing.plaf.MenuUI;
  19. import com.sun.java.swing.plaf.UIResource;
  20. import java.awt.AWTEvent;
  21. import java.awt.Color;
  22. import java.awt.Component;
  23. import java.awt.Dimension;
  24. import java.awt.Graphics;
  25. import java.awt.Insets;
  26. import java.awt.Point;
  27. import java.awt.event.ComponentEvent;
  28. import java.awt.event.InputEvent;
  29. import java.awt.event.KeyEvent;
  30. import java.awt.event.MouseEvent;
  31. import java.awt.event.MouseMotionListener;
  32. import java.io.Serializable;
  33.  
  34. public class BasicMenuUI extends MenuUI implements Serializable {
  35.    protected JMenu menu;
  36.    protected MouseListener mouseListener;
  37.    protected MouseMotionListener dragListener;
  38.    protected ChangeListener menuChangeListener;
  39.    protected static Color pressedBackground;
  40.    protected static Color pressedForeground;
  41.    private int lastMnemonic;
  42.    protected static final int defaultTextIconGap = 4;
  43.    protected Icon menuArrow;
  44.    protected Icon checkIcon;
  45.    protected boolean oldBorderPainted;
  46.  
  47.    public static ComponentUI createUI(JComponent var0) {
  48.       return new BasicMenuUI();
  49.    }
  50.  
  51.    public void installUI(JComponent var1) {
  52.       this.menu = (JMenu)var1;
  53.       this.menu.setDelay(200);
  54.       this.initListeners(var1);
  55.       this.addListeners(var1);
  56.       var1.setOpaque(true);
  57.       LookAndFeel.installBorder(var1, "Menu.border");
  58.       this.oldBorderPainted = this.menu.isBorderPainted();
  59.       this.menu.setBorderPainted((Boolean)UIManager.get("MenuItem.borderPainted"));
  60.       LookAndFeel.installColorsAndFont(var1, "Menu.background", "Menu.foreground", "Menu.font");
  61.       this.validateKeyboardAccelerator();
  62.       if (pressedBackground == null || pressedBackground instanceof UIResource) {
  63.          pressedBackground = UIManager.getColor("Menu.pressedBackground");
  64.       }
  65.  
  66.       if (pressedForeground == null || pressedForeground instanceof UIResource) {
  67.          pressedForeground = UIManager.getColor("Menu.pressedForeground");
  68.       }
  69.  
  70.    }
  71.  
  72.    public void uninstallUI(JComponent var1) {
  73.       this.menu.setArmed(false);
  74.       this.menu.setSelected(false);
  75.       this.removeListeners(var1);
  76.       var1.resetKeyboardActions();
  77.       LookAndFeel.uninstallBorder(var1);
  78.       ((JMenu)var1).setBorderPainted(this.oldBorderPainted);
  79.       if (this.menuArrow instanceof UIResource) {
  80.          this.menuArrow = null;
  81.       }
  82.  
  83.       if (this.checkIcon instanceof UIResource) {
  84.          this.checkIcon = null;
  85.       }
  86.  
  87.    }
  88.  
  89.    protected void validateKeyboardAccelerator() {
  90.       if (this.menu.getModel().getMnemonic() != this.lastMnemonic) {
  91.          this.menu.unregisterKeyboardAction(KeyStroke.getKeyStroke(this.lastMnemonic, 8, false));
  92.          this.lastMnemonic = this.menu.getModel().getMnemonic();
  93.          this.menu.registerKeyboardAction(new PostAction(this.menu, true), KeyStroke.getKeyStroke(this.lastMnemonic, 8, false), 2);
  94.       }
  95.  
  96.    }
  97.  
  98.    protected MouseListener createMouseListener(JComponent var1) {
  99.       return new MouseListener((JMenu)var1);
  100.    }
  101.  
  102.    protected MouseMotionListener createMouseMotionListener(JComponent var1) {
  103.       return new BasicMenuMouseMotionListener();
  104.    }
  105.  
  106.    protected ChangeListener createMenuChangeListener(JComponent var1) {
  107.       return new MenuChangeListener((JMenu)var1, this);
  108.    }
  109.  
  110.    protected void initListeners(JComponent var1) {
  111.       this.mouseListener = this.createMouseListener(var1);
  112.       this.dragListener = this.createMouseMotionListener(var1);
  113.       this.menuChangeListener = this.createMenuChangeListener(var1);
  114.    }
  115.  
  116.    protected void addListeners(JComponent var1) {
  117.       ((Component)var1).addMouseListener(this.mouseListener);
  118.       ((Component)var1).addMouseMotionListener(this.dragListener);
  119.       ((JMenu)var1).addChangeListener(this.menuChangeListener);
  120.    }
  121.  
  122.    protected void removeListeners(JComponent var1) {
  123.       ((Component)var1).removeMouseListener(this.mouseListener);
  124.       ((Component)var1).removeMouseMotionListener(this.dragListener);
  125.       ((JMenu)var1).removeChangeListener(this.menuChangeListener);
  126.    }
  127.  
  128.    protected void installDefaultIcons() {
  129.       if (this.menu != null && this.menu.getParent() != null && !(this.menu.getParent() instanceof JMenuBar)) {
  130.          if (this.menuArrow == null || this.menuArrow instanceof UIResource) {
  131.             this.menuArrow = UIManager.getIcon("Menu.arrowIcon");
  132.          }
  133.  
  134.          if (this.checkIcon == null || this.checkIcon instanceof UIResource) {
  135.             this.checkIcon = UIManager.getIcon("MenuItem.checkIcon");
  136.          }
  137.       }
  138.  
  139.    }
  140.  
  141.    public void update(Graphics var1, JComponent var2) {
  142.       this.paint(var1, var2);
  143.    }
  144.  
  145.    public void paint(Graphics var1, JComponent var2) {
  146.       this.installDefaultIcons();
  147.       ((AbstractButton)var2).getModel();
  148.       BasicGraphicsUtils.paintMenuItem(var1, var2, this.checkIcon, this.menuArrow, pressedBackground, pressedForeground, 4);
  149.    }
  150.  
  151.    public Dimension getMinimumSize(JComponent var1) {
  152.       return this.getPreferredSize(var1);
  153.    }
  154.  
  155.    public Dimension getMaximumSize(JComponent var1) {
  156.       return this.getPreferredSize(var1);
  157.    }
  158.  
  159.    public Insets getDefaultMargin(AbstractButton var1) {
  160.       return new Insets(2, 2, 2, 2);
  161.    }
  162.  
  163.    public Dimension getPreferredSize(JComponent var1) {
  164.       this.installDefaultIcons();
  165.       return BasicGraphicsUtils.getPreferredMenuItemSize(var1, this.checkIcon, this.menuArrow, 4);
  166.    }
  167.  
  168.    public void processMouseEvent(JMenuItem var1, MouseEvent var2, MenuElement[] var3, MenuSelectionManager var4) {
  169.       Point var5 = var2.getPoint();
  170.       if (((Component)var1).isEnabled()) {
  171.          if (var5.x >= 0 && var5.x < ((JComponent)var1).getWidth() && var5.y >= 0 && var5.y < ((JComponent)var1).getHeight()) {
  172.             JMenu var9 = (JMenu)var1;
  173.             MenuElement[] var7 = var4.getSelectedPath();
  174.             if (var7.length <= 0 || var7[var7.length - 1] != var9.getPopupMenu()) {
  175.                if (var9.isTopLevelMenu() || var9.getDelay() == 0 || ((AWTEvent)var2).getID() == 506) {
  176.                   MenuElement[] var8 = new MenuElement[var3.length + 1];
  177.                   System.arraycopy(var3, 0, var8, 0, var3.length);
  178.                   var8[var3.length] = var9.getPopupMenu();
  179.                   var4.setSelectedPath(var8);
  180.                   return;
  181.                }
  182.  
  183.                var4.setSelectedPath(var3);
  184.                this.setupPostTimer(var9);
  185.             }
  186.  
  187.          } else {
  188.             if (((AWTEvent)var2).getID() == 502) {
  189.                Component var6 = var4.componentForPoint(((ComponentEvent)var2).getComponent(), var2.getPoint());
  190.                if (var6 == null) {
  191.                   var4.clearSelectedPath();
  192.                }
  193.             }
  194.  
  195.          }
  196.       }
  197.    }
  198.  
  199.    private int lower(int var1) {
  200.       return var1 >= 65 && var1 <= 90 ? var1 + 97 - 65 : var1;
  201.    }
  202.  
  203.    public void processKeyEvent(JMenuItem var1, KeyEvent var2, MenuElement[] var3, MenuSelectionManager var4) {
  204.       int var5 = ((AbstractButton)var1).getMnemonic();
  205.       if (var5 != 0) {
  206.          if (((AWTEvent)var2).getID() == 401 && this.lower(var5) == this.lower(var2.getKeyChar())) {
  207.             JPopupMenu var6 = ((JMenu)var1).getPopupMenu();
  208.             MenuElement[] var7 = var6.getSubElements();
  209.             if (var7.length > 0) {
  210.                MenuElement[] var8 = new MenuElement[var3.length + 2];
  211.                System.arraycopy(var3, 0, var8, 0, var3.length);
  212.                var8[var3.length] = var6;
  213.                var8[var3.length + 1] = var7[0];
  214.                var4.setSelectedPath(var8);
  215.             }
  216.  
  217.             ((InputEvent)var2).consume();
  218.          }
  219.  
  220.       }
  221.    }
  222.  
  223.    protected void setupPostTimer(JMenu var1) {
  224.       Timer var2 = new Timer(var1.getDelay(), new PostAction(var1, false));
  225.       var2.setRepeats(false);
  226.       var2.start();
  227.    }
  228.  
  229.    public void menuCanceled(JMenu var1) {
  230.       MenuSelectionManager var2 = MenuSelectionManager.defaultManager();
  231.       if (var2.isComponentPartOfCurrentMenu(var1)) {
  232.          MenuSelectionManager.defaultManager().clearSelectedPath();
  233.       }
  234.  
  235.    }
  236. }
  237.