classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] fixes to swing button implementation


From: graydon hoare
Subject: [PATCH] fixes to swing button implementation
Date: Tue, 06 Jan 2004 15:05:53 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031115 Thunderbird/0.3

hi,

this patch implements JButton as near as I can tell "correctly" -- that is, moving full responsibility for event handling and painting into the plaf, teaching the model about properties and listeners, delegating state from the component to the model, following the spec for state transitions in the model, etc. it also includes some small changes to rendering the button.

ok to commit?

-graydon

2004-01-06  Graydon Hoare  <address@hidden>

        * javax/swing/AbstractButton.java: Re-implement most of class.
        * javax/swing/DefaultButtonModel.java: Likewise.
        * javax/swing/BasicButtonUI.java: Likewise.
        * javax/swing/plaf/basic/BasicGraphicsUtils.java 
(getPreferredButtonSize):
        Incorporate margins into button size.
Index: javax/swing/AbstractButton.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/AbstractButton.java,v
retrieving revision 1.3
diff -u -b -w -r1.3 AbstractButton.java
--- javax/swing/AbstractButton.java     14 Jul 2003 05:33:30 -0000      1.3
+++ javax/swing/AbstractButton.java     6 Jan 2004 20:04:32 -0000
@@ -38,6 +38,8 @@
 
 package javax.swing;
 
+import java.awt.AWTEventMulticaster;
+import java.awt.AWTEvent;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.Insets;
@@ -62,12 +64,16 @@
 import javax.swing.event.ChangeListener;
 import javax.swing.plaf.ButtonUI;
 import javax.swing.text.AttributeSet;
+import java.util.Vector;
+import java.io.Serializable;
 
 /**
  * Provides basic button functionality
  *
  * @author Ronald Veldema (address@hidden)
+ * @author Graydon Hoare (address@hidden)
  */
