package net.xoetrope.html;
import net.xoetrope.xui.XAttributedComponent;
import net.xoetrope.xui.XTextHolder;
import net.xoetrope.xui.XTextRenderer;
import netscape.javascript.JSObject;
import org.w3c.dom.Node;
import org.w3c.dom.html.HTMLFormElement;
import org.w3c.dom.html.HTMLLabelElement;
/**
* Draws text. The text may be wrapped over multiple lines. Double buffering is
* switched off by default.
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License: see license.txt
*
* @version $Revision: 2.7 $
*/
public class XLabel extends XHtmlWidget implements XTextHolder, XAttributedComponent
{
protected JSObject obj;
/**
* The content of the label
*/
protected String text;
/**
* The renderer that will draw the content
*/
protected XTextRenderer renderer = new XTextRenderer();
/**
* Create a new XLabel component.
*/
public XLabel()
{
super();
Node content = htmlDoc.getBody().getFirstChild().cloneNode( true );
HTMLLabelElement label = (HTMLLabelElement)htmlDoc.createElement( "LABEL" );
label.appendChild( content );
labelElement = label;
}
/**
* Create a new XLabel component with a specific value and id.
*
* @param txt
* the text of this XLabel
* @param id
* the id at which the user has to refer to act on this component
*/
public XLabel( String txt, String id )
{
super();
Node content = htmlDoc.getBody().getFirstChild().cloneNode( true );
HTMLLabelElement label = (HTMLLabelElement)htmlDoc.createElement( "LABEL" );
label.setId( id );
label.appendChild( content );
label.getFirstChild().setNodeValue( txt );
labelElement = label;
text = txt;
}
/**
* Create a new XLabel based on an existing HTML label element.
*
* @param labelElement
* the HTML label tag the XLabel refers to
*/
public XLabel( HTMLLabelElement labelElement )
{
super( labelElement );
text = labelElement.getFirstChild().getNodeValue();
}
/**
* Set the text content of the XLabel.
*
* @param str
* the new text value
*/
public void setText( String str )
{
text = str;
labelElement.getFirstChild().setNodeValue( text );
}
/**
* Get the text.
*
* @return the label text
*/
public String getText()
{
return text;
}
/**
* Get the text horizontal alignment. The function is same to getHorizontalAlignment()
*
* @return the horizontal alignment flag<br>
* <ul>
* <li>XTextRenderer.LEFT - left align the text</li>
* <li>XTextRenderer.RIGHT - right align the text</li>
* <li>XTextRenderer.CENTER - center the text</li>
* </ul>
*/
public int getAlignment()
{
return getHorizontalAlignment();
}
/**
* Set the horizontal alignment of the text. The function is same to setHorizontalAlignment(int)
*
* @param align
* 1 to right align the text, 0 for left alignment and 2 for centered
* text
*/
public void setAlignment( int align )
{
setHorizontalAlignment( align );
}
/**
* Get the text horizontal alignment.
*
* @return the horizontal alignment flag<br>
* <ul>
* <li>XTextRenderer.LEFT - left align the text</li>
* <li>XTextRenderer.RIGHT - right align the text</li>
* <li>XTextRenderer.CENTER - center the text</li>
* </ul>
*/
public int getHorizontalAlignment()
{
return renderer.getHorizontalAlignment();
}
/**
* Set the horizontal alignment of the text.
*
* @param align
* 1 to right align the text, 0 for left alignment and 2 for centered
* text
*/
public void setHorizontalAlignment( int align )
{
renderer.setHorizontalAlignment( align );
}
/**
* Get the text vertical alignment.
*
* @return the vertical alignment flag<br>
* <ul>
* <li>XTextRenderer.TOP - top align the text</li>
* <li>XTextRenderer.BOTTOM - bottom align the text</li>
* <li>XTextRenderer.CENTER - center the text</li>
* </ul>
*/
public int getVerticalAlignment()
{
return renderer.getVerticalAlignment();
}
/**
* Set the vertical alignment of the text.
*
* @param align
* 1 to bottom align the text, 0 for top alignment and 2 for centered
* text
*/
public void setVerticalAlignment( int align )
{
renderer.setVerticalAlignment( align );
}
/**
* Set one or more attributes of the component. Attributes include <br>
* <OL>
* <LI>align (left|right|center ) or</LI>
* <LI>alignment (left|right|center )</LI>
* </OL>
*
* @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 )
{
labelElement.setAttribute( attribName, (String)attribValue );
String attribNameLwr = attribName.toLowerCase();
String attribValueLwr = ( (String)attribValue ).toLowerCase();
if (( attribNameLwr.compareTo( "align" ) == 0 ) || ( attribNameLwr.compareTo( "alignment" ) == 0 )
|| ( attribNameLwr.compareTo( "halign" ) == 0 ) || ( attribNameLwr.compareTo( "horizontalalignment" ) == 0 )) {
if ( attribValueLwr.compareTo( "right" ) == 0 )
setHorizontalAlignment( XTextRenderer.RIGHT );
else if ( attribValueLwr.compareTo( "center" ) == 0 )
setHorizontalAlignment( XTextRenderer.CENTER );
else
setHorizontalAlignment( XTextRenderer.LEFT );
}
else if (( attribNameLwr.compareTo( "valign" ) == 0 ) || ( attribNameLwr.compareTo( "verticalalignment" ) == 0 )) {
if ( attribValueLwr.compareTo( "bottom" ) == 0 )
setVerticalAlignment( XTextRenderer.BOTTOM );
else if ( attribValueLwr.compareTo( "center" ) == 0 )
setVerticalAlignment( XTextRenderer.CENTER );
else
setVerticalAlignment( XTextRenderer.TOP );
}
else
return -1;
return 0;
}
/**
* Set the element this XLabel depends on.
*
* @param htmlFor
* the id of this HTML element
*/
public void setHtmlFor( String htmlFor )
{
labelElement.setHtmlFor( htmlFor );
}
/**
* Get the element this XLabel depends on.
*
* @return the id of this HTML element
*/
public String getHtmlFor()
{
return labelElement.getHtmlFor();
}
/**
* Get the form this XLabel belongs to.
*
* @return the HTML Form element
*/
public HTMLFormElement getForm()
{
return labelElement.getForm();
}
/**
* Get the size of the XLabel'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." + labelElement.getId() + ".style.fontSize" ) );
}
/**
* Set the size of the XLabel'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." + labelElement.getId() + ".style.fontSize = \"" + fontsize + "\"" );
}
/**
* Get the background color of the XLabel.
*
* @return the hexadecimal value of the background color
*/
public String getBackgroundColor()
{
obj = JSObject.getWindow( XApplet.getApplet() );
return (String)obj.eval( "document.all." + labelElement.getId() + ".style.background" );
}
/**
* Set the background color of the XLabel.
*
* @param hexColor
* the hexadecimal value of the background color
*/
public void setBackgroundColor( String hexColor )
{
obj = JSObject.getWindow( XApplet.getApplet() );
obj.eval( "document.all." + labelElement.getId() + ".style.background = \"" + hexColor + "\"" );
}
/**
* Get the font color of the XLabel.
*
* @return the hexadecimal value of the font color
*/
public String getFontColor()
{
obj = JSObject.getWindow( XApplet.getApplet() );
return (String)obj.eval( "document.all." + labelElement.getId() + ".style.color" );
}
/**
* Set the font color of the XLabel.
*
* @param hexColor
* the hexadecimal value of the font color
*/
public void setFontColor( String hexColor )
{
obj = JSObject.getWindow( XApplet.getApplet() );
obj.eval( "document.all." + labelElement.getId() + ".style.color = \"" + hexColor + "\"" );
}
/**
* Set the XLabel 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." + labelElement.getId() + ".style.visibility = \"" + status + "\"" );
}
/**
* Method used to know if the XLabel is visible or hidden on the HTML page.
*
* @return true if the XLabel is visible, false otherwise
*/
public boolean isVisible()
{
obj = JSObject.getWindow( XApplet.getApplet() );
if ( obj.eval( "document.all." + labelElement.getId() + ".style.visibility" ).toString().equals( "hidden" ) ) {
return false;
}
else {
return true;
}
}
/**
* Put the XLabels's text in bold.
*
* @param bold
* true to put the text in bold, false otherwise
*/
public void setBold( boolean bold )
{
obj = JSObject.getWindow( XApplet.getApplet() );
if ( bold ) {
obj.eval( "document.all." + labelElement.getId() + ".style.fontWeight = \"bold\"" );
}
else {
obj.eval( "document.all." + labelElement.getId() + ".style.fontWeight = \"\"" );
}
}
/**
* Get the HTML element at which refers the XLabel
*
* @return the HTML element.
*/
public HTMLLabelElement getHTMLElement()
{
return labelElement;
}
}