WingLabel.java :  » Web-Framework » wingS » com » javujavu » javux » wings » Java Open Source

Java Open Source » Web Framework » wingS 
wingS » com » javujavu » javux » wings » WingLabel.java
/*
*  Javu WingS - Lightweight Java Component Set
*  Copyright (c) 2005-2007 Krzysztof A. Sadlocha
*  e-mail: ksadlocha@programics.com
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.javujavu.javux.wings;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import com.javujavu.javux.wings.item.DefaultItemRenderer;
import com.javujavu.javux.wings.item.ItemRenderer;

/**
 * A display area for a label. It can be text, image or any object
 * that can be rendered with <code>{@link ItemRenderer}</code> set to this component.
 *
 * <br>
 * <b>This is one of the core WingS classes required by all the components</b><br>
 * <br>
 * <b>This class is thread safe.</b>
 * @see ItemRenderer
 * @see DefaultItemRenderer
 **/
public class WingLabel extends WingComponent
{
  protected Object label;
  protected int alignment;
  protected int textPosition;

    /**
     * Constructs an empty label.
     **/
  public WingLabel()
  {
    this(null,LEFT);
  }
    /**
     * Constructs a new label with the specified <code>label</code> item.
     * @param label label item usually String
     **/
  public WingLabel(Object label)
  {
    this(label,LEFT);
  }
    /**
     * Constructs a new label with the specified <code>label</code> item
     *  with the specified alignment
     * @param label label item usually String
     * @param alignment the alignment value.
     **/
  public WingLabel(Object label, int alignment)
  {
    this.label= label;
    this.alignment= alignment;
    this.textPosition= RIGHT;
  }
    /**
     * Loads skin resources.<br>
     * <pre>
     * styles:
     * [optional styleID.]label.normal
     * [optional styleID.]label.disabled
     * </pre>
     * @see Style
     * @see WingSkin
     */
  public void loadSkin()
  {
    stNormal= WingSkin.getStyle(styleId, "label", NORMAL, null);
    stDisabled= WingSkin.getStyle(styleId, "label", DISABLED, stNormal).merge(stTop);
    stNormal= stNormal.merge(stTop);
  }

    /**
     * Sets the label item for this label to the specified value.
     * @param      label the label item that this label displays.
     */
  public void setLabel(Object label)
  {
    if((this.label==null && label==null) || this.label==label)
    {
      return;
    }
    this.label= label;
    revalidateAndRepaint();
  }
    /**
     * Returns the label item of this label.
     * @return     the label item of this label
     */
  public Object getLabel() { return label; }

    /**
     * Sets the alignment of the label's contents along the X axis.
     * @param alignment  One of the following constants
     *           <code>WingConst.LEFT</code>,
     *           <code>WingConst.CENTER</code>
     *           <code>WingConst.RIGHT</code>
     */
  public void setHorizontalAlignment(int alignment)
  {
    this.alignment= alignment;
    repaintVisible();
  }
    /**
     * Sets the horizontal position of the label's text,
     * relative to its image.
     *
     * @param textPosition  One of the following constants
     *           <code>WingConst.LEFT</code>,
     *           <code>WingConst.RIGHT</code>
     */
  public void setTextPosition(int textPosition)
  {
    this.textPosition= textPosition;
    repaintVisible();
  }

  public Dimension getPreferredSize()
  {
    Dimension prefSize= wingPrefSize;
    if(prefSize==null)
    {
      wingPrefSize= prefSize= getRenderer().getItemSize(label, this,
          getStyle(), null);
    }
    return prefSize;
  }

  /**
   * @see com.javujavu.javux.wings.WingComponent#paintBackground(java.awt.Graphics)
   */
  public void paintBackground(Graphics g)
  {
    Style st= getStyle();
    Dimension size= getSize();
    getRenderer().drawItem(g, 0,0, size.width, size.height,
        label, this, st, st.margin, alignment, textPosition,
        null);
  }
  /**
   * @see com.javujavu.javux.wings.WingComponent#imageUpdate(java.awt.Image, int, int, int, int, int)
   */
  public boolean imageUpdate(Image img, int infoflags,
      int x, int y, int width, int height)
  {
    return isShowing() && getRenderer().imageUpdate(img,
        infoflags, x, y, width, height, label, this, getStyle(),
        new Rectangle(getSize()));
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.