+
 public abstract class AbstractButton extends JComponent
   implements ItemSelectable, SwingConstants
 {
@@ -85,7 +91,31 @@
   ButtonModel model;
   Insets margin;
 
+  ActionListener actionListener;
+  ItemListener itemListener;
+  
+  // ChangeEvents are not AWTEvents, can't use multicaster
+  Vector changeListeners = new Vector ();
+
+  public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted";
+  public static final String CONTENT_AREA_FILLED_CHANGED_PROPERTY = 
"contentAreaFilled";
+  public static final String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon";
+  public static final String DISABLED_SELECTED_ICON_CHANGED_PROPERTY = 
"disabledSelectedIcon";
   public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";
+  public static final String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = 
"horizontalAlignment";
+  public static final String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = 
"horizontalTextPosition";
+  public static final String ICON_CHANGED_PROPERTY = "icon";
+  public static final String MARGIN_CHANGED_PROPERTY = "margin";
+  public static final String MNEMONIC_CHANGED_PROPERTY = "mnemonic";
+  public static final String MODEL_CHANGED_PROPERTY = "model";
+  public static final String PRESSED_ICON_CHANGED_PROPERTY = "pressedIcon";
+  public static final String ROLLOVER_ENABLED_CHANGED_PROPERTY = 
"rolloverEnabled";
+  public static final String ROLLOVER_ICON_CHANGED_PROPERTY = "rolloverIcon";
+  public static final String ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY = 
"rolloverSelectedIcon";
+  public static final String SELECTED_ICON_CHANGED_PROPERTY = "selectedIcon";
+  public static final String TEXT_CHANGED_PROPERTY = "text";
+  public static final String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = 
"verticalAlignment";
+  public static final String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = 
"verticalTextPosition";
 
   /**
    * AccessibleAbstractButton
@@ -340,7 +370,6 @@
     {
       c.getModel().setArmed(false);
 
-      System.out.println("LOST FOCUS");
       if (c.isFocusPainted())
         {
           c.repaint();
@@ -348,7 +377,6 @@
     }
     public void focusGained(FocusEvent event)
     {
-      System.out.println("GAIN FOCUS");
     }
   }
 
@@ -369,6 +397,9 @@
     addFocusListener( new JFocusListener(this) );
 
     setModel(new DefaultButtonModel(this));
+    model.addActionListener (new ButtonActionListener ());
+    model.addChangeListener (new ButtonChangeListener ());
+    model.addItemListener (new ButtonItemListener ());
 
     updateUI(); // get a proper ui
   }
@@ -386,156 +417,295 @@
   {   getModel().setActionCommand(aCommand);   }
 
   public void addActionListener(ActionListener l)
-  {    getModel().addActionListener(l);    }
+    {  
+       actionListener = AWTEventMulticaster.add (actionListener, l);
+       if (actionListener != null)
+           enableEvents (AWTEvent.ACTION_EVENT_MASK);
+    }   
 
   public void removeActionListener(ActionListener l)
-  {    getModel().removeActionListener(l);    }
-
-  public void addChangeListener(ChangeListener l)
-  {   getModel().addChangeListener(l);     }
-
-  public void removeChangeListener(ChangeListener l)
-  {  getModel().removeChangeListener(l);    }
+    {  
+       actionListener = AWTEventMulticaster.remove (actionListener, l);
+    }
 
   public void addItemListener(ItemListener l)
-  {  getModel().addItemListener(l);    }
+    {  
+       itemListener = AWTEventMulticaster.add (itemListener, l);
+       if (itemListener != null)
+           enableEvents (AWTEvent.ITEM_EVENT_MASK);
+    }
 
   public void removeItemListener(ItemListener l)
-  {  getModel().removeItemListener(l);  }
+    {
+       itemListener = AWTEventMulticaster.remove (itemListener, l);
+    }
 
-  public int getHorizontalAlignment()
-  {    return hori_align;    }
+    public void addChangeListener (ChangeListener l)
+    {   
+       changeListeners.addElement (l);
+    }
 
-  public int getHorizontalTextPosition()
-  {    return hori_text_pos;    }
+    public void removeChangeListener (ChangeListener l)
+    {  
+       changeListeners.removeElement (l);
+    }
 
-  public int getVerticalAlignment()
-  {    return vert_align;   }
+    public void fireItemStateChanged (ItemEvent e)
+    {
+       itemListener.itemStateChanged (e);
+    }
 
-  public int getVerticalTextPosition()
-  {    return vert_text_pos;  }
+    public void fireActionPerformed (ActionEvent e)
+    {
+       actionListener.actionPerformed (e);
+    }
 
-  protected  void fireItemStateChanged(ItemEvent event)
+    public void fireStateChanged (ChangeEvent event)
   {
+       for (int i=0; i < changeListeners.size(); i++)
+           {
+               ChangeListener cl = (ChangeListener) changeListeners.get (i);
+               cl.stateChanged (event);
+           }
   }
   
-  protected  void fireStateChanged(ChangeEvent event)
+    protected void stateChanged()
   {
+       fireStateChanged (new ChangeEvent (this));
+       revalidate ();
+       repaint ();
   }
   
-  protected void fireActionPerformed(ActionEvent event)
+    /*
+     * inner classes used to subscribe to events generated by our model
+     */
+
+    private class ButtonChangeListener 
+       implements ChangeListener, Serializable
+    {
+       public void stateChanged (ChangeEvent e)
   {
+           AbstractButton.this.fireStateChanged (e);
+           AbstractButton.this.revalidate ();
+           AbstractButton.this.repaint ();
+       }
   }
 
-  public void setVerticalAlignment(int alignment)
-  {    vert_align = alignment;    }
+    private class ButtonActionListener
+       implements ActionListener, Serializable
+    {
+       public void actionPerformed(ActionEvent e)
+       {
+           AbstractButton.this.fireActionPerformed (e);
+       }       
+    }
 
-  public void setHorizontalAlignment(int alignment)
-  {   hori_align = alignment;   }
+    private class ButtonItemListener
+       implements ItemListener, Serializable
+    {
+       public void itemStateChanged (ItemEvent e)
+       {
+           AbstractButton.this.fireItemStateChanged (e);
+       }
+    }
 
-  public void setVerticalTextPosition(int textPosition)
-  {    vert_text_pos = textPosition;    }
 
-  public void setHorizontalTextPosition(int textPosition)
-  {   hori_text_pos = textPosition;   }
+    /* 
+     * delegated properties
+     */
 
   public int getMnemonic()
-  {    return getModel().getMnemonic();    }
+    {
+       return getModel ().getMnemonic ();
+    }
 
   public void setMnemonic(char mne)
-  {    getModel().setMnemonic(mne);    }
+    {
+       int old = getModel ().getMnemonic ();
+       getModel ().setMnemonic (mne);
+       firePropertyChange (MNEMONIC_CHANGED_PROPERTY, old, (int) mne);
+    }
 
   public void setMnemonic(int mne)
-  {    getModel().setMnemonic(mne);    }
+    {
+       int old = mne;
+       getModel ().setMnemonic (mne);
+       firePropertyChange (MNEMONIC_CHANGED_PROPERTY, old, mne);
+    }
 
-  public void setRolloverEnabled(boolean b)
-  {    getModel().setRollover(b);    }
+    public void setRolloverEnabled (boolean r)
+    {
+       boolean old = getModel ().isRollover ();
+       getModel ().setRollover (r);
+       firePropertyChange (ROLLOVER_ENABLED_CHANGED_PROPERTY, old, r);
+    }
 
   public boolean isRolloverEnabled()
-  {    return getModel().isRollover();     }
+    {
+       return getModel ().isRollover ();
+    }
 
-  public boolean isBorderPainted()
-  {    return paint_border;    }
+    public void setSelected(boolean s)
+    {
+       getModel ().setSelected (s);
+    }
 
-  public void setBorderPainted(boolean b)
+    public boolean isSelected()
   {
-    if (b != paint_border)
+       return getModel ().isSelected ();
+    }
+
+    public void setEnabled (boolean b)
       {
-        paint_border = b;
+       super.setEnabled (b);
+       getModel ().setEnabled (b);
+    }
+
+
+
+    /*
+     * intrinsic properties
+     */
+
+
+    public int getHorizontalAlignment ()
+    {  
+       return hori_align;
+    }
+
+    public void setHorizontalAlignment (int a)
+    {
+       int old = hori_align;
+       hori_align = a;
+       firePropertyChange (HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, old, a);
         revalidate();
         repaint();
       }
+
+    public int getHorizontalTextPosition ()
+    {  
+       return hori_text_pos;
   }
 
-  public Action getAction()
-  {    return action_taken;    }
+    public void setHorizontalTextPosition (int t)
+    {
+       int old = hori_text_pos;
+       hori_text_pos = t;
+       firePropertyChange (HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, old, t);
+       revalidate ();
+       repaint ();                     
+    }
 
-  public void setAction(Action a)
+    public int getVerticalAlignment ()
   {
-    action_taken = a;
+       return vert_align;
+    }
+
+    public void setVerticalAlignment (int a)
+    {
+       int old = vert_align;
+       vert_align = a;
+       firePropertyChange (VERTICAL_ALIGNMENT_CHANGED_PROPERTY, old, a);
     revalidate();
     repaint();
   }
 
-  public void setSelected(boolean b)
-  {    getModel().setSelected(b);    }
+    public int getVerticalTextPosition ()
+    {
+       return vert_text_pos;
+    }
 
-  public boolean isSelected()
-  {    return getModel().isSelected();     }
+    public void setVerticalTextPosition (int t)
+    {
+       int old = vert_text_pos;
+       vert_text_pos = t;
+       firePropertyChange (VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, old, t);
+       revalidate ();
+       repaint ();                     
+    }
 
-  public Icon getIcon()
-  {    return default_icon;    }
 
-  public void setIcon(Icon defaultIcon)
+    public boolean isBorderPainted ()
   {
-    if (default_icon == defaultIcon)
-      return;
+       return paint_border;
+    }
 
-    default_icon = defaultIcon;
-    if (default_icon != null)
+    public void setBorderPainted (boolean b)
       {
-        // XXX FIXME - icons do not know their parent
-        //                     default_icon.setParent(this);
+       boolean old = paint_border;
+       paint_border = b;
+       firePropertyChange (ROLLOVER_ENABLED_CHANGED_PROPERTY, old, b);
+       revalidate ();
+       repaint ();
       }
+
+    public Action getAction ()
+    {
+       return action_taken;
+    }
+
+    public void setAction (Action a)
+    {
+       action_taken = a;
+    }
+
+    public Icon getIcon()
+    {
+       return default_icon;
+    }
+
+    public void setIcon(Icon i)
+    {
+       Icon old = default_icon;
+       default_icon = i;
+       firePropertyChange (ICON_CHANGED_PROPERTY, old, i);
     revalidate();
     repaint();
   }
 
   public String getText()
-  {    return text;    }
+    {
+       return text;
+    }
 
   public void setLabel(String label)
-  {    setText(label);    }
+    {
+       setText (label);
+    }
 
   public String getLabel()
-  {    return getText();    }
+    {
+       return getText ();
+    }
 
-  public void setText(String text)
+    public void setText (String t)
   {
-    this.text = text;
+       String old = text;
+       text = t;
+       firePropertyChange (TEXT_CHANGED_PROPERTY, old, t);
     revalidate();
     repaint();
   }
 
   public       Insets getMargin()
-  {      return margin; }
+    {
+       return margin; 
+    }
 
   public void setMargin(Insets m)
   {
+       Insets old = margin;
     margin = m;
+       firePropertyChange (MARGIN_CHANGED_PROPERTY, old, m);
     revalidate();
     repaint();
   }
 
-  public void setEnabled(boolean b)
-  {
-    super.setEnabled(b);
-    getModel().setEnabled(b);
-    repaint();
-  }
 
   public Icon getPressedIcon()
-  {    return pressed_button;    }
+    {
+       return pressed_button;
+    }
 
   public void setPressedIcon(Icon pressedIcon)
   {
@@ -545,7 +715,9 @@
   }
 
   public Icon getDisabledIcon()
-  {    return disabled_button;    }
+    {
+       return disabled_button;
+    }
 
   public void setDisabledIcon(Icon disabledIcon)
   {
@@ -555,16 +727,17 @@
   }
 
   public boolean isFocusPainted()
-  {   return paint_focus;   }
+    {
+       return paint_focus;
+    }
 
   public void setFocusPainted(boolean b)
   {
     boolean old = paint_focus;
     paint_focus = b;
 
-    firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY,
-                       old,
-                       b);
+       firePropertyChange (FOCUS_PAINTED_CHANGED_PROPERTY, old, b);
+
     if (hasFocus())
       {
         revalidate();
@@ -572,6 +745,7 @@
       }
   }
 
