Package net.xoetrope.html

Source Code of net.xoetrope.html.XTabPanel

package net.xoetrope.html;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import org.w3c.dom.Node;
import org.w3c.dom.html.HTMLAnchorElement;
import org.w3c.dom.html.HTMLDivElement;

/**
* A tab panel. The methods are modelled on the JTabbedPane
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2004<br>
* License: see license.txt $Revision: 2.3 $
*/
public class XTabPanel extends XHtmlWidget implements MouseListener
{

  protected XPanel contentPane;

  protected XPanel tabPane;

  protected XTable tabs;

  protected int selIdx = 1;

  protected Node tBody, tRow, text;

  /**
   * Create a new tab panel attached to a panel
   *
   * @param the
   *          panel attached to the tab panel
   */
  public XTabPanel( XPanel panel )
  {
    super();
    HTMLDivElement tabpane = (HTMLDivElement)htmlDoc.createElement( "DIV" );
    tabPane = new XPanel( tabpane );
    tabs = new XTable();
    tabPane.add( tabs.tableElement );
    divElement = tabpane;
    contentPane = panel;
  }

  /**
   * Create a new XTabPanel attached to a XPanel, based on a XTable
   *
   * @param panel
   *          the XPanel attached to the tab panel
   * @param tabtable
   *          the tabs of the tab panel
   */
  public XTabPanel( XPanel panel, XTable tabtable )
  {
    super();
    HTMLDivElement tabpane = (HTMLDivElement)htmlDoc.createElement( "DIV" );
    tabPane = new XPanel( tabpane );
    tabs = tabtable;
    tabPane.add( tabs.tableElement );
    divElement = tabpane;
    contentPane = panel;
  }

  /**
   * Create a new tab panel attached to a panel, filled with the title values
   *
   * @param panel
   *          the panel attached to the tab panel
   * @param title
   *          the title of the tabs
   * @param id
   *          the id of the tab panel
   */
  public XTabPanel( XPanel panel, String[] title, String id )
  {
    super();
    HTMLDivElement tabpane = (HTMLDivElement)htmlDoc.createElement( "DIV" );
    tabPane = new XPanel( tabpane );
    tabs = new XTable();
    tabPane.add( tabs.tableElement );
    divElement = tabpane;
    contentPane = panel;

    panel.insertBefore( tabPane );
    tabPane.setId( id );

    tabs.fillRow( 1, title );
    tabs.setBorder( 0 );

    for ( int i = 1; i <= title.length; i++ ) {
      if ( i != selIdx ) {
        tabs.setBackgroundColorAt( 1, i, "#CCFFFF" );
      }
      else {
        tabs.setBackgroundColorAt( 1, i, panel.getBackgroundColor() );
      }
    }

  }

  /**
   * Add a new tab. The add method could not be overloaded so this method adds
   * does the equivalent.
   *
   * @param comp
   *          the new tab component
   * @return the new component
   */
  public void addTab( String title )
  {
    int location = tabs.getColumnNumber() + 1;
    tabs.insertColumn( location );
  }

  /**
   * Get a tab's background color
   *
   * @param index
   *          the tab index
   * @return the color
   */
  public String getBackgroundAt( int index )
  {
    return tabs.getBackgroundColorAt( 1, index );
  }

  /**
   * Get a tab's foreground color
   *
   * @param index
   *          the tab index
   * @return the color
   */
  public String getForegroundAt( int index )
  {
    return tabs.getFontColorAt( 1, index );
  }

  /**
   * Get the number of tabs
   *
   * @return the number of tabs
   */
  public int getTabCount()
  {
    return tabs.getColumnNumber();
  }

  /**
   * Get the index of the selected tab
   *
   * @return the selection index
   */
  public int getSelectedIndex()
  {
    return selIdx;
  }

