package net.xoetrope.html;
import net.xoetrope.xui.XAttributedComponent;
import netscape.javascript.JSObject;
import org.w3c.dom.Node;
import org.w3c.dom.html.HTMLDivElement;
import org.w3c.dom.html.HTMLElement;
/**
* A basic container for components. The panel can optional draw a border. By
* default no frame is displayed.
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License: see license.txt
*
* @version $Revision: 2.6 $
*/
public class XPanel extends XHtmlWidget implements XAttributedComponent
{
protected JSObject obj;
/**
* Constructs a new panel with a null layout
*/
public XPanel()
{
super();
HTMLDivElement panel = (HTMLDivElement)htmlDoc.createElement( "DIV" );
divElement = panel;
}
/**
* Create a new XPanel with a value and an id attribute.
*
* @param value
* the text to display in the panel
* @param id
* the id of the panel
*/
public XPanel( String value, String id )
{
super();
Node content = htmlDoc.getBody().getFirstChild().cloneNode( true );
HTMLDivElement panel = (HTMLDivElement)htmlDoc.createElement( "DIV" );
panel.setId( id );
panel.appendChild( content );
panel.getFirstChild().setNodeValue( value );
divElement = panel;
}
/**
* Create a new XPanel based on an existing HTML div element.
*
* @param divElement
* the HTML div tag the XPanel refers to
*/
public XPanel( HTMLDivElement divElement )
{
super( divElement );
}
/**
* Remove all the elements contained in the panel
*/
public void resetContent()
{
if ( !divElement.hasChildNodes() ) {
for ( int i = divElement.getChildNodes().getLength(); i > 0; i-- ) {
divElement.removeChild( divElement.getChildNodes().item( i - 1 ) );
}
}
}
/**
* Add an element in the XPanel
*
* @param element
* the element to add
*/
public void add( HTMLElement element )
{
Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
if ( divElement.hasChildNodes() ) {
divElement.appendChild( text );
}
divElement.appendChild( element );
divElement.appendChild( text.cloneNode( true ) );
}
/**
* Set the XPanel visible or invisible.
*
* @param visible
* true if visible, false otherwise
*/
public void setVisible( boolean visible )
{
obj = JSObject.getWindow( XApplet.getApplet() );
String visibility = "";
if ( visible ) {
visibility = "visible";
}
else {
visibility = "hidden";
}
obj.eval( "document.all." + divElement.getId() + ".style.visibility = '" + visibility + "'" );
}
/**
* Method used to know if the XPanel is visible or hidden on the HTML page.
*
* @return true if the XPanel is visible, false otherwise
*/
public boolean isVisible()
{
obj = JSObject.getWindow( XApplet.getApplet() );
if ( obj.eval( "document.all." + divElement.getId() + ".style.visibility" ).toString().equals( "hidden" ) ) {
return false;
}
else {
return true;
}
}
/**
* Set one or more attributes of the component.
*
* @param attribName
* the attribute name
* @param attribValue
* the attribute value
* @return 0 for success, non zero for failure or to require some further
* action
*/
public int setAttribute( String attribName, Object attribValue )
{
divElement.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 divElement.getAttribute( attribName );
}
/**
* Get the size of the XPanel's elements' 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." + divElement.getId() + ".style.fontSize" ) );
}
/**
* Set the size of the XPanel's elements' text.
*
* @param fontsize
* the int value of the font size
*/
public void setFontSize( int fontsize )
{
obj = JSObject.getWindow( XApplet.getApplet() );
obj.eval( "document.all." + divElement.getId() + ".style.fontSize = \"" + fontsize + "\"" );
}
/**
* Get the background color of the XPanel.
*
* @return the hexadecimal value of the background color
*/
public String getBackgroundColor()
{
obj = JSObject.getWindow( XApplet.getApplet() );
return (String)obj.eval( "document.all." + divElement.getId() + ".style.background" );
}
/**
* Set the background color of the XPanel.
*
* @param hexColor
* the hexadecimal value of the background color
*/
public void setBackgroundColor( String hexColor )
{
obj = JSObject.getWindow( XApplet.getApplet() );
obj.eval( "document.all." + divElement.getId() + ".style.background = \"" + hexColor + "\"" );
}
/**
* Get the font color of the XPanel's elements' text.
*
* @return the hexadecimal value of the font color
*/
public String getFontColor()
{
obj = JSObject.getWindow( XApplet.getApplet() );
return (String)obj.eval( "document.all." + divElement.getId() + ".style.color" );
}
/**
* Set the font color of the XPanel's elements' text.
*
* @param hexColor
* the hexadecimal value of the font color
*/
public void setFontColor( String hexColor )
{
obj = JSObject.getWindow( XApplet.getApplet() );
obj.eval( "document.all." + divElement.getId() + ".style.color = \"" + hexColor + "\"" );
}
/**
* Set the font face of the XPanel's elements' text.
*
* @param fontFace
* the font face
*/
public void setFontFace( String fontFace )
{
obj = JSObject.getWindow( XApplet.getApplet() );
obj.eval( "document.all." + divElement.getId() + ".style.fontFamily = \"" + fontFace + "\"" );
}
/**
* Get the font face of the XPanel's elements' text.
*
* @return the font face
*/
public String getFontFace()
{
obj = JSObject.getWindow( XApplet.getApplet() );
return (String)obj.eval( "document.all." + divElement.getId() + ".style.fontFamily" );
}
/**
* Set the align attribute of the XPanel
*
* @param align
* the alignment of the XPanel (left, right, center, justified)
*/
public void setAlign( String align )
{
divElement.setAlign( align );
}
/**
* Get the align attribute of the XPanel.
*
* @return the alignment of this XPanel
*/
public String getAlign()
{
return divElement.getAlign();
}
/**
* Set the id attribute of the XPanel
*
* @param id
* the id of the panel
*/
public void setId( String id )
{
divElement.setId( id );
}
/**
* Insert a new panel before the current one
*
* @param panel
* the panel that is to be inserted
*/
public void insertBefore( XPanel panel )
{
HTMLDivElement div = panel.getHTMLElement();
Node text = htmlDoc.getBody().getFirstChild().cloneNode( true );
div.getParentNode().insertBefore( div, divElement );
div.getParentNode().insertBefore( text, divElement );
}
/**
* Get the HTML element at which refers the XPanel.
*
* @return the HTML element.
*/
public HTMLDivElement getHTMLElement()
{
return divElement;
}
}