+
   public boolean isFocusTraversable()
   {
     //Identifies whether or not this component can receive the focus.
@@ -738,64 +912,4 @@
     }
     */
   }
-
-  protected void processActionEvent(ActionEvent e)
-  {
-    System.out.println("PROCESS-ACTION-EVENT: " + e);
-  }
-
-  protected void processMouseEvent(MouseEvent e)
-  {
-    // System.out.println("PROCESS-MOUSE-EVENT: " + e + ", 
PRESSED-IN-MODEL="+getModel().isPressed());
-
-    switch (e.getID())
-      {
-      case MouseEvent.MOUSE_MOVED:
-        {
-          break;
-        }
-      case MouseEvent.MOUSE_PRESSED:
-        {
-          if (! isEnabled())
-            {
-              System.out.println("button not enabled, ignoring press");
-            }
-          else
-            {
-              System.out.println("telling model:press: " + getModel());
-              getModel().setPressed(true);
-              repaint();
-            }
-          break;
-        }
-
-      case MouseEvent.MOUSE_RELEASED:
-        {
-          if (! isEnabled())
-            {
-              System.out.println("button not enabled, ignoring release");
-            }
-          else
-            {
-              int flags = 0;
-
-              System.out.println("        XXX--> " + getActionCommand());
-
-              fireActionPerformed(new ActionEvent(this,
-                                                  ActionEvent.ACTION_PERFORMED,
-                                                  getActionCommand(),
-                                                  flags));
-
-              //System.out.println("telling model:release");
-              getModel().setPressed(false);
-              repaint();
-            }
-          break;
-        }
-      case MouseEvent.MOUSE_CLICKED:
-        {
-          break;
-        }
-      }
-  }
 }
