1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4   
     5  import org.aswing.*;
     6  import org.aswing.utils.ArrayUtils;
     7   
     8  /**
     9   *
    10   * @author iiley
    11   */
    12  class org.aswing.ButtonGroup{
    13      // the list of buttons participating in this group
    14      private var buttons:Array;
    15  
    16      /**
    17  	 * The current selection.
    18  	 */
    19      private var selection:ButtonModel = null;
    20  
    21      /**
    22  	 * Creates a new <code>ButtonGroup</code>.
    23  	 */
    24      public function ButtonGroup() {
    25      	buttons = new Array();
    26      }
    27  
    28      /**
    29  	 * Adds the button to the group.
    30  	 * 
    31  	 * @param b the button to be added
    32  	 */ 
    33      public function append(b:AbstractButton):Void {
    34          if(b == null) {
    35              return;
    36          }
    37          buttons.push(b);
    38  
    39          if (b.isSelected()) {
    40              if (selection == null) {
    41                  selection = b.getModel();
    42              } else {
    43                  b.setSelected(false);
    44              }
    45          }
    46  
    47          b.getModel().setGroup(this);
    48      }
    49   
    50      /**
    51  	 * Removes the button from the group.
    52  	 * 
    53  	 * @param b the button to be removed
    54  	 */ 
    55      public function remove(b:AbstractButton):Void {
    56          if(b == null) {
    57              return;
    58          }
    59          ArrayUtils.removeFromArray(buttons, b);
    60          if(b.getModel() == selection) {
    61              selection = null;
    62          }
    63          b.getModel().setGroup(null);
    64      }
    65  
    66      /**
    67  	 * Returns all the buttons that are participating in this group.
    68  	 * 
    69  	 * @return an <code>Array</code> of the buttons in this group
    70  	 */
    71      public function getElements():Array {
    72          return ArrayUtils.cloneArray(buttons);
    73      }
    74  
    75      /**
    76  	 * Returns the model of the selected button.
    77  	 * 
    78  	 * @return the selected button model
    79  	 */
    80      public function getSelection():ButtonModel {
    81          return selection;
    82      }
    83  
    84      /**
    85  	 * Sets the selected value for the <code>ButtonModel</code>. Only one
    86  	 * button in the group may be selected at a time.
    87  	 * 
    88  	 * @param m the <code>ButtonModel</code>
    89  	 * @param b <code>true</code> if this button is to be selected,
    90  	 *            otherwise <code>false</code>
    91  	 */
    92      public function setSelected(m:ButtonModel, b:Boolean):Void {
    93          if (b && m != null && m != selection) {
    94              var oldSelection:ButtonModel = selection;
    95              selection = m;
    96              if (oldSelection != null) {
    97                  oldSelection.setSelected(false);
    98              }
    99              m.setSelected(true);
   100          } 
   101      }
   102  
   103      /**
   104  	 * Returns whether a <code>ButtonModel</code> is selected.
   105  	 * 
   106  	 * @return <code>true</code> if the button is selected, otherwise returns
   107  	 *         <code>false</code>
   108  	 */
   109      public function isSelected(m:ButtonModel):Boolean {
   110          return (m == selection);
   111      }
   112  
   113      /**
   114  	 * Returns the number of buttons in the group.
   115  	 * 
   116  	 * @return the button count
   117  	 */
   118      public function getButtonCount():Number {
   119      	return buttons.length;
   120      }
   121  }
   122