home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 73XY51 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  6.4 KB  |  157 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.JMenuItem;
  7. import com.sun.java.swing.JRadioButtonMenuItem;
  8. import com.sun.java.swing.LookAndFeel;
  9. import com.sun.java.swing.MenuElement;
  10. import com.sun.java.swing.MenuSelectionManager;
  11. import com.sun.java.swing.UIManager;
  12. import com.sun.java.swing.plaf.ComponentUI;
  13. import com.sun.java.swing.plaf.RadioButtonMenuItemUI;
  14. import com.sun.java.swing.plaf.UIResource;
  15. import java.awt.AWTEvent;
  16. import java.awt.Color;
  17. import java.awt.Component;
  18. import java.awt.Dimension;
  19. import java.awt.Graphics;
  20. import java.awt.Insets;
  21. import java.awt.Point;
  22. import java.awt.event.MouseEvent;
  23. import java.awt.event.MouseListener;
  24. import java.awt.event.MouseMotionListener;
  25. import java.io.Serializable;
  26.  
  27. public class BasicRadioButtonMenuItemUI extends RadioButtonMenuItemUI implements Serializable {
  28.    protected static Color pressedBackground = null;
  29.    protected static Color pressedForeground = null;
  30.    protected Icon menuArrow = null;
  31.    protected Icon checkIcon = null;
  32.    protected static final int defaultTextIconGap = 4;
  33.    protected MouseListener mouseListener;
  34.    protected MouseMotionListener mouseMotionListener;
  35.    protected boolean oldBorderPainted;
  36.  
  37.    protected void addListeners(JComponent c) {
  38.       ((Component)c).addMouseListener(this.mouseListener);
  39.       ((Component)c).addMouseMotionListener(this.mouseMotionListener);
  40.    }
  41.  
  42.    protected MouseListener createMouseListener(JComponent c) {
  43.       return new BasicMenuMouseListener();
  44.    }
  45.  
  46.    protected MouseMotionListener createMouseMotionListener(JComponent c) {
  47.       return new BasicMenuMouseMotionListener();
  48.    }
  49.  
  50.    public static ComponentUI createUI(JComponent b) {
  51.       return new BasicRadioButtonMenuItemUI();
  52.    }
  53.  
  54.    public Insets getDefaultMargin(AbstractButton c) {
  55.       return new Insets(2, 2, 2, 2);
  56.    }
  57.  
  58.    public Dimension getMaximumSize(JComponent c) {
  59.       return this.getPreferredSize(c);
  60.    }
  61.  
  62.    public Dimension getMinimumSize(JComponent c) {
  63.       return this.getPreferredSize(c);
  64.    }
  65.  
  66.    public Dimension getPreferredSize(JComponent c) {
  67.       return BasicGraphicsUtils.getPreferredMenuItemSize(c, this.checkIcon, this.menuArrow, 4);
  68.    }
  69.  
  70.    protected void initListeners(JComponent c) {
  71.       this.mouseListener = this.createMouseListener(c);
  72.       this.mouseMotionListener = this.createMouseMotionListener(c);
  73.    }
  74.  
  75.    public void installUI(JComponent c) {
  76.       JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem)c;
  77.       this.initListeners(c);
  78.       this.addListeners(c);
  79.       c.setOpaque(true);
  80.       LookAndFeel.installBorder(c, "MenuItem.border");
  81.       this.oldBorderPainted = ((AbstractButton)menuItem).isBorderPainted();
  82.       ((AbstractButton)menuItem).setBorderPainted((Boolean)UIManager.get("MenuItem.borderPainted"));
  83.       LookAndFeel.installColorsAndFont(c, "MenuItem.background", "MenuItem.foreground", "MenuItem.font");
  84.       if (pressedBackground == null || pressedBackground instanceof UIResource) {
  85.          pressedBackground = UIManager.getColor("MenuItem.pressedBackground");
  86.       }
  87.  
  88.       if (pressedForeground == null || pressedForeground instanceof UIResource) {
  89.          pressedForeground = UIManager.getColor("MenuItem.pressedForeground");
  90.       }
  91.  
  92.       if (this.menuArrow == null || this.menuArrow instanceof UIResource) {
  93.          this.menuArrow = UIManager.getIcon("MenuItem.arrowIcon");
  94.       }
  95.  
  96.       if (((AbstractButton)menuItem).getSelectedIcon() == null || ((AbstractButton)menuItem).getSelectedIcon() instanceof UIResource) {
  97.          ((AbstractButton)menuItem).setSelectedIcon(UIManager.getIcon("RadioButtonMenuItem.icon"));
  98.       }
  99.  
  100.    }
  101.  
  102.    public void paint(Graphics g, JComponent c) {
  103.       BasicGraphicsUtils.paintMenuItem(g, c, ((JRadioButtonMenuItem)c).getSelectedIcon(), this.menuArrow, pressedBackground, pressedForeground, 4);
  104.    }
  105.  
  106.    public void processMouseEvent(JMenuItem item, MouseEvent e, MenuElement[] path, MenuSelectionManager manager) {
  107.       Point p = e.getPoint();
  108.       if (p.x >= 0 && p.x < ((JComponent)item).getWidth() && p.y >= 0 && p.y < ((JComponent)item).getHeight()) {
  109.          if (((AWTEvent)e).getID() == 502) {
  110.             manager.clearSelectedPath();
  111.             ((AbstractButton)item).doClick(0);
  112.             item.setArmed(false);
  113.          } else {
  114.             manager.setSelectedPath(path);
  115.          }
  116.       } else if (((AbstractButton)item).getModel().isArmed()) {
  117.          MenuElement[] newPath = new MenuElement[path.length - 1];
  118.          int i = 0;
  119.  
  120.          for(int c = path.length - 1; i < c; ++i) {
  121.             newPath[i] = path[i];
  122.          }
  123.  
  124.          manager.setSelectedPath(newPath);
  125.       }
  126.  
  127.    }
  128.  
  129.    protected void removeListeners(JComponent c) {
  130.       ((Component)c).removeMouseListener(this.mouseListener);
  131.       ((Component)c).removeMouseMotionListener(this.mouseMotionListener);
  132.    }
  133.  
  134.    public void uninstallUI(JComponent c) {
  135.       JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem)c;
  136.       this.removeListeners(c);
  137.       LookAndFeel.uninstallBorder(c);
  138.       ((JMenuItem)c).setBorderPainted(this.oldBorderPainted);
  139.       if (this.menuArrow instanceof UIResource) {
  140.          this.menuArrow = null;
  141.       }
  142.  
  143.       if (this.checkIcon instanceof UIResource) {
  144.          this.checkIcon = null;
  145.       }
  146.  
  147.       if (((AbstractButton)menuItem).getSelectedIcon() instanceof UIResource) {
  148.          ((AbstractButton)menuItem).setSelectedIcon((Icon)null);
  149.       }
  150.  
  151.    }
  152.  
  153.    public void update(Graphics g, JComponent c) {
  154.       this.paint(g, c);
  155.    }
  156. }
  157.