package com.easyfeeling.ui.controls;
import javax.swing.JLabel;
/**
* 可配置 * 号标签
* @author luojialin
* @since 2014-2-24
*/
public class SFLabel extends JLabel {
public static String LEFT = "left";
public static String RIGHT = "right";
private String position = "right";
private boolean redStart = false;
private String visibleText;
private String text;
/**
* 无参构造函数
*/
public SFLabel() {
this(null,false);
}
/**
* 构造函数
* @param text 标签名称
*/
public SFLabel(String text) {
this(text,false);
}
/**
* 构造函数
* @param text 标签名称
* @param flag 是否显示 * 号
*/
public SFLabel(String text,boolean flag) {
this(text,flag,null);
}
/**
* 构造函数
* @param title 标签
* @param flage 是否显示 星号
* @param position 星号显示的位置 左、右
*/
public SFLabel(String title,boolean flag,String pos) {
super();
text = title;
redStart = flag;
position = pos;
if(redStart){
visibleText = setLabelText(title,position);
}else{
visibleText = title;
}
setText(visibleText);
}
public boolean isRedStart() {
return redStart;
}
/**
* 设置标签 显示红星
* @param redStart
*/
public void setRedStart(boolean redStart) {
this.redStart = redStart;
if(redStart)
visibleText = setLabelText(text,position);
setText(visibleText);
this.repaint();
}
public String getPosition() {
return position;
}
/**
* 设置 * 号在标签上的位置
* @param defaultPosition
*/
public void setPosition(String defaultPosition) {
this.position = defaultPosition;
if(redStart){
visibleText = setLabelText(text,position);
}
setText(visibleText);
this.repaint();
}
private String setLabelText(String text ,String direction){
if("left".equals(position)){
visibleText = "<html><font color=red>*</font>"+text+"</html>";
}else if("right".equals(position)){
visibleText = "<html>"+text+"<font color=red>*</font></html>";
}else{
visibleText = "<html>"+text+"<font color=red>*</font></html>";
}
return visibleText;
}