Package net.xoetrope.html

Source Code of net.xoetrope.html.XButton

package net.xoetrope.html;

import net.xoetrope.xui.XAttributedComponent;
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;

/**
* <p>
* A wrapper for the AWT Button class. In addition to wrapping the button this
* object can hold a value.
* </p>
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License: see license.txt
*
* @version $Revision: 2.6 $
*/
public class XButton extends XHtmlWidget implements XValueHolder, XAttributedComponent
{

  protected JSObject obj;

  /**
   * Create a new button
   *
   * @param isImage
   *          true if the button is an image button, false otherwise
   */
  public XButton( boolean isImage )
  {
    super();
    HTMLInputElement button = (HTMLInputElement)htmlDoc.createElement( "INPUT" );
    if ( isImage ) {
      button.setAttribute( "type", "button" );
    }
    else {
      button.setAttribute( "type", "image" );
    }
    inputElement = button;
  }

  /**
   * Create a new text button with a value and an id
   *
   * @param value
   *          the button value
   * @param id
   *          the button id
   */
  public XButton( String value, String id )
  {
    super();
    HTMLInputElement button = (HTMLInputElement)htmlDoc.createElement( "INPUT" );
    button.setId( id );
    button.setAttribute( "type", "button" );
    button.setValue( value );
    inputElement = button;
  }

  /**
   * Create a new image button with a src, an id and an alt attribute
   *
   * @param src
   *          the image source
   * @param id
   *          the button id
   * @param alt
   *          the alternative text to display if the button can't load
   */
  public XButton( String src, String id, String alt )
  {
    super();
    HTMLInputElement button = (HTMLInputElement)htmlDoc.createElement( "INPUT" );
    button.setId( id );
    button.setAttribute( "type", "image" );
    button.setSrc( src );
    button.setAlt( alt );
    inputElement = button;
  }

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

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

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

  /**
   * Get teh button text/caption
   *
   * @return the current text
   */
  public String getText()
  {
    return (String)this.getValue();
  }

  /**
   * Set teh button text/caption
   *
   * @param s
   *          the new text
   */
  public void setValue( Object obj )
  {
    inputElement.setValue( (String)obj );
  }

  /**
   * Get teh button text/caption
   *
   * @return the current text
   */
  public Object getValue()
  {
    return inputElement.getValue();
  }

  /**
   * Get the button type
   *
   * @return the button type (button, submit, image)
   */
  public String getType()
  {
    return inputElement.getType();
  }

  /**
   * Set the XButton 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 XButton 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" );
    }
  }

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

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

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

  /**
   * 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 a shortcut to click on this XButton
   *
   * @param accessKey
   *          the key used as a shortcut
   */
  public void setAccessKey( String accessKey )
  {
    inputElement.setAccessKey( accessKey );
  }

  /**
   * Get the shortcut key to click on this XButton
   *
   * @return the key used as a shortcut
   */
  public String getAccessKey()
  {
    return inputElement.getAccessKey();
  }

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

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

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

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

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

  /**
   * Set the size of the XButton.
   *
   * @param width
   *          the width of the button
   */
  public void setSize( int width )
  {
    inputElement.setSize( "" + width );
  }

  /**
   * Get the size of the XButton
   *
   * @return the width of the button
   */
  public String getSize()
  {
    return inputElement.getSize();
  }

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

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

  /**
   * Get the background color of the XButton.
   *
   * @return the hexadecimal value of the background color
   */
  public String getBackgroundColor()
  {
    obj = JSObject.getWindow( XApplet.getApplet() );
    return (String)obj.eval( "document.all." + inputElement.getId() + ".style.background" );
  }

  /**
   * Set the background color of the XButton.
   *
   * @param hexColor
   *          the hexadecimal value of the background color
   */
  public void setBackgroundColor( String hexColor )
  {
    obj = JSObject.getWindow( XApplet.getApplet() );
    obj.eval( "document.all." + inputElement.getId() + ".style.background = \"" + hexColor + "\"" );
  }

  /**
   * Get the font color of the XButton.
   *
   * @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" );
  }

  /**
   * Set the font color of the XButton.
   *
   * @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 HTML element at which refers the XButton
   *
   * @return the HTML element.
   */
  public HTMLInputElement getHTMLElement()
  {
    return inputElement;
  }

}
TOP

Related Classes of net.xoetrope.html.XButton

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.