1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4   
     5  import org.aswing.*;
     6   
     7   
     8  /**
     9   * The default implementation of a Button component's data model.
    10   * @author iiley
    11   */
    12  class org.aswing.DefaultButtonModel extends EventDispatcher implements ButtonModel{
    13   	private var stateMask:Number;	
    14  	private var group:ButtonGroup;
    15  	private var enabled:Boolean;
    16  	private var rollOver:Boolean;
    17  	private var pressed:Boolean;
    18  	private var released:Boolean;
    19  	private var selected:Boolean;
    20  	
    21  	public function DefaultButtonModel(){
    22  		super();
    23  		stateMask = 0;
    24  		enabled = true;
    25  		rollOver = false;
    26  		pressed = false;
    27  		released = false;
    28  		selected = false;
    29  	}
    30  	
    31  	/**
    32  	 * Indicates that the button has been pressed the and not be released yet.
    33  	 * <p><b>This is not same as Java Swing's, this is some like Java Swing's Armed</b>
    34  	 */
    35  	public function isPressed():Boolean{
    36          return pressed;
    37  	}
    38  		
    39  	/**
    40  	 * Indicates if button has been released. This is some like java Swing's pressed when released inside.
    41  	 */
    42  	public function isReleased():Boolean{
    43          return released;
    44  	}
    45  	        
    46      /**
    47       * Indicates if the button has been selected. Only needed for
    48       * certain types of buttons - such as RadioButton or Checkbox.
    49       *
    50       * @return true if the button is selected
    51       */
    52      public function isSelected():Boolean {
    53          return selected;
    54      }
    55          
    56      /**
    57       * Indicates whether the button can be selected or pressed by
    58       * an input device (such as a mouse pointer). (Checkbox-buttons
    59       * are selected, regular buttons are "pressed".)
    60       *
    61       * @return true if the button is enabled, and therefore
    62       *         selectable (or pressable)
    63       */
    64      public function isEnabled():Boolean {
    65          return enabled;
    66      }
    67                  
    68      /**
    69       * Indicates that the mouse is over the button.
    70       *
    71       * @return true if the mouse is over the button
    72       */
    73      public function isRollOver():Boolean {
    74          return rollOver;
    75      }
    76          
    77      /**
    78       * Sets the button to released or unreleased.
    79       */
    80  	public function setReleased(b:Boolean):Void{
    81          if((released == b) || !isEnabled()) {
    82              return;
    83          }
    84              
    85          released = b;
    86          if(released){
    87          	pressed = false;
    88          }
    89              
    90          fireStateChanged();		
    91  	}
    92  		     
    93  
    94      /**
    95       * Enables or disables the button.
    96       * 
    97       * @param b true to enable the button
    98       * @see #isEnabled()
    99       */
   100      public function setEnabled(b:Boolean):Void {
   101          if(enabled == b) {
   102              return;
   103          }
   104              
   105          enabled = b;
   106          if (!b) {
   107              pressed = false;
   108          }
   109  
   110              
   111          fireStateChanged();
   112      }
   113          
   114      /**
   115       * Selects or deselects the button.
   116       *
   117       * @param b true selects the button,
   118       *          false deselects the button
   119       */
   120      public function setSelected(b:Boolean):Void {
   121          if (selected == b) {
   122              return;
   123          }
   124  
   125          selected = b;
   126  		
   127          fireStateChanged();
   128          fireSelectionChanged();
   129      }
   130          
   131          
   132      /**
   133       * Sets the button to pressed or unpressed.
   134       * 
   135       * @param b true to set the button to "pressed"
   136       * @see #isPressed()
   137       */
   138      public function setPressed(b:Boolean):Void {
   139          if((pressed == b) || !isEnabled()) {
   140              return;
   141          }
   142          
   143          pressed = b;
   144          if(pressed){
   145          	released = false;
   146          }
   147          
   148          fireStateChanged();
   149      }   
   150  
   151      /**
   152       * Sets or clears the button's rollover state
   153       * 
   154       * @param b true to turn on rollover
   155       * @see #isRollOver()
   156       */
   157      public function setRollOver(b:Boolean):Void {
   158          if((rollOver == b) || !isEnabled()) {
   159              return;
   160          }
   161          
   162          rollOver = b;
   163  
   164          fireStateChanged();
   165      }
   166  	
   167  	/**
   168       * Identifies the group this button belongs to --
   169       * needed for radio buttons, which are mutually
   170       * exclusive within their group.
   171       *
   172       * @param group the <code>ButtonGroup</code> this button belongs to
   173       */
   174      public function setGroup(group:ButtonGroup):Void {
   175          this.group = group;
   176      }
   177  
   178      /**
   179       * Returns the group that this button belongs to.
   180       * Normally used with radio buttons, which are mutually
   181       * exclusive within their group.
   182       *
   183       * @return a <code>ButtonGroup</code> that this button belongs to
   184       */
   185      public function getGroup():ButtonGroup {
   186          return group;
   187      }
   188      
   189  	public function addChangeListener(func:Function, obj:Object):Object{
   190  		return addEventListener(AbstractButton.ON_STATE_CHANGED, func, obj);
   191  	}
   192  
   193  	public function addSelectionListener(func:Function, obj:Object):Object{
   194  		return addEventListener(AbstractButton.ON_SELECTION_CHANGED, func, obj);
   195  	}
   196          
   197      private function fireStateChanged():Void{
   198      	dispatchEvent(AbstractButton.ON_STATE_CHANGED, createEventObj(AbstractButton.ON_STATE_CHANGED));
   199      }
   200  	private function fireSelectionChanged() : Void {
   201  		dispatchEvent(AbstractButton.ON_SELECTION_CHANGED, createEventObj(AbstractButton.ON_SELECTION_CHANGED));
   202  	}
   203  	
   204  	public function toString():String{
   205  		return "org.aswing.DefaultButtonModel[]";
   206  	}
   207  }
   208