Index: javax/swing/DefaultButtonModel.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/DefaultButtonModel.java,v
retrieving revision 1.3
diff -u -b -w -r1.3 DefaultButtonModel.java
--- javax/swing/DefaultButtonModel.java 11 Jun 2003 13:20:39 -0000      1.3
+++ javax/swing/DefaultButtonModel.java 6 Jan 2004 20:04:32 -0000
@@ -38,6 +38,8 @@
 
 package javax.swing;
 
+import java.awt.AWTEvent;
+import java.awt.AWTEventMulticaster;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
@@ -54,125 +56,227 @@
 {
   static final long serialVersionUID = -5342609566534980231L;
 
-    Vector actions          = new Vector();
 
-    Vector items    = new Vector();
-    Vector changes  = new Vector();
+    ActionListener actionListener;
+    ItemListener itemListener;
+
+    // ChangeEvents are not AWTEvents
+    Vector changeListeners = new Vector ();
+
     ButtonGroup group;
     JComponent comp;
 
+    boolean armed = false;
+    boolean enabled = true;
+    boolean pressed = false;
+    boolean rolledOver = false;
+    boolean selected = false;
+    int mnemonic;  
+    String actionCommand;
     
     DefaultButtonModel(JComponent a)
     {
        comp = a;
     }
 
-
     public Object[] getSelectedObjects()
     {
        return null;
     }
 
 
-    public void fireItemStateChanged(ItemEvent event)
+    /* 
+     * event listener and firing methods
+     */
+
+    public void addActionListener (ActionListener l)
+    {  
+       actionListener = AWTEventMulticaster.add (actionListener, l);
+    }   
+    
+    public void removeActionListener (ActionListener l)
+    {  
+       actionListener = AWTEventMulticaster.remove (actionListener, l);
+    }
+    
+    public void addItemListener (ItemListener l)
+    {  
+       itemListener = AWTEventMulticaster.add (itemListener, l);
+    }
+
+    public void removeItemListener (ItemListener l)
     {
-       for (int i=0;i<items.size();i++)
+       itemListener = AWTEventMulticaster.remove (itemListener, l);
+    }
+
+    public void addChangeListener (ChangeListener l)
            {
-               ItemListener a = (ItemListener) items.get(i);
-               a.itemStateChanged(event);
+       changeListeners.addElement (l);
            }
+    
+    public void removeChangeListener (ChangeListener l)
+    {  
+       changeListeners.removeElement (l);
     }
+
+    public void fireItemStateChanged (ItemEvent e)
+    {
+       itemListener.itemStateChanged (e);
+    }
+
+    public void fireActionPerformed (ActionEvent e)
+    {
+       actionListener.actionPerformed (e);
+    }
+
     public void fireStateChanged(ChangeEvent event)
     {
-       for (int i=0;i<changes.size();i++)
+       for (int i=0; i < changeListeners.size(); i++)
            {
-               ChangeListener a = (ChangeListener) changes.get(i);
-               a.stateChanged(event);
+               ChangeListener cl = (ChangeListener) changeListeners.get (i);
+               cl.stateChanged (event);
            }
     }
-    public void fireActionPerformed(ActionEvent event)
+
+    protected void stateChanged()
     {
-       for (int i=0;i<actions.size();i++)
+       fireStateChanged (new ChangeEvent (this));
+    }
+    
+
+    /* 
+     * property accessors
+     */
+
+    public boolean isArmed() 
+    { 
+       return armed; 
+    }
+
+    public void setArmed(boolean a)
+    { 
+       if (armed != a)
            {
-               ActionListener a = (ActionListener) actions.get(i);
-               a.actionPerformed(event);
+               armed = a; 
+               stateChanged ();
            }
     }
 
-    boolean arm;
-    public boolean isArmed()          { return arm; }
-    public void setArmed(boolean b)   { arm = b; }
+    public boolean isEnabled()
+    {
+       return enabled; 
+    }
 
-    boolean enabled = true;
-    public boolean isEnabled()         { return enabled; }
-    public void setEnabled(boolean b)  { enabled = b; }
+    public void setEnabled (boolean e)
+    {
+       if (enabled != e)
+           {
+               enabled = e; 
+               stateChanged ();
+           }
+    }
 
-    boolean pressed;
-    public void setPressed(boolean b)  
+    public void setPressed (boolean p)  
     {
-       pressed = b; 
+       if (p != pressed)
+           {
+               pressed = p;
+               stateChanged ();
     }
-    public boolean isPressed()         { return pressed; }
 
+       if (enabled && armed && !pressed)
+           {
+               int flags = 0;
+               fireActionPerformed (new ActionEvent(this,
+                                                    
ActionEvent.ACTION_PERFORMED,
+                                                    getActionCommand(), 
flags));
+           }
+    }
 
-    public void removeActionListener(ActionListener l) { 
actions.removeElement(l); }
-    public void addActionListener(ActionListener l)    
+    public boolean isPressed ()
     {  
-       //      comp.enableEvents( AWTEvent.ACTION_EVENT_MASK );
-       actions.addElement(l);    
+       return pressed;
     }
 
-    public void addItemListener(ItemListener l)        { items.addElement(l); }
-    public void removeItemListener(ItemListener l)     { 
items.removeElement(l); }
+    public void setRollover (boolean r)
+    {
+       if (rolledOver != r)
+           {
+               rolledOver = r; 
+               stateChanged ();
+           }
+    }
 
-    public void addChangeListener(ChangeListener l)    { 
changes.addElement(l); }
-    public void removeChangeListener(ChangeListener l) { 
changes.removeElement(l); }
+    public boolean isRollover () 
+    { 
+       return rolledOver;
+    }
 
-    boolean roll;
-    public void setRollover(boolean b) { roll = b; }
-    public boolean isRollover()        { return roll; }
+    public int getMnemonic ()
+    {
+       return mnemonic;
+    }
 
-    int mne;  
-    public int  getMnemonic()        { return mne; }
-    public void setMnemonic(int key) { mne = key; }
+    public void setMnemonic (int key)
+    {
+       if (mnemonic != key)
+           {
+               mnemonic = key;
+               stateChanged ();
+           }
+    }
 
-    String com;
-    public void setActionCommand(String s) { com = s; }
-    public String getActionCommand()       { return com; }
+    public void setActionCommand (String s)
+    {
+       if (actionCommand != s)
+           {
+               actionCommand = s;
+               stateChanged ();
+           }
+    }
+
+    public String getActionCommand ()
+    { 
+       return actionCommand; 
+    }
 
-    public void setGroup(ButtonGroup group)
+    public void setGroup (ButtonGroup g)
+    {
+       if (group != g)
     {
-       this.group = group;
+               group = g;
+               stateChanged ();
+           }
     }
 
-    boolean sel;
-    public void setSelected(boolean b) 
+    public void setSelected(boolean s) 
     { 
        if (group != null)
            {
-               if (b == true)
+               if (s == true)
                    {
-                       System.out.println("selected button in group:"+this);
-                       group.setSelected(this, b);
-                       sel = true;
+                       group.setSelected(this, s);
+                       selected = true;
+                       stateChanged ();
                    }
                else
                    {
-                       System.out.println("deselected button in group: " + 
this);
-                       sel = false;
+                       selected = false;
+                       stateChanged ();
                    }
            } 
        else
            {
-               sel = b;
+               if (selected != s)
+                   {
+                       selected = s;
+                       stateChanged ();
            }
     }
-    public boolean isSelected()        { return sel; }
 }
 
-
-
-
-
-
-
+    public boolean isSelected ()
+    {
+       return selected;
+    }
+}
Index: javax/swing/plaf/basic/BasicButtonUI.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicButtonUI.java,v
retrieving revision 1.3
diff -u -b -w -r1.3 BasicButtonUI.java
--- javax/swing/plaf/basic/BasicButtonUI.java   19 Jun 2003 10:48:46 -0000      
1.3
+++ javax/swing/plaf/basic/BasicButtonUI.java   6 Jan 2004 20:04:32 -0000
@@ -41,6 +41,7 @@
 import javax.swing.*;
 import javax.swing.plaf.*;
 import java.awt.*;
