Package net.xoetrope.html

Source Code of net.xoetrope.html.XEdit

package net.xoetrope.html;

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

/**
* <p>
* A wrapper for the Swing JFormattedTextField class
* </p>
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License: see license.txt
*
* @version 1.0
*/
public class XEdit extends XHtmlWidget implements XTextHolder
{

  protected JSObject obj;

  /**
   * Create a new XEdit
   */
  public XEdit()
  {
    super();
    HTMLInputElement edit = (HTMLInputElement)htmlDoc.createElement( "INPUT" );
    edit.setAttribute( "type", "text" );
    inputElement = edit;
  }

  /**
   * Create a new XEdit with an id and a specific value.
   *
   * @param value
   *          the value in the edit field
   * @param id
   *          the id of the edit field
   */
  public XEdit( String value, String id )
  {
    HTMLInputElement edit = (HTMLInputElement)htmlDoc.createElement( "INPUT" );
    edit.setAttribute( "type", "text" );
    edit.setValue( value );
    edit.setId( id );
    inputElement = edit;
  }

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

  /**
   * Lose focus on the current edit field.
   */
  public void blur()
  {
    inputElement.blur();
  }

  /**
   * Focus on the current edit field.
   */
  public void focus()
  {
    inputElement.focus();
  }

  /**
   * Select the content of the edit field.
   */
  public void select()
  {
    inputElement.select();
  }

  /**
   * Set the default value of the XEdit.
   *
   * @param value
   *          the default value
   */
  public void setDefaultValue( String value )
  {
    inputElement.setDefaultValue( value );
  }

  /**
   * Get the default value of the XEdit.
   *
   * @return the default value
   */
  public String getDefaultValue()
  {
    return inputElement.getDefaultValue();
  }

  /**
   * Get the Object value of the text field
   *
   * @return the Object value for this field
   */
  public Object getValue()
  {
    return inputElement.getValue();
  }

  /**
   * Set the Object value for the text field
   *
   * @param value
   *          the new object value
   */
  public void setValue( Object value )
  {
    inputElement.setValue( value.toString() );
  }

  /**
   * Get the text content of the current edit field.
   *
   * @return the current text value of the edit field
   */
  public String getText()
  {
    return (String)this.getValue();
  }

  /**
   * Set the text content of the current edit field.
   *
   * @param text
   *          the new text value of the edit field
   */
  public void setText( String text )
  {
    this.setValue( text );
  }

  /**
   * Set the XEdit 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 XEdit 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
   */
  public void setAttribute( String attribName, Object attribValue )
  {
    inputElement.setAttribute( attribName, (String)attribValue );
  }

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

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

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

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

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

  /**
   * Set the maximum number of characters the edit field can handle.
   *
   * @param maxLength
   *          the maximum length
   */
  public void setMaxLength( int maxLength )
  {
    inputElement.setMaxLength( maxLength );
  }

  /**
   * Get the max number of characters the edit field can handle.
   *
   * @return the maximum length
   */
  public int getMaxLength()
  {
    return inputElement.getMaxLength();
  }

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

  /**
   * Get the size of the edit field.
   *
   * @return the size of the edit field
   */
  public String getSize()
  {
    return inputElement.getSize();
  }

  /**
   * Get the size of the XEdit'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 XEdit'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 XEdit.
   *
   * @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 XEdit.
   *
   * @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 XEdit'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" );
  }

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

}
TOP

Related Classes of net.xoetrope.html.XEdit

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.