  /**
   * Get the title of the tab at the specified index
   *
   * @param index
   *          the tab index
   * @return the tab text/title
   */
  public String getTitleAt( int index )
  {
    Node location = tabs.tableElement.getLastChild().getFirstChild().getChildNodes().item( 2 * index - 1 ).getChildNodes().item( 1 ).getFirstChild();
    return location.getNodeValue();
  }

  /**
   * Set the tab background color
   *
   * @param index
   *          the tab index
   * @param clr
   *          the new colr
   */
  public void setBackgroundAt( int index, String hexColor )
  {
    tabs.setBackgroundColorAt( 1, index, hexColor );
  }

  /**
   * Set the foreground color
   *
   * @param index
   *          the tab index
   * @param clr
   *          the new color
   */
  public void setForegroundAt( int index, String hexColor )
  {
    tabs.setFontColorAt( 1, index, hexColor );
  }

  /**
   * Select a tab
   *
   * @param index
   *          the tab index
   */
  public void setSelectedIndex( int index )
  {
    if ( index != selIdx ) {
      tabs.setBackgroundColorAt( 1, index, contentPane.getBackgroundColor() );
    }
    tabs.setBackgroundColorAt( 1, selIdx, "#CCFFFF" );
    selIdx = index;
  }

  /**
   * Set a tab's title
   *
   * @param index
   *          the tab index
   * @param str
   *          the text for the tab
   */
  public void setTitleAt( int index, String str )
  {
    tabs.tableElement.getLastChild().getFirstChild().getChildNodes().item( 2 * index - 1 ).getChildNodes().item( 1 ).getFirstChild().setNodeValue(
        str );
  }

  /**
   * Attach a link to one of the tab panel's tab
   *
   * @param index
   *          the index of the tab (starting at 1)
   * @param linkText
   *          the text to be displayed
   * @param url
   *          the url of the page the link is going to display
   */
  public void setLinkAt( int index, String linkText, String url )
  {
    HTMLAnchorElement anchor = (HTMLAnchorElement)htmlDoc.createElement( "A" );
    Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
    anchor.appendChild( text );
    anchor.setHref( url );
    Node location = tabs.tableElement.getLastChild().getFirstChild().getChildNodes().item( 2 * index - 1 );
    location.appendChild( anchor );
    location.appendChild( text.cloneNode( true ) );
    location.getChildNodes().item( 1 ).getFirstChild().setNodeValue( linkText );

  }

  /**
   * Get the link attached to one of the tab panel's tab
   *
   * @param index
   *          the index of the tab
   * @return the url of the link if there is one, or null otherwise
   */
  public String getLinkAt( int index )
  {
    Node location = tabs.tableElement.getLastChild().getFirstChild().getChildNodes().item( 2 * index - 1 ).getChildNodes().item( 1 );
    if ( location != null ) {
      return ( (HTMLAnchorElement)location ).getHref();
    }
    else {
      return null;
    }
  }

  /**
   * Get the HTML element at which refers the XTabPanel.
   *
   * @return the HTML element.
   */
  public HTMLDivElement getHTMLElement()
  {
    return divElement;
  }

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

  // -Mouse listener
  // methods-----------------------------------------------------
  /**
   * Invoked when the mouse button has been clicked (pressed and released) on a
   * component.
   *
   * @param e
   *          the event
   */
  public void mouseClicked( MouseEvent e )
  {
  }

  /**
   * Invoked when a mouse button has been pressed on a component.
   *
   * @param e
   *          the event
   */
  public void mousePressed( MouseEvent e )
  {
  }

  /**
   * Invoked when a mouse button has been released on a component.
   *
   * @param e
   *          the event
   */
  public void mouseReleased( MouseEvent e )
  {
  }

  /**
   * Invoked when the mouse enters a component.
   *
   * @param e
   *          the event
   */
  public void mouseEntered( MouseEvent e )
  {
  }

  /**
   * Invoked when the mouse exits a component.
   *
   * @param e
   *          the event
   */
  public void mouseExited( MouseEvent e )
  {
  }

}
TOP

Related Classes of net.xoetrope.html.XTabPanel

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.