+import java.awt.event.*;
 
 
 public class BasicButtonUI extends ButtonUI
@@ -52,13 +53,68 @@
     Color pressedBackgroundColor;
     Color normalBackgroundColor;
 
-
     public static ComponentUI createUI(final JComponent c) 
     {
        return new BasicButtonUI();
     }
 
     
+    static private class FocusUIListener extends FocusAdapter
+    {
+       AbstractButton button;
+
+       FocusUIListener (AbstractButton b)
+       {
+           button = b;
+       }
+       
+       public void focusLost(FocusEvent event)
+       {
+           button.getModel ().setArmed (false);
+       }
+    }
+
+    static private class ButtonUIListener extends MouseAdapter
+    {
+       AbstractButton button;
+
+       public ButtonUIListener (AbstractButton b)
+       {
+           button = b;
+       }
+
+       public void mousePressed (MouseEvent e) 
+       {
+           if ((e.getModifiers () & InputEvent.BUTTON1_MASK) != 0)
+               {
+                   button.getModel ().setPressed (true);
+                   button.getModel ().setArmed (true);
+               }
+       }
+
+       public void mouseEntered (MouseEvent e) 
+       {
+           if ((e.getModifiers () & InputEvent.BUTTON1_MASK) != 0)
+               {
+                   button.getModel ().setPressed (true);
+               }
+       }
+
+       public void mouseExited (MouseEvent e) 
+       {
+           button.getModel ().setArmed (false);
+       }
+
+       public void mouseReleased (MouseEvent e) 
+       {
+           if ((e.getModifiers () & InputEvent.BUTTON1_MASK) != 0)
+               {
+                   button.getModel ().setPressed (false);
+                   button.getModel ().setArmed (false);
+               }
+       }               
+    }
+    
     public void installUI(final JComponent c) 
     {
        super.installUI(c);
@@ -68,6 +124,12 @@
        pressedBackgroundColor   = new Color(150,150,150);
        pressedBackgroundColor   = new Color(150,150,150);
        normalBackgroundColor    = new Color(192,192,192);
+
+       // this tells the border (if we have one) how to paint.
+       c.setBackground (normalBackgroundColor);
+
+       c.addMouseListener (new ButtonUIListener ((AbstractButton) c));
+       c.addFocusListener (new FocusUIListener ((AbstractButton) c));
     }
     
 
