JToggleButton.java 3.73 KB
Newer Older
1
/* JToggleButton.java -- 
2
   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

38

39 40
package javax.swing;

41 42 43
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.swing.plaf.ButtonUI;
44

45
public class JToggleButton extends AbstractButton implements Accessible
46 47
{

48
  public static class ToggleButtonModel extends DefaultButtonModel
49
  {
Graydon Hoare committed
50 51
    private static final long serialVersionUID = -1589950750899943974L;
  
52
    public void setPressed(boolean b)  
53
    {
54 55
      if (! isEnabled())
        return;
56
      
57 58 59 60 61 62
      super.setPressed(b);
      
      // setPressed(false) == mouse release on us,
      // if we were armed, we flip the selected state.
      if (!b && isArmed())
        setSelected(! isSelected());
63
    }
64 65 66
  }


Graydon Hoare committed
67 68
  private static final long serialVersionUID = -3128248873429850443L;
    
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  public JToggleButton()
  {
    this(null, null);
  }
  public JToggleButton(Action a)
  {
    this();
    setAction(a);
  }

  public JToggleButton(Icon icon)
  { 
    this(null, icon);
  }    
  
Graydon Hoare committed
84 85 86 87 88
  public JToggleButton (Icon icon, boolean selected) 
  {
    this(null, icon, selected);
  }
  
89 90 91 92 93
  public JToggleButton(String text)
  {
    this(text, null);
  }
      
Graydon Hoare committed
94 95
  public JToggleButton(String text, boolean selected)
  {
Graydon Hoare committed
96
    this(text, null, selected);
Graydon Hoare committed
97 98
  }

99 100 101 102
  public JToggleButton(String text, Icon icon)
  {
    this(text, icon, false);
  }
103

104 105 106
  public JToggleButton (String text, Icon icon, boolean selected) 
  {
    super(text, icon);
107

Graydon Hoare committed
108
    horizontalAlignment = LEADING;
109 110 111
    setModel(new ToggleButtonModel());	
    model.setSelected(selected);
  }
112

Graydon Hoare committed
113
  /**
Graydon Hoare committed
114 115 116
   * Gets the AccessibleContext associated with this <code>JToggleButton</code>.
   *
   * @return the associated context
Graydon Hoare committed
117
   */
118 119 120 121
  public AccessibleContext getAccessibleContext()
  {
    return null;
  }
122
  
Graydon Hoare committed
123
  /**
124 125
   * Returns a string that specifies the name of the Look and Feel
   * class that renders this component.
Graydon Hoare committed
126
   */
127 128 129 130
  public String getUIClassID()
  {
    return "ToggleButtonUI";
  }
131
  
132 133 134 135
  protected  String paramString()
  {
    return "JToggleButton";
  }
136 137
  
  
138 139 140 141 142
  public void updateUI()
  {	
    ButtonUI b = (ButtonUI)UIManager.getUI(this);
    setUI(b);
  }
143 144 145 146
}