Package net.xoetrope.html

Source Code of net.xoetrope.html.XList

package net.xoetrope.html;

import netscape.javascript.JSObject;
import org.w3c.dom.html.HTMLOListElement;
import org.w3c.dom.html.HTMLUListElement;
import org.w3c.dom.Node;
import org.w3c.dom.html.HTMLLIElement;

/**
* <p>
* A wrapper for the AWT List class
* </p>
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2004<br>
* License: see license.txt
*
* @version $Revision: 2.2 $
*/
public class XList extends XHtmlWidget
{

  protected JSObject obj;

  /**
   * Create a new list component
   *
   * @param ordered
   *          true if the list ordered, false otherwise
   */
  public XList( boolean ordered )
  {
    super();
    Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
    if ( ordered ) {
      HTMLOListElement ol = (HTMLOListElement)htmlDoc.createElement( "OL" );
      ol.appendChild( text );
      olElement = ol;
    }
    else {
      HTMLUListElement ul = (HTMLUListElement)htmlDoc.createElement( "UL" );
      ul.appendChild( text );
      ulElement = ul;
    }
  }

  /**
   * Create a new XList with an id and filled with elements
   *
   * @param ordered
   *          true if the list ordered, false otherwise
   * @param id
   *          the id of the list
   * @param elements
   *          the elements of the list
   */
  public XList( boolean ordered, String id, String[] elements )
  {
    super();
    Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
    if ( ordered ) {
      HTMLOListElement ol = (HTMLOListElement)htmlDoc.createElement( "OL" );
      ol.appendChild( text );
      ol.setId( id );
      olElement = ol;
      for ( int i = 0; i < elements.length; i++ ) {
        oAddElement( elements[ i ] );
      }
    }
    else {
      HTMLUListElement ul = (HTMLUListElement)htmlDoc.createElement( "UL" );
      ul.appendChild( text );
      ul.setId( id );
      ulElement = ul;
      for ( int i = 0; i < elements.length; i++ ) {
        uAddElement( elements[ i ] );
      }
    }
  }

  /**
   * Create a new ordered XList based on an existing HTML ordered list.
   *
   * @param olElement
   *          the HTML ol tag the XList refers to
   */
  public XList( HTMLOListElement olElement )
  {
    super( olElement );
  }

  /**
   * Create a new unordered XList based on an existing HTML unordered list.
   *
   * @param ulElement
   *          the HTML ul tag the XList refers to
   */
  public XList( HTMLUListElement ulElement )
  {
    super( ulElement );
  }

  /**
   * Add a new element at the end of an ordered list
   *
   * @param text
   *          the element to add
   */
  public void oAddElement( String text )
  {
    Node n = htmlDoc.getBody().getFirstChild().cloneNode( true );
    Node n2 = n.cloneNode( true );
    HTMLLIElement li = (HTMLLIElement)htmlDoc.createElement( "LI" );
    li.appendChild( n );
    li.getFirstChild().setNodeValue( text );
    olElement.appendChild( li );
    olElement.appendChild( n2 );
  }

  /**
   * Remove the last element of the current ordered list
   */
  public void oRemoveElement()
  {
    olElement.removeChild( olElement.getLastChild() );
    olElement.removeChild( olElement.getLastChild() );
  }

  /**
   * Remove the element at the index given in argument in the current ordered
   * list
   *
   * @param index
   *          the index of the element to remove (starting at 1)
   */
  public void oRemoveElement( int index )
  {
    olElement.removeChild( olElement.getChildNodes().item( 2 * index - 1 ) );
    olElement.removeChild( olElement.getChildNodes().item( 2 * index - 1 ) );
  }

  /**
   * Replace the value of the list item at the specified index. If there is no
   * item at this index, add the item at the end of the list.
   *
   * @param text
   *          the value of the list item
   * @param index
   *          the place you want the item to be inserted
   */
  public void oSetText( String text, int index )
  {
    Node n;
    Node currentNode = olElement.getChildNodes().item( index );
    if ( currentNode != null ) {
      n = currentNode;
      n.getFirstChild().setNodeValue( text );
      olElement.replaceChild( n, currentNode );
    }
    else {
      oAddElement( text );
    }
  }

  /**
   * Insert an item into an ordered list.
   *
   * @param text
   *          the value of the list item
   * @param index
   *          the place the item is to be inserted
   */
  public void oInsertElement( String text, int index )
  {
    Node currentNode = olElement.getChildNodes().item( 2 * index - 1 );
    if ( currentNode != null ) {
      HTMLLIElement li = (HTMLLIElement)htmlDoc.createElement( "LI" );
      Node n = htmlDoc.getBody().getFirstChild().cloneNode( true );
      olElement.insertBefore( n, currentNode );
      li.appendChild( n.cloneNode( true ) );
      li.getFirstChild().setNodeValue( text );
      olElement.insertBefore( li, currentNode );
    }
    else {
      oAddElement( text );
    }
  }