@@ -87,6 +149,7 @@
        Rectangle tr = new Rectangle();
        Rectangle ir = new Rectangle();
        Rectangle vr = new Rectangle();
+       Rectangle br = new Rectangle();
 
         Font f = c.getFont();
 
@@ -94,14 +157,20 @@
 
         FontMetrics fm = g.getFontMetrics(f);
 
-        Insets i = c.getInsets();
+       Insets border = b.getInsets();
+       Insets margin = b.getMargin();
 
-        vr.x      = i.left;
-        vr.y      = i.top;
-        vr.width  = b.getWidth()  - (i.right  + vr.x);
-        vr.height = b.getHeight() - (i.bottom + vr.y);
+       br.x      = border.left;
+       br.y      = border.top;
+       br.width  = b.getWidth() - (border.right + border.left);
+       br.height = b.getHeight() - (border.top + border.bottom);
+
+        vr.x      = br.x + margin.left;
+        vr.y      = br.y + margin.top;
+        vr.width  = br.width  - (margin.right + margin.left);
+        vr.height = br.height - (margin.top + margin.bottom);
        
-       //System.out.println("             VIEW-RECT-BUTTON="+vr+", 
insets="+i+", FONTM="+fm);
+       // System.out.println("             VIEW-RECT-BUTTON="+vr+", 
insets="+border+" margin="+margin+", FONTM="+fm);
        
        String text = SwingUtilities.layoutCompoundLabel(c,
                                                         fm, 
@@ -116,17 +185,11 @@
                                                         tr,
                                                         gap);
 
