org.wings.SLabel Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2000,2005 wingS development team.
*
* This file is part of wingS (http://wingsframework.org).
*
* wingS 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.
*
* Please see COPYING for the complete licence.
*/
package org.wings;
import org.wings.plaf.LabelCG;
/**
* Display area for a short text string or an image, or both.
*
* You can specify where in the label's display area the label's contents
* are aligned by setting the vertical and horizontal alignment.
* You can also specify the position of the text relative to the image.
*
* This component will take best efforts to quote the text accordingly to the needs of the end platform.
* So special characters i.e. like \n
and \u0123
should work without any further actions.
* To disable quoting and i.e. include custom HTML tags you may disable quoting by prefixing "<html>"
* on you payload text equivalent to the behaviour within Swing.
*
* To enable wordwrapping refer to {@link #setWordWrap(boolean)}.
*
* @author Armin Haaf
*/
public class SLabel extends SComponent {
protected String text;
protected SIcon icon = null;
protected SIcon disabledIcon = null;
protected int verticalTextPosition = SConstants.CENTER;
protected int horizontalTextPosition = SConstants.RIGHT;
protected int iconTextGap = 4;
protected boolean wordWrap;
/**
* Creates a new SLabel
instance with the specified text
* (left alligned) and no icon.
*
* @param text The text to be displayed by the label.
*/
public SLabel(String text) {
this(text, null, SConstants.LEFT);
}
/**
* Creates a new SLabel
instance with no text and no icon.
*/
public SLabel() {
this((String) null);
}
/**
* Creates a new SLabel
instance with the specified icon
* (left alligned) and no text.
*
* @param icon The image to be displayed by the label.
*/
public SLabel(SIcon icon) {
this(icon, SConstants.LEFT);
}
/**
* Creates a new SLabel
instance with the specified icon
* (alligned as specified) and no text.
*
* @param icon The image to be displayed by the label.
* @param horizontalAlignment One of the following constants defined in
* SConstants
:
* LEFT
, CENTER
, RIGHT
.
* @see SConstants
*/
public SLabel(SIcon icon, int horizontalAlignment) {
this(null, icon, horizontalAlignment);
}
/**
* Creates a new SLabel
instance with the specified icon
* and the specified text (left alligned).
*
* @param text The text to be displayed by the label.
* @param icon The image to be displayed by the label.
*/
public SLabel(String text, SIcon icon) {
this(text, icon, SConstants.LEFT);
}
/**
* Creates a new SLabel
instance with the specified icon
* and the specified text (alligned as specified).
*
* @param text The text to be displayed by the label.
* @param icon The image to be displayed by the label.
* @param horizontalAlignment One of the following constants defined in
* SConstants
:
* LEFT
, CENTER
, RIGHT
.
* @see SConstants
*/
public SLabel(String text, SIcon icon, int horizontalAlignment) {
setText(text);
setIcon(icon);
setHorizontalAlignment(horizontalAlignment);
}
/**
* Creates a new SLabel
instance with the specified text
* (alligned as specified) and no icon.
*
* @param text The text to be displayed by the label.
* @param horizontalAlignment One of the following constants defined in
* SConstants
:
* LEFT
, CENTER
, RIGHT
.
* @see SConstants
*/
public SLabel(String text, int horizontalAlignment) {
this(text, null, horizontalAlignment);
}
/**
* Returns the horizontal position of the lable's text
*
* @return the position
* @see SConstants
* @see #setHorizontalTextPosition
*/
public int getHorizontalTextPosition() {
return horizontalTextPosition;
}
/**
* Sets the horizontal position of the lable's text, relative to its icon.
*
* The default value of this property is CENTER.
*
* @param textPosition One of the following constants defined in
* SConstants
:
* LEFT
, CENTER
, RIGHT
.
*/
public void setHorizontalTextPosition(int textPosition) {
int oldVal = this.horizontalTextPosition;
reloadIfChange(horizontalTextPosition, textPosition);
horizontalTextPosition = textPosition;
propertyChangeSupport.firePropertyChange("horizontalTextPosition", oldVal, this.horizontalTextPosition);
}
/**
* Sets the vertical position of the lable's text, relative to its icon.
*
* The default value of this property is CENTER.
*
* @param textPosition One of the following constants defined in
* SConstants
:
* TOP
, CENTER
, BOTTOM
.
*/
public void setVerticalTextPosition(int textPosition) {
int oldVal = this.verticalTextPosition;
reloadIfChange(verticalTextPosition, textPosition);
verticalTextPosition = textPosition;
propertyChangeSupport.firePropertyChange("verticalTextPosition", oldVal, this.verticalTextPosition);
}
/**
* Returns the vertical position of the label's text
*
* @return the position
* @see SConstants
* @see #setVerticalTextPosition
*/
public int getVerticalTextPosition() {
return verticalTextPosition;
}
/**
* Defines the icon the component will display.
* @param i
*/
public void setIcon(SIcon i) {
if (isDifferent(icon, i)) {
SIcon oldVal = this.icon;
// do reload if previous text was null
if (isUpdatePossible() && icon != null && SLabel.class.isAssignableFrom(getClass()))
update(((LabelCG) getCG()).getIconUpdate(this, i));
else
reload();
icon = i;
propertyChangeSupport.firePropertyChange("icon", oldVal, this.icon);
}
}
/**
* Returns the icon the label displays.
* @return the icon
*/
public SIcon getIcon() {
return icon;
}
/**
* Set the icon that will be displayed if the label is disabled.
* @param i
*/
public void setDisabledIcon(SIcon i) {
SIcon oldVal = this.disabledIcon;
reloadIfChange(disabledIcon, i);
disabledIcon = i;
propertyChangeSupport.firePropertyChange("disabledIcon", oldVal, this.disabledIcon);
}
/**
* Returns the icon that is displayed when the label is disabled.
* @return the diabledIcon
*/
public SIcon getDisabledIcon() {
return disabledIcon;
}
/**
* Returns the text of the label
* @return the text
*/
public String getText() {
return text;
}
/**
* Sets the text of the label. Nothing will be displayed if the text is an empty string or null
.
* Use <html> as prefix to disable implicit
* quoting of the payload text. By default the component should handle and quote special characters as \n
* and \u0123
correctly.
*
* @param t The new text
*/
public void setText(String t) {
if (isDifferent(text, t)) {
String oldVal = this.text;
// do reload if previous text was null
if (isUpdatePossible() && text != null && SLabel.class.isAssignableFrom(getClass()))
update(((LabelCG) getCG()).getTextUpdate(this, t));
else
reload();
text = t;
propertyChangeSupport.firePropertyChange("text", oldVal, this.text);
}
}
/**
* Determiens if the label text word wrap inside the browser. Defaults to false
(Swing).
* @return false
if the label should not word wrap an be in line as in Swing.
*/
public boolean isWordWrap() {
return wordWrap;
}
/**
* Defines if the label is allowed to wrap.
* @param wordWrap Set to true
if you want labels to allow to break into more lines than passed.
*/
public void setWordWrap(boolean wordWrap) {
if (this.wordWrap != wordWrap) {
boolean oldVal = this.wordWrap;
this.wordWrap = wordWrap;
reload();
propertyChangeSupport.firePropertyChange("wordWrap", oldVal, this.wordWrap);
}
}
/**
* Returns the amount of space between the text and the icon
* displayed in this label.
*
* @return an int equal to the number of pixels between the text
* and the icon.
* @see #setIconTextGap
*/
public int getIconTextGap() {
return iconTextGap;
}
/**
* If both the icon and text properties are set, this property
* defines the space between them.
*
* The default value of this property is 4 pixels.
*
*
* @see #getIconTextGap
*/
public void setIconTextGap(int iconTextGap) {
int oldVal = this.iconTextGap;
reloadIfChange(this.iconTextGap, iconTextGap);
this.iconTextGap = iconTextGap;
propertyChangeSupport.firePropertyChange("iconTextGap", oldVal, this.iconTextGap);
}
public void setCG(LabelCG cg) {
super.setCG(cg);
}
}