Package net.xoetrope.html

Source Code of net.xoetrope.html.XRadioButton

package net.xoetrope.html;

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;
import netscape.javascript.JSObject;
import org.w3c.dom.html.HTMLFormElement;
import org.w3c.dom.html.HTMLInputElement;
import org.w3c.dom.Node;

/**
* A wrapper for the JRadioButton class
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2004<br>
* License: see license.txt $Revision: 2.4 $
*/
public class XRadioButton extends XHtmlWidget implements XStateHolder, XValueHolder, XRadioHolder, XAttributedComponent,
    XRadioButtonGroup
{

  private Checkbox cb;

  protected JSObject obj;

  /**
   * Create a new radio button
   */
  public XRadioButton()
  {
    super();
    HTMLInputElement radio = (HTMLInputElement)htmlDoc.createElement( "INPUT" );
    radio.setAttribute( "type", "radio" );
    inputElement = radio;
  }

  /**
   * Create a new radio button with a value and an id
   *
   * @param id
   *          the radio button id
   * @param value
   *          the radio button value
   */
  public XRadioButton( String id, String value )
  {
    super();
    HTMLInputElement radio = (HTMLInputElement)htmlDoc.createElement( "INPUT" );
    Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
    radio.appendChild( text );
    radio.getFirstChild().setNodeValue( value );
    radio.setId( id );
    radio.setAttribute( "type", "radio" );
    inputElement = radio;
  }

  /**
   * Create a new XRadioButton based on an existing HTML radiobutton element.
   *
   * @param inputElement
   *          the HTML input tag the XRadioButton refers to
   */
  public XRadioButton( HTMLInputElement inputElement )
  {
    super( inputElement );
  }

  /**
   * Simulate a click on the XRadioButton.
   */
  public void click()
  {
    inputElement.click();
  }

  /**
   * Set the XRadioButton visible or invisible.
   *
   * @param visible
   *          true if visible, false otherwise
   */
  public void setVisible( boolean visible )
  {
    obj = JSObject.getWindow( XApplet.getApplet() );
    String status = "";
    if ( !visible ) {
      status = "hidden";
    }
    else {
      status = "visible";
    }
    obj.eval( "document.all." + inputElement.getId() + ".style.visibility = \"" + status + "\"" );
  }

  /**
   * Set the XRadioButton enabled or disabled.
   *
   * @param enabled
   *          true if enabled, false otherwise
   * @todo fix this method
   */
  public void setEnabled( boolean enabled )
  {
    obj = JSObject.getWindow( XApplet.getApplet() );
    if ( !enabled ) {
      inputElement.setDisabled( enabled );
    }
    else {
      /**
       * Methods getAttributeNode( String nodeName ) and removeAttribute( String
       * attribName ) not supported in FireFox.
       *
       * @todo remove the disabled attribute without using JavaScript
       */
      obj.eval( "document.all." + inputElement.getId() + ".disabled = false" );
    }
  }

  /**
   * Set one or more attributes of the component.
   *
   * @param attribName
   *          the name of the attribute
   * @param attribValue
   *          the value of the attribute
   * @return 0 for success, non zero for failure or to require some further
   *         action
   */
  public int setAttribute( String attribName, Object attribValue )
  {
    inputElement.setAttribute( attribName, (String)attribValue );
    return 0;
  }

  /**
   * Get an attribute value
   *
   * @param attribName
   *          the attribute which value is requested
   * @return the value of this attribute
   */
  public String getAttribute( String attribName )
  {
    return inputElement.getAttribute( attribName );
  }

  /**
   * Set whether the radio button is checked by default
   *
   * @param defaultChecked
   *          true if the radio button is checked by default, false otherwise
   */
  public void setDefaultChecked( boolean defaultChecked )
  {
    inputElement.setDefaultChecked( defaultChecked );
  }

  /**
   * Get whether the radio button is checked by default
   *
   * @return true if the radio button is checked by default, false otherwise
   */
  public boolean getDefaultChecked()
  {
    return inputElement.getDefaultChecked();
  }

  /**
   * Set the align attribute of the XRadioButton
   *
   * @param align
   *          the alignment of the XRadioButton (left, right, center, justified)
   */
  public void setAlign( String align )
  {
    inputElement.setAlign( align );
  }

  /**
   * Get the align attribute of the XRadioButton.
   *
   * @return the alignment of this XRadioButton
   */
  public String getAlign()
  {
    return inputElement.getAlign();
  }

  /**
   * Set the alt attribute of the XRadioButton.
   *
   * @param name
   *          the alternative name to be displayed
   */
  public void setAlternativeName( String name )
  {
    inputElement.setAlt( name );
  }

  /**
   * Get the alt attribute of the XRadioButton.
   *
   * @return the alternative name for this XRadioButton
   */
  public String getAlternativeName()
  {
    return inputElement.getAlt();
  }

  /**
   * Get the form this XRadioButton belongs to.
   *
   * @return the HTML Form element
   */
  public HTMLFormElement getForm()
  {
    return inputElement.getForm();
  }

  /**
   * Enables or disables the radio button
   *
   * @param select
   *          true if the radio is to be selected, false otherwise
   * @todo fix this method of checking the radiobutton
   */
  public void setSelected( boolean select )
  {
    /*
     * Can't use the setChecked( boolean checked ) method : whatever is the
     * value of checked, it always puts the checked status to true.
     */
    if ( select != inputElement.getChecked() )
      inputElement.click();
  }

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

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

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

  /**
   * Set the value associated with this button
   *
   * @param newValue
   *          the new button value
   */
  public void setValue( Object newValue )
  {
    if ( inputElement.getFirstChild() == null ) {
      Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
      inputElement.appendChild( text );
    }
    inputElement.getFirstChild().setNodeValue( (String)newValue );
  }

  /**
   * Check if the the radiobutton is selected
   *
   * @return true if the current radio button is selected, false otherwise
   */
  public boolean isSelected()
  {
    return inputElement.getChecked();
  }

  /**
   * Set the radio button group
   *
   * @param group
   *          the name of the group
   */
  public void setGroup( String group )
  {
    inputElement.setName( group );
  }

  /**
   * Get the radio button group
   *
   * @return the name of the group
   */
  public String getGroup()
  {
    return inputElement.getName();
  }

  /**
   * Insert a new radiobutton after the current one
   *
   * @param element
   *          the displayed name of the button
   * @param group
   *          the group of the button
   * @param id
   *          the unique identifier of the radio button
   */
  public void insertAfter( String element, String group, String id )
  {
    XRadioButton radio = new XRadioButton( id, element );
    Node n = htmlDoc.getBody().getFirstChild().cloneNode( true );
    Node location = inputElement.getNextSibling();
    radio.getHTMLElement().setAttribute( "name", group );
    inputElement.getParentNode().insertBefore( n, location );
    inputElement.getParentNode().insertBefore( radio.getHTMLElement(), location );
  }

  /**
   * Insert a new radiobutton before the current one
   *
   * @param element
   *          the displayed name of the button
   * @param group
   *          the group of the button
   * @param id
   *          the unique identifier of the radio button
   */
  public void insertBefore( String element, String group, String id )
  {
    XRadioButton radio = new XRadioButton( id, element );
    Node n = htmlDoc.getBody().getFirstChild().cloneNode( true );
    radio.getHTMLElement().setAttribute( "name", group );
    inputElement.getParentNode().insertBefore( radio.getHTMLElement(), inputElement );
    inputElement.getParentNode().insertBefore( n, inputElement );
  }

  /**
   * Delete the current XRadioButton
   */
  public void deleteElement()
  {
    inputElement.getParentNode().removeChild( inputElement.getNextSibling() );
    inputElement.getParentNode().removeChild( inputElement );
  }

  /**
   * Set the size of the XRadioButton's text.
   *
   * @param size
   *          the int value of the font size
   */
  public void setFontSize( int size )
  {
    obj = JSObject.getWindow( XApplet.getApplet() );
    obj.eval( "document.all." + inputElement.getId() + ".style.fontsize = '" + size + "'" );
  }

  /**
   * Get the size of the XRadioButton's text.
   *
   * @return the int value of the font size
   */
  public String getFontSize()
  {
    obj = JSObject.getWindow( XApplet.getApplet() );
    return (String)obj.eval( "document.all." + inputElement.getId() + ".style.fontsize" );
  }

  /**
   * Set the font color of the XRadioButton's elements.
   *
   * @param hexColor
   *          the hexadecimal value of the font color
   */
  public void setFontColor( String hexColor )
  {
    obj = JSObject.getWindow( XApplet.getApplet() );
    obj.eval( "document.all." + inputElement.getId() + ".style.color = '" + hexColor + "'" );
  }

  /**
   * Get the font color of the XRadioButton's elements.
   *
   * @return the hexadecimal value of the font color
   */
  public String getFontColor()
  {
    obj = JSObject.getWindow( XApplet.getApplet() );
    return (String)obj.eval( "document.all." + inputElement.getId() + ".style.color" );
  }

  /**
   * Get the HTML element at which refers the XRadioButton.
   *
   * @return the HTML element.
   */
  public HTMLInputElement getHTMLElement()
  {
    return inputElement;
  }

  /*
   * ============================================================================== |
   * Abstract methods from the interfaces implemented |
   * =============================================================================
   */

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

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

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

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

    return null;
  }

  /**
   * 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();
    cb.setCheckboxGroup( cbGroup );
    return cbGroup;
  }

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

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

}
TOP

Related Classes of net.xoetrope.html.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.