Package net.xoetrope.html

Source Code of net.xoetrope.html.XCheckbox

package net.xoetrope.html;

import net.xoetrope.xui.XAttributedComponent;
import net.xoetrope.xui.XStateHolder;
import net.xoetrope.xui.XTextHolder;
import net.xoetrope.xui.XValueHolder;
import netscape.javascript.JSObject;
import org.w3c.dom.Node;
import org.w3c.dom.html.HTMLFormElement;
import org.w3c.dom.html.HTMLInputElement;

/**
* A wrapper for the Swing JCheckbox class
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2004<br>
* License: see license.txt
*
* @version 1.0
*/
public class XCheckbox extends XHtmlWidget implements XStateHolder, XValueHolder, XAttributedComponent
{

  protected JSObject obj;

  /**
   * Construct a new checkbox
   */
  public XCheckbox()
  {
    super();
    HTMLInputElement check = (HTMLInputElement)htmlDoc.createElement( "INPUT" );
    check.setAttribute( "type", "checkbox" );
    inputElement = check;
  }

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

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

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

  /**
   * Set the check box text
   *
   * @param s
   *          the new text
   */
  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 checkbox's value if it has one or else get the text
   *
   * @return the value for this box
   */
  public Object getValue()
  {
    if ( inputElement.getFirstChild() != null ) {
      return inputElement.getFirstChild().getNodeValue();
    }
    return null;
  }

  /**
   * Set the value associated with this box
   *
   * @param newValue
   *          the new box 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 );
  }

  /**
   * Get the status of the check box
   *
   * @return true if the check box is enabled, false otherwise
   */
  public boolean isSelected()
  {
    return inputElement.getChecked();
  }

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

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

  /**
   * Enables or disables the check box
   */
  public void setSelected( boolean checked )
  {
    /*
     * Can't use the setChecked( boolean checked ) method : whatever is the
     * value of checked, it always puts the checked status to true.
     */
    /** @todo fix this method of checking the checkbox */
    if ( checked != inputElement.getChecked() )
      inputElement.click();

  }

  /**
   * Set the XCheckbox 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 XCheckbox 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 the align attribute of the XCheckbox
   *
   * @param align
   *          the alignment of the XCheckbox (left, right, center, justified)
   */
  public void setAlign( String align )
  {
    inputElement.setAlign( align );
  }

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

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

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

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

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

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

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

  /**
   * Set the size of the XCheckbox'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 XCheckbox'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 XCheckbox'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 XCheckbox'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 XCheckbox.
   *
   * @return the HTML element.
   */
  public HTMLInputElement getHTMLElement()
  {
    return inputElement;
  }

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

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

  /**
   * Set the component state
   *
   * @param object
   *          the selection state. Possible values: 1 or true and 0 or false
   */
  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 );
    }
  }

}
TOP

Related Classes of net.xoetrope.html.XCheckbox

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.