Package net.xoetrope.html

Source Code of net.xoetrope.html.XImage

package net.xoetrope.html;

import java.awt.Image;
import net.xoetrope.xui.XAttributedComponent;
import net.xoetrope.xui.XImageHolder;
import netscape.javascript.JSObject;
import org.w3c.dom.html.HTMLImageElement;

/**
* <p>
* Draws an image
* </p>
* <p>
* Copyright: Copyright (c) Xoetrope Ltd., 1998-2003
* </p>
* License: see license.txt $Revision: 2.7 $
*/
public class XImage extends XHtmlWidget implements XImageHolder, XAttributedComponent
{

  protected JSObject obj;

  /**
   * Constructs a blank image control.
   */
  public XImage()
  {
    super();
    HTMLImageElement image = (HTMLImageElement)htmlDoc.createElement( "IMG" );
    imageElement = image;
  }

  /**
   * Create a new XImage with an id and a src attribute
   *
   * @param src
   *          the path where to find the image to display
   * @param id
   *          the id of the image
   */
  public XImage( String src, String id )
  {
    super();
    HTMLImageElement image = (HTMLImageElement)htmlDoc.createElement( "IMG" );
    image.setId( id );
    image.setSrc( src );
    imageElement = image;
  }

  /**
   * Create a new XImage based on an existing HTML edit field.
   *
   * @param inputElement
   *          the HTML img tag the XImage refers to
   */
  public XImage( HTMLImageElement imageElement )
  {
    super( imageElement );
  }

  /**
   * Sets the image to display.
   *
   * @param img
   *          the image
   */
  public void setImageSrc( String src )
  {
    imageElement.setSrc( src );
  }

  /**
   * Get the image src attribute
   *
   * @return the relative path where to find the attribute
   */
  public String getImageSrc()
  {
    return imageElement.getSrc();
  }

  /**
   * Set the name attribute of the XImage
   *
   * @param name
   *          the name of the image
   */
  public void setImageName( String name )
  {
    imageElement.setName( name );
  }

  /**
   * Gets the name of the image being displayed.
   *
   * @return the image name
   */
  public String getImageName()
  {
    return imageElement.getName();
  }

  /**
   * Get the size of this image
   *
   * @return the size
   */
  public String getFullSize()
  {
    if ( imageElement == null )
      return null;
    return ( "width = " + imageElement.getWidth() + " ; height = " + imageElement.getHeight() );
  }

  /**
   * Set the size of the image
   *
   * @param width
   *          the width od the image
   * @param height
   *          the height of the image
   */
  public void setFullSize( int width, int height )
  {
    imageElement.setWidth( "" + width );
    imageElement.setHeight( "" + height );
  }

  /**
   * Set one or more attributes of the component.
   * <OL>
   * <LI>content, value=the image file name</LI>
   * <LI>imagename, value=the image file name</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 )
  {
    imageElement.setAttribute( attribName, (String)attribValue );
    String attribNameLwr = attribName.toLowerCase();
//    String attribValueLwr = ( (String)attribValue ).toLowerCase();
    if ( attribNameLwr.equals( "content" ) || attribNameLwr.equals( "imagename" ) ) {
      imageElement.setName( (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 imageElement.getAttribute( attribName );
  }

  /**
   * Set the margins of the image
   *
   * @param horizontalMargin
   *          the horizontal margin
   * @param verticalMargin
   *          the vertical margin
   */
  public void setMargins( int horizontalMargin, int verticalMargin )
  {
    setHorizontalMargin( horizontalMargin );
    setVerticalMargin( verticalMargin );
  }

  /**
   * Get the margins of the image.
   *
   * @return a String representing the margins of the image
   */
  public String getMargins()
  {
    return ( "H. margin = " + getHorizontalMargin() + " ; V. margin = " + getVerticalMargin() );
  }

  /**
   * Set the horizontal margin of the image
   *
   * @param margin
   *          the horizontal margin
   */
  public void setHorizontalMargin( int margin )
  {
    imageElement.setHspace( "" + margin );
  }

  /**
   * Get the horizontal margin of the image
   *
   * @return the horizontal margin
   */
  public String getHorizontalMargin()
  {
    return ( "H. margin = " + imageElement.getHspace() );
  }

  /**
   * Set the vertical margin of the image
   *
   * @param margin
   *          the vertical margin
   */
  public void setVerticalMargin( int margin )
  {
    imageElement.setVspace( "" + margin );
  }

  /**
   * Set the vertical margin of the image
   *
   * @return the vertical margin
   */
  public String getVerticalMargin()
  {
    return ( "V. margin = " + imageElement.getVspace() );
  }

  /**
   * Set the align attribute of the XImage
   *
   * @param align
   *          the alignment of the XImage (left, right, center, justified)
   */
  public void setAlign( String align )
  {
    imageElement.setAlign( align );
  }

  /**
   * Get the align attribute of the XImage.
   *
   * @return the alignment of this XImage
   */
  public String getAlign()
  {
    return imageElement.getAlign();
  }

  /**
   * Set the border of the image
   *
   * @param size
   *          the thickness of the border in pixel
   */
  public void setBorder( int size )
  {
    imageElement.setBorder( "" + size );
  }

  /**
   * Get the border of the image
   *
   * @return the thickness of the border in pixel
   */
  public String getBorder()
  {
    return imageElement.getBorder();
  }

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

  /**
   * Get the HTML element at which refers the XImage
   *
   * @return the HTML element.
   */
  public HTMLImageElement getHTMLElement()
  {
    return imageElement;
  }

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

  /**
   * Sets the image to display.
   *
   * @param img
   *          the image
   * @todo complete this method
   */
  public void setImage( Image img )
  {
  }

}
TOP

Related Classes of net.xoetrope.html.XImage

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.