  /**
   * Get an element from the list
   *
   * @param index
   *          the index of the element to get (starting at 1)
   */
  public String oGetElementAt( int index )
  {
    Node currentNode = olElement.getChildNodes().item( index );
    if ( currentNode != null ) {
      return currentNode.getFirstChild().getNodeValue();
    }
    return "No element at this index.";
  }

  /**
   * Get the number of elements in the list
   *
   * @return the size of the list
   */
  public int oGetSize()
  {
    return olElement.getChildNodes().getLength() - 1;
  }

  /**
   * Get the start point of the list
   *
   * @return the start number
   */
  public int oGetStartPoint()
  {
    return olElement.getStart();
  }

  /**
   * Set the start point of the list
   *
   * @param start
   *          the start number
   */
  public void oSetStartPoint( int start )
  {
    olElement.setStart( start );
  }

  /**
   * Get the numbering type of the list
   *
   * @return the numbering type (1, i, I)
   */
  public String oGetNumberType()
  {
    return olElement.getType();
  }

  /**
   * Set the numbering type of the list
   *
   * @param type
   *          the numbering type (1, i, I)
   */
  public void oSetNumberType( String type )
  {
    olElement.setType( type );
  }

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

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

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

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

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

  /**
   * Get the HTML element at which refers the XList.
   *
   * @return the HTML element.
   */
  public HTMLOListElement oGetHTMLElement()
  {
    return olElement;
  }

  /**
   * Add a new element at the end of an unordered list
   *
   * @param text
   *          the element to add
   */
  public void uAddElement( String text )
  {
    Node n = htmlDoc.getBody().getFirstChild().cloneNode( true );
    Node n2 = n.cloneNode( true );
    HTMLLIElement li = (HTMLLIElement)htmlDoc.createElement( "LI" );
    li.appendChild( n );
    li.getFirstChild().setNodeValue( text );
    ulElement.appendChild( li );
    ulElement.appendChild( n2 );
  }

  /**
   * Remove the last element of the current unordered list
   */
  public void uRemoveElement()
  {
    ulElement.removeChild( ulElement.getLastChild() );
    ulElement.removeChild( ulElement.getLastChild() );
  }

  /**
   * Remove the element at the index given in argument in the current unordered
   * list
   *
   * @param index
   *          the index of the element to remove (starting at 1)
   */
  public void uRemoveElement( int index )
  {
    ulElement.removeChild( ulElement.getChildNodes().item( 2 * index - 1 ) );
    ulElement.removeChild( ulElement.getChildNodes().item( 2 * index - 1 ) );
  }

  /**
   * Replace the value of the list item at the specified index. If there is no
   * item at this index, add the item at the end of the list.
   *
   * @param text
   *          the value of the list item
   * @param index
   *          the place you want the item to be inserted
   */
  public void uSetText( String text, int index )
  {
    Node n;
    Node currentNode = ulElement.getChildNodes().item( index );
    if ( currentNode != null ) {
      n = currentNode;
      n.getFirstChild().setNodeValue( text );
      ulElement.insertBefore( n, currentNode );
    }
    else {
      uAddElement( text );
    }
  }

  /**
   * Insert an item into an unordered list.
   *
   * @param text
   *          the value of the list item
   * @param index
   *          the place you want the item to be inserted
   */
  public void uInsertElement( String text, int index )
  {
    Node currentNode = ulElement.getChildNodes().item( 2 * index - 1 );
    if ( currentNode != null ) {
      HTMLLIElement li = (HTMLLIElement)htmlDoc.createElement( "LI" );
      Node n = htmlDoc.getBody().getFirstChild().cloneNode( true );
      ulElement.insertBefore( n, currentNode );
      li.appendChild( n.cloneNode( true ) );
      li.getFirstChild().setNodeValue( text );
      ulElement.insertBefore( li, currentNode );
    }
    else {
      uAddElement( text );
    }
  }

  /**
   * Get an element from the list
   *
   * @param index
   *          the index of the element to get (starting at 1)
   */
  public String uGetElementAt( int index )
  {
    Node currentNode = ulElement.getChildNodes().item( index );
    if ( currentNode != null ) {
      return currentNode.getFirstChild().getNodeValue();
    }
    return "No element at this index";
  }

  /**
   * Get the bullet type
   *
   * @return the bullet type (circle, diamond, disc, square)
   */
  public String uGetBulletType()
  {
    return ulElement.getType();
  }

  /**
   * Set the bullet type
   *
   * @param type
   *          the bullet type (circle, diamond, disc, square)
   */
  public void uSetBulletType( String type )
  {
    ulElement.setType( type );
  }

  /**
   * Get the number of elements in the list
   *
   * @return the size of the list
   */
  public int uGetSize()
  {
    return ulElement.getChildNodes().getLength() - 1;
  }

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

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

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

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

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

  /**
   * Get the HTML element at which refers the XList.
   *
   * @return the HTML element.
   */
  public HTMLUListElement uGetHTMLElement()
  {
    return ulElement;
  }

}
TOP

Related Classes of net.xoetrope.html.XList

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.