Package net.xoetrope.html

Source Code of net.xoetrope.html.XComboBox

package net.xoetrope.html;

import net.xoetrope.xui.XListHolder;
import netscape.javascript.JSObject;
import org.w3c.dom.html.HTMLSelectElement;
import org.w3c.dom.Node;
import org.w3c.dom.html.HTMLOptionElement;

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

  protected JSObject obj;

  /**
   * Create a new combo box
   */
  public XComboBox()
  {
    super();
    HTMLSelectElement select = (HTMLSelectElement)htmlDoc.createElement( "SELECT" );
    selectElement = select;
  }

  /**
   * Create a new combo box with an id and filled with elements.
   *
   * @param id
   *          the combo box id
   * @param elements
   *          the elements contained in the combo box
   */
  public XComboBox( String id, String[] elements )
  {
    super();
    HTMLSelectElement select = (HTMLSelectElement)htmlDoc.createElement( "SELECT" );
    select.setId( id );
    selectElement = select;
    for ( int i = 0; i < elements.length; i++ ) {
      addElement( elements[ i ] );
    }
  }

  /**
   * Create a new XCComboBox based on an existing HTML combobox element.
   *
   * @param select
   *          the HTML select tag the XComboBox refers to
   */
  public XComboBox( HTMLSelectElement select )
  {
    super( select );
  }

  /**
   * Select an item in the list
   *
   * @param i
   *          the selected item's index
   */
  public void select( int i )
  {
    if ( i <= getSize() ) {
      selectElement.setSelectedIndex( i - 1 );
    }
  }
 
  /**
   * Set the list to its default selection state
   */
  public void setDefaultSelection()
  {
    select( 0 );
  }

  /**
   * Select an item in the list
   *
   * @param object
   *          the item to select
   */
  public void setSelectedObject( Object object )
  {
    if ( object != null ) {
      for ( int i = 0; i < getSize(); i++ ) {
        if ( selectElement.getChildNodes().item( i ).getFirstChild().getNodeValue().equals( (String)object ) ) {
          select( i + 1 );
        }
      }
    }
    else {
      select( 1 );
    }
  }

  /**
   * Get the selected object
   *
   * @return the selection
   */
  public Object getSelectedObject()
  {
    return selectElement.getChildNodes().item( getSelectedIndex() ).getFirstChild().getNodeValue();
  }
 
  /**
   * Get the selected objects
   * @return the selected object
   */
  public Object[] getSelectedObjects()
  {
    Object[] values = new Object[ 1 ];
    values[ 0 ] = getSelectedObject();
    return values;
  }

  /**
   * Set the selected objects
   * @param values the selected object
   */
  public void setSelectedObjects( Object[] values )
  {
    setSelectedObject( values[ 0 ] );
  }
 
  /**
   * Get the size of the combo box, ie the number of elements
   *
   * @return the number of elements
   */
  public int getSize()
  {
    return selectElement.getChildNodes().getLength();
  }

  /**
   * Get the index of a specified object of the list.
   *
   * @param o
   *          the object which position is requested
   * @return the index of this element or -1 if there is no such element in the
   *         list
   */
  private int getPosition( Object o )
  {
    for ( int i = 0; i < selectElement.getSize(); i++ ) {
      if ( selectElement.getOptions().item( i ).getFirstChild().getNodeValue().equals( o.toString() ) ) {
        return i;
      }
    }
    return -1;
  }

  /**
   * Add an element at the end of the combo box
   *
   * @param element
   *          the element to be added to the list
   */
  public void addElement( String element )
  {
    Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
    HTMLOptionElement option = (HTMLOptionElement)htmlDoc.createElement( "OPTION" );
    option.appendChild( text );
    option.getFirstChild().setNodeValue( element );
    selectElement.appendChild( option );
  }

  /**
   * Insert an element at the specified index. If the index entered is greater
   * than the length of the combo box itself, the item is added at the end of
   * the list.
   *
   * @param element
   *          the element to insert in the list
   * @param index
   *          the index where to insert the element (starting at 1)
   */
  public void insertElement( String element, int index )
  {
    if ( index <= getSize() ) {
      HTMLOptionElement option = (HTMLOptionElement)htmlDoc.createElement( "OPTION" );
      Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
      option.appendChild( text );
      option.getFirstChild().setNodeValue( element );
      selectElement.insertBefore( option, selectElement.getChildNodes().item( index - 1 ) );
    }
    else {
      addElement( element );
    }
  }

  /**
   * Delete the current XComboBox.
   */
  public void deleteElement( int index )
  {
    if ( index <= getSize() ) {
      selectElement.remove( index - 1 );
    }
  }

  /**
   * Get the size of the XComboBox'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." + selectElement.getId() + ".style.fontSize" ) );
  }

  /**
   * Set the size of the XComboBox'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." + selectElement.getId() + ".style.fontSize = \"" + fontsize + "\"" );
  }

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

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

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

  /**
   * Set the font color of the XComboBox'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." + selectElement.getId() + ".style.color = \"" + hexColor + "\"" );
  }

  /**
   * Set the XComboBox 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." + selectElement.getId() + ".style.visibility = \"" + status + "\"" );
  }

  /**
   * Set the XComboBox 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 ) {
      selectElement.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." + selectElement.getId() + ".disabled = false" );
    }
  }

  /**
   * Get the HTML element at which refers the XComboBox.
   *
   * @return the HTML element.
   */
  public HTMLSelectElement getHTMLElement()
  {
    return selectElement;
  }

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

  /**
   * Get the number of items in the list
   *
   * @return the number of items
   */
  public int getItemCount()
  {
    return getSize();
  }

  /**
   * Remove all items from the list
   */
  public void removeAll()
  {
    for ( int i = 1; i <= getSize(); i++ ) {
      deleteElement( i );
    }
  }

  /**
   * Add an item to the list
   *
   * @param s
   *          the new Item
   */
  public void addItem( String s )
  {
    addElement( s );
  }

  /**
   * Select an item
   *
   * @param object
   *          The object to select
   */
  public void select( Object object )
  {
    select( object );
  }

  /**
   * Get the selected index
   *
   * @return the selected index
   */
  public int getSelectedIndex()
  {
    return selectElement.getSelectedIndex();
  }
 
  /**
   * Get the list selection mode. This method returns 0.
   * @return
   * <ul>
   * <li>0=ListSelectionModel.SINGLE_SELECTION,</li>
   * <li>1=ListSelectionModel.SINGLE_INTERVAL_SELECTION,</li>
   * <li>2=ListSelectionModel.MULTIPLE_INTERVAL_SELECTION</li>
   * </ul>
   */
  public int getSelectionMode()
  {
    return 0;
  }
}
TOP

Related Classes of net.xoetrope.html.XComboBox

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.