-        if (b.getModel().isPressed() ||
+        if (b.getModel().isArmed () ||
            b.getModel().isSelected())
-           {
-             //System.out.println("paint pressed");
-               paintButtonPressed(g, c);
-           }
+           paintButtonPressed (g, br, c);
        else
-           {
-             //System.out.println("paint normal");
-               paintButtonNormal(g, c);
-           }
+           paintButtonNormal (g, br, c);
        
        paintIcon(g, c, ir);
        paintText(g, c, tr, b.getText());
@@ -152,7 +215,7 @@
                int x = iconRect.x;
                int y = iconRect.y;
 
-               System.out.println("WE HAVE AN ICON: " + b.getIcon());
+               // System.out.println("WE HAVE AN ICON: " + b.getIcon());
  
                b.getIcon().paintIcon(c, g, x, y);
            }
@@ -163,22 +226,25 @@
     }
 
     protected void paintButtonPressed(Graphics g,
+                                     Rectangle area, 
                                      JComponent b)
     {
        Dimension size = b.getSize();
        
        g.setColor(pressedBackgroundColor);
-       g.fillRect(1,1,size.width-2, size.height-2);                
-
+       g.fillRect(area.x, area.y,
+                  area.width, area.height);
     }
     
     protected void paintButtonNormal(Graphics g,
+                                    Rectangle area, 
                                     JComponent b)
     {
        Dimension size = b.getSize();
        
        g.setColor(normalBackgroundColor);
-       g.fillRect(1,1,size.width-2, size.height-2);                
+       g.fillRect(area.x, area.y,
+                  area.width, area.height);
 
     }
     
