Package net.xoetrope.awt

Source Code of net.xoetrope.awt.XLabel

package net.xoetrope.awt;

import java.awt.Canvas;
import java.awt.Graphics;

import net.xoetrope.xui.XAttributedComponentEx;
import net.xoetrope.xui.XTextHolder;
import net.xoetrope.xui.XTextRenderer;
import java.awt.Dimension;
import java.awt.Image;
import net.xoetrope.xui.XProject;

/**
* 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 Canvas implements XTextHolder, XAttributedComponentEx
{
  /**
   * The content of the label
   */
  protected String text;
 
  /**
   * The renderer that will draw the content
   */
  protected XTextRenderer renderer = new XTextRenderer();
 
  /**
   * true for a double buffered display
   */
  protected boolean doubleBuffered = false;
 
  /**
   * the internal buffer
   */
  protected Image bufferImage = null;
 
  /**
   * The width of the buffer image
   */
  protected int bufferWidth;
 
  /**
   * The buffer image height
   */
  protected int bufferHeight;

  /**
   * Reset the text
   * @param str the new text
   */
  public void setText( String str )
  {
    text = str;
    repaint();
  }

  /**
   * Update the component
   * @param g the graphics context
   */
  public void update(Graphics g)
  {
    paint(g);
  }

  /**
   * Repaint the component once it has been created
   */
  public void addNotify()
  {
    super.addNotify();
    repaint( 0 );
  }

  /**
   * Render the text
   * @param g the graphics context
   */
  public void paint( Graphics g )
  {
    if ( doubleBuffered ) {
      // Check the buffersize with the current panelsize
      // or initialises the image with the first paint
      if ( bufferWidth != getSize().width ||
           bufferHeight != getSize().height ||
           bufferImage == null )
        resetBuffer();

      Graphics bufferGraphics = bufferImage.getGraphics();
      // Clear the offscreen image
      bufferGraphics.clearRect( 0, 0, bufferWidth, bufferHeight );

      if ( text != null )
        renderer.paintText( this, bufferGraphics, text );

      // Copy offscreen image onto the onscreen image
      g.drawImage( bufferImage, 0, 0, this );

      // Cleanup
      bufferGraphics.dispose();
      bufferGraphics = null;
    }
    else if ( text != null )
      renderer.paintText( this, g, text );
  }

  /**
   * Gets 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();
  }

  /**
   * Sets 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 );
  }

  /**
   * Sets the transparency of the text.
   * @param b true to make text transparent
   */
  public void setTransparent( boolean b )
  {
    renderer.setTransparent( b );
  }

  /**
   * Set one or more attributes of the component.
   * <OL>
   * <LI>align (left|right|center ) or</LI>
   * <LI>alignment  (left|right|center )</LI>
   * <LI>opaque (true|false) set the component opaque property</LI>
   * <LI>tooltip, value=the tooltip text</LI>
   * <LI>buffered, value=(true|false) turn double buffering on or off</LI>
   * <LI>antialias, value=(true|false) override the page antialiasing setting</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 )
  {
    return setAttribute( null, attribName, attribValue );
  }
 
  /**
   * Set one or more attributes of the component.
   * <OL>
   * <LI>align (left|right|center ) or</LI>
   * <LI>alignment  (left|right|center )</LI>
   * <LI>opaque (true|false) set the component opaque property</LI>
   * <LI>tooltip, value=the tooltip text</LI>
   * <LI>buffered, value=(true|false) turn double buffering on or off</LI>
   * <LI>antialias, value=(true|false) override the page antialiasing setting</LI>
   * </OL>
   * @param project the current project or null
   * @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( XProject project, String attribName, Object attribValue )
  {
    String attribNameLwr = attribName.toLowerCase();
    String attribValueLwr = ((String)attribValue).toLowerCase();
    if (( attribNameLwr.equals( "align" )) || ( attribNameLwr.equals( "alignment" ))
        || ( attribNameLwr.equals( "halign" )) || ( attribNameLwr.equals( "horizontalalignment" ))) {
      if ( attribValueLwr.equals( "right" ))
        setHorizontalAlignment( XTextRenderer.RIGHT );
      else if ( attribValueLwr.equals( "center" ))
        setHorizontalAlignment( XTextRenderer.CENTER );
      else
        setHorizontalAlignment( XTextRenderer.LEFT );
    }
    else if (( attribNameLwr.equals( "valign" )) || ( attribNameLwr.equals( "verticalalignment" ))) {
      if ( attribValueLwr.equals( "bottom" ))
        setVerticalAlignment( XTextRenderer.BOTTOM );
      else if ( attribValueLwr.equals( "center" ))
        setVerticalAlignment( XTextRenderer.CENTER );
      else
        setVerticalAlignment( XTextRenderer.TOP );
    }
    else if ( attribNameLwr.equals( "content" )) {
      String s = attribValue.toString();
      if ( project != null )
        s = project.getTranslator().translate( s );
      setText( s );
    }
    else
      return 0;
   
    return -1;
}

  /**
   * Gets the preferred size of this component.
   * @return a dimension object indicating this component's preferred size
   * @see #getMinimumSize
   * @see LayoutManager
   */
  public Dimension getPreferredSize()
  {
    return renderer.getPreferredSize( this, text );
  }

  /**
   * Toggle use of double buffering when painting this component
   * @param buffer  true to double buffer
   */
  public void setDoubleBuffered( boolean buffer )
  {
    doubleBuffered = buffer;
  }

  private void resetBuffer()
  {
    // always keep track of the image size
    Dimension size = getSize();
    bufferWidth = size.width;
    bufferHeight = size.height;

    if ( bufferImage != null ) {
      bufferImage.flush();
      bufferImage = null;
    }

    // create the new image with the size of the panel
    bufferImage = createImage( bufferWidth, bufferHeight );
  }
}
TOP

Related Classes of net.xoetrope.awt.XLabel

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.