home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / BasicRadioButtonMenuItemUI.java < prev    next >
Text File  |  1998-02-26  |  6KB  |  198 lines

  1. /*
  2.  * @(#)BasicRadioButtonMenuItemUI.java    1.33 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import com.sun.java.swing.*;
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import com.sun.java.swing.plaf.*;
  27. import com.sun.java.swing.border.*;
  28. import java.io.Serializable;
  29.  
  30. /**
  31.  * BasicRadioButtonMenuItem implementation
  32.  * <p>
  33.  * Warning: serialized objects of this class will not be compatible with
  34.  * future swing releases.  The current serialization support is appropriate 
  35.  * for short term storage or RMI between Swing1.0 applications.  It will
  36.  * not be possible to load serialized Swing1.0 objects with future releases
  37.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  38.  * baseline for the serialized form of Swing objects.
  39.  *
  40.  * @version 1.33 02/02/98
  41.  * @author Georges Saab
  42.  * @author David Karlton
  43.  */
  44. public class BasicRadioButtonMenuItemUI extends RadioButtonMenuItemUI implements Serializable
  45. {
  46.     protected static Color pressedBackground = null;
  47.     protected static Color pressedForeground = null;
  48.     protected Icon menuArrow = null;
  49.     protected Icon checkIcon = null;
  50.  
  51.     // visual constants
  52.     protected static final int defaultTextIconGap = 4;
  53.  
  54.     protected MouseListener       mouseListener;
  55.     protected MouseMotionListener mouseMotionListener;
  56.  
  57.     protected boolean oldBorderPainted;
  58.  
  59.     public static ComponentUI createUI(JComponent b) {
  60.         return new BasicRadioButtonMenuItemUI();
  61.     }
  62.  
  63.     public void installUI(JComponent c) {
  64.     JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) c;
  65.     initListeners(c);
  66.     addListeners(c);
  67.     // Set defaults
  68.     c.setOpaque(true);
  69.     LookAndFeel.installBorder(c,"MenuItem.border");
  70.     oldBorderPainted = menuItem.isBorderPainted();
  71.     menuItem.setBorderPainted( ( (Boolean) (UIManager.get("MenuItem.borderPainted")) ).booleanValue() );
  72.     LookAndFeel.installColorsAndFont(c,
  73.                           "MenuItem.background",
  74.                           "MenuItem.foreground",
  75.                           "MenuItem.font");
  76.     // MenuItem specific defaults
  77.     if (pressedBackground == null || 
  78.         pressedBackground instanceof UIResource) {
  79.         pressedBackground = 
  80.         UIManager.getColor("MenuItem.pressedBackground");
  81.     }
  82.     if (pressedForeground == null || 
  83.         pressedForeground instanceof UIResource) {
  84.         pressedForeground = 
  85.         UIManager.getColor("MenuItem.pressedForeground");
  86.     }
  87.     if (menuArrow == null ||
  88.         menuArrow instanceof UIResource) {
  89.         menuArrow = UIManager.getIcon("MenuItem.arrowIcon");
  90.     }
  91.     if (menuItem.getSelectedIcon() == null ||
  92.         menuItem.getSelectedIcon() instanceof UIResource) {
  93.         menuItem.setSelectedIcon(UIManager.getIcon("RadioButtonMenuItem.icon"));
  94.     }
  95.     }
  96.  
  97.     public void uninstallUI(JComponent c) {
  98.     JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) c;
  99.     removeListeners(c);
  100.     LookAndFeel.uninstallBorder(c);
  101.     ((JMenuItem) c).setBorderPainted( oldBorderPainted );
  102.     if (menuArrow instanceof UIResource)
  103.         menuArrow = null;
  104.     if (checkIcon instanceof UIResource)
  105.         checkIcon = null;
  106.         if (menuItem.getSelectedIcon() instanceof UIResource) {
  107.             menuItem.setSelectedIcon(null);
  108.         }
  109.     }
  110.  
  111.     protected void initListeners(JComponent c) {
  112.         mouseListener = createMouseListener(c);
  113.         mouseMotionListener  = createMouseMotionListener(c);
  114.     }
  115.  
  116.     protected void addListeners(JComponent c) {
  117.         c.addMouseListener(mouseListener);    
  118.         c.addMouseMotionListener(mouseMotionListener);
  119.     }
  120.  
  121.     protected void removeListeners(JComponent c) {
  122.         c.removeMouseListener(mouseListener);
  123.         c.removeMouseMotionListener(mouseMotionListener);
  124.     }
  125.  
  126.     protected MouseListener createMouseListener(JComponent c) {
  127.     return new BasicMenuMouseListener();
  128.     }
  129.  
  130.     protected MouseMotionListener createMouseMotionListener(JComponent c) {
  131.         return new BasicMenuMouseMotionListener();
  132.     }
  133.  
  134.     public Insets getDefaultMargin(AbstractButton c) { 
  135.         return new Insets(2,2,2,2);
  136.     }
  137.  
  138.     public Dimension getMinimumSize(JComponent c) {
  139.         return getPreferredSize(c);
  140.     }
  141.  
  142.     public Dimension getPreferredSize(JComponent c) {
  143.     return BasicGraphicsUtils.getPreferredMenuItemSize(c,
  144.                                checkIcon, 
  145.                                menuArrow, 
  146.                                defaultTextIconGap);
  147.     }
  148.  
  149.     public Dimension getMaximumSize(JComponent c) {
  150.         return getPreferredSize(c);
  151.     }
  152.  
  153.     /**
  154.      * We draw the background in BasicGraphicsUtils.paintMenuItem()
  155.      * so override update (which fills the background of opaque
  156.      * components by default) to just call paint().
  157.      *
  158.      * @see BasicGraphicsUtils#paintMenuItem
  159.      */
  160.     public void update(Graphics g, JComponent c) {
  161.         paint(g, c);
  162.     }
  163.  
  164.     public void paint(Graphics g, JComponent c) {
  165.     BasicGraphicsUtils.paintMenuItem(g, c, ((JRadioButtonMenuItem)c).getSelectedIcon(), menuArrow,
  166.                      pressedBackground, pressedForeground,
  167.                      defaultTextIconGap);    
  168.     }
  169.  
  170.  
  171.     public void processMouseEvent(JMenuItem item,MouseEvent e,MenuElement path[],MenuSelectionManager manager) {
  172.         Point p = e.getPoint();
  173.         if(p.x >= 0 && p.x < item.getWidth() &&
  174.            p.y >= 0 && p.y < item.getHeight()) {
  175.             if(e.getID() == MouseEvent.MOUSE_RELEASED) {
  176.                 manager.clearSelectedPath();
  177.                 item.doClick(0);
  178.                 item.setArmed(false);
  179.             } else
  180.                 manager.setSelectedPath(path);
  181.         } else if(item.getModel().isArmed()) {
  182.             MenuElement newPath[] = new MenuElement[path.length-1];
  183.             int i,c;
  184.             for(i=0,c=path.length-1;i<c;i++)
  185.                 newPath[i] = path[i];
  186.             manager.setSelectedPath(newPath);
  187.         }
  188.     }
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.