@@ -199,7 +265,7 @@
                                      text, 
                                      0,
                                      textRect.x, 
-                                     textRect.y + fm.getAscent()/2);
+                                     textRect.y + fm.getAscent ());
     } 
 }
 
Index: javax/swing/plaf/basic/BasicGraphicsUtils.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicGraphicsUtils.java,v
retrieving revision 1.4
diff -u -b -w -r1.4 BasicGraphicsUtils.java
--- javax/swing/plaf/basic/BasicGraphicsUtils.java      1 Aug 2003 20:10:22 
-0000       1.4
+++ javax/swing/plaf/basic/BasicGraphicsUtils.java      6 Jan 2004 20:04:32 
-0000
@@ -595,6 +595,7 @@
     Rectangle iconRect = new Rectangle();
     Rectangle textRect = new Rectangle();
     Insets insets = b.getInsets();
+    Insets margin = b.getMargin();
     
     /* For determining the ideal size, do not assume a size restriction. */
     viewRect = new Rectangle(0, 0,
@@ -630,7 +631,11 @@
      */
     contentRect = textRect.union(iconRect);
 
-    return new Dimension(insets.left + contentRect.width + insets.right,
-                         insets.top + contentRect.height + insets.bottom);
+    return new Dimension(insets.left + margin.left
+                        + contentRect.width 
+                        + insets.right + margin.right,
+                         insets.top + margin.top
+                        + contentRect.height 
+                        + insets.bottom + margin.bottom);
   }
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]