Package net.xoetrope.awt

Source Code of net.xoetrope.awt.XRadioButton

package net.xoetrope.awt;

import java.awt.Checkbox;
import java.awt.CheckboxGroup;

import net.xoetrope.xui.XAttributedComponent;
import net.xoetrope.xui.XRadioButtonGroup;
import net.xoetrope.xui.XRadioHolder;
import net.xoetrope.xui.XStateHolder;
import net.xoetrope.xui.XTextHolder;
import net.xoetrope.xui.XValueHolder;

/**
* A wrapper for the Checkbox class
* <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License:      see license.txt
* @version $Revision: 2.5 $
*/
public class XRadioButton extends Checkbox implements XStateHolder, XValueHolder, XRadioHolder, XAttributedComponent, XRadioButtonGroup
{
  Object value;

  /**
   * Constructs a blank radio button
   */
  public XRadioButton()
  {
  }

  /**
   * Sets the text of the button
   * @param s the new caption
   */
  public void setText( String s )
  {
    setLabel( s );
  }

  /**
   * Gets the text/caption
   * @return the text value
   */
  public String getText()
  {
    return getLabel();
  }

  /**
   * Create a new checkbox group and adds this radio button top the new group
   * @return the new group
   */
  public Object createGroup()
  {
    CheckboxGroup cbGroup = new CheckboxGroup();
    super.setCheckboxGroup( cbGroup );
    return cbGroup;
  }

  /**
   * Set the button group
   * @param grp the group control this radio button
   */
  public void setRadioButtonGroup( Object grp )
  {
    super.setCheckboxGroup((CheckboxGroup)grp );
  }

  /**
   * Gets the group controlling this radio button
   * @return the ButtonGroup
   */
  public Object getRadioButtonGroup()
  {
    return getCheckboxGroup();
  }

  /**
   * Gets the selected radio button
   * @return the selected radio button
   */
  public Object getSelectedObject()
  {
    try {
      return getCheckboxGroup().getSelectedCheckbox();
    }
    catch ( Exception ex ) {
    }

    return null;
  }

  /**
   * Select an item
   * @param object the index of the item to select
   */
  public void setSelectedObject( Object object )
  {
    getCheckboxGroup().setSelectedCheckbox( (Checkbox)object );
  }

  /**
   * Selects this radio button
   */
  public void setSelected()
  {
    setState( true );
    getCheckboxGroup().setSelectedCheckbox( this );
  }

  /**
   * Adds the checkbox to the same group as this component.
   * @param cb the checkbox/radiobutton to be added to the group
   */
  public void associate( Object cb )
  {
    ((XRadioButton)cb).setRadioButtonGroup( getCheckboxGroup());
  }

  /**
   * Set one or more attributes of the component. Currently this handles the
   * attributes
   * <OL>
   * <LI>selected, value=true|false</LI>
   * </OL>
   * @param attribName the attribute name
   * @param attribValue the attribute value
   * @return 0 for success, non zero for failure or to require some further action
   */
  public int setAttribute( String attribName, Object attribValue )
  {
    if ( attribName.equals( "selected" )) {
      CheckboxGroup cbGroup = getCheckboxGroup();
      if ( cbGroup == null )
        cbGroup = (CheckboxGroup)createGroup();
      if ( attribValue.equals( "true" )) {
        setState( true );
        cbGroup.setSelectedCheckbox( this );
      }
      return 0;
    }
   
    return -1;
  }

  /**
   * Get the component state
   * @return the new component state
   */
  public Object getComponentState()
  {
    return new Boolean( getState());
  }

  /**
   * Set the component selection state: '1' or 'true' to select this option
   * @param o the object state'
   */
  public void setComponentState( Object o )
  {
    if ( o != null ) {
      String objValue = o.toString();
      boolean value = objValue.equals( "1" );
      if ( !value )
        value |= objValue.equals( "true" );
      setState( value );
    }
  }

  /**
   * Get the radiobutton's value if it has one or else get the text
   * @return the value for this button
   */
  public Object getValue()
  {
    if ( value != null )
      return value;

    return getText();
  }

  /**
   * Set the value associated with this button
   * @param newValue the new button value
   */
  public void setValue( Object newValue )
  {
    value = newValue;
  }

  /**
   * Return the text of the radio button. This is so that the model outputs the
   * selected text and not the component details.
   * @return the current value of this radio button
   */
  public String toString()
  {
    return ( String ) getValue();
  }
}
TOP

Related Classes of net.xoetrope.awt.XRadioButton

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.