1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  import org.aswing.ASWingConstants;
     5  import org.aswing.Component;
     6  import org.aswing.GrayFilteredIcon;
     7  import org.aswing.Icon;
     8  import org.aswing.plaf.LabelUI;
     9  import org.aswing.UIManager;
    10  import org.aswing.utils.StringUtils;
    11  
    12  /**
    13   *
    14   * @author iiley
    15   */
    16  class org.aswing.JLabel extends Component {
    17  	
    18  	/**
    19  	 * A fast access to ASSwingConstants Constant
    20  	 * @see org.aswing.ASSwingConstants
    21  	 */
    22  	public static var CENTER:Number  = ASWingConstants.CENTER;
    23  	/**
    24  	 * A fast access to ASWingConstants Constant
    25  	 * @see org.aswing.ASWingConstants
    26  	 */
    27  	public static var TOP:Number     = ASWingConstants.TOP;
    28  	/**
    29  	 * A fast access to ASWingConstants Constant
    30  	 * @see org.aswing.ASWingConstants
    31  	 */
    32      public static var LEFT:Number    = ASWingConstants.LEFT;
    33  	/**
    34  	 * A fast access to ASWingConstants Constant
    35  	 * @see org.aswing.ASWingConstants
    36  	 */
    37      public static var BOTTOM:Number  = ASWingConstants.BOTTOM;
    38   	/**
    39  	 * A fast access to ASWingConstants Constant
    40  	 * @see org.aswing.ASWingConstants
    41  	 */
    42      public static var RIGHT:Number   = ASWingConstants.RIGHT;
    43  	/**
    44  	 * A fast access to ASWingConstants Constant
    45  	 * @see org.aswing.ASWingConstants
    46  	 */        
    47  	public static var HORIZONTAL:Number = ASWingConstants.HORIZONTAL;
    48  	/**
    49  	 * A fast access to ASWingConstants Constant
    50  	 * @see org.aswing.ASWingConstants
    51  	 */
    52  	public static var VERTICAL:Number   = ASWingConstants.VERTICAL;
    53  	
    54  	
    55  	private var icon:Icon;
    56  	private var text:String;
    57  	private var disabledIcon:Icon;
    58  	
    59  	// Icon/Label Alignment
    60      private var verticalAlignment:Number;
    61      private var horizontalAlignment:Number;
    62      
    63      private var verticalTextPosition:Number;
    64      private var horizontalTextPosition:Number;
    65  
    66      private var iconTextGap:Number;
    67      
    68      /**
    69       * JLabel(text:String, icon:Icon, horizontalAlignment:Number)<br>
    70       * JLabel(text:String, icon:Icon)<br>
    71       * JLabel(text:String, horizontalAlignment:Number)<br>
    72       * JLabel(text:String)<br>
    73       * JLabel(icon:Icon, horizontalAlignment:Number)<br>
    74       * JLabel(icon:Icon)<br>
    75       * JLabel()<br>
    76  	 * <p>
    77       */
    78  	function JLabel(text, icon, horizontalAlignment:Number) {
    79  		super();
    80  		setName("JLabel");
    81  		//default
    82      	this.verticalAlignment = CENTER;
    83      	this.horizontalAlignment = CENTER;
    84      	this.verticalTextPosition = CENTER;
    85      	this.horizontalTextPosition = RIGHT;
    86      	
    87      	iconTextGap = 0;
    88      	
    89      	if(text == undefined){
    90      		if(icon != null){
    91      			this.icon = Icon(icon);
    92      			if(horizontalAlignment != undefined){
    93      				this.horizontalAlignment = horizontalAlignment;
    94      			}
    95      		}
    96      	}else if(horizontalAlignment != undefined){
    97  			this.horizontalAlignment = horizontalAlignment;
    98  			this.text = StringUtils.castString(text);
    99  			this.icon = Icon(icon);
   100  		}else if(text instanceof Icon){
   101  			this.text = null;
   102  			this.icon = Icon(text);
   103  			if(icon != undefined){
   104  				this.horizontalAlignment = Number(icon);
   105  			}
   106  		}else{
   107  			this.text = StringUtils.castString(text);
   108  			if(icon != undefined){
   109  				if(icon instanceof Icon){
   110  					this.icon = Icon(icon);
   111  				}else{
   112  					this.horizontalAlignment = Number(icon);
   113  				}
   114  			}
   115  		}
   116  		
   117  		updateUI();
   118  	}
   119  	
   120      public function updateUI():Void{
   121      	setUI(LabelUI(UIManager.getUI(this)));
   122      }
   123      
   124      public function setUI(newUI:LabelUI):Void{
   125      	super.setUI(newUI);
   126      }
   127  	
   128  	public function getUIClassID():String{
   129  		return "LabelUI";
   130  	}	
   131  
   132  	/**
   133  	 * Sets text and icon at one method here.
   134  	 * @param text the text for the label
   135  	 * @param icon the default icon for the label
   136  	 * @see #setText()
   137  	 * @see #setIcon()
   138  	 */	
   139  	public function setContent(text:String, icon:Icon):Void{
   140  		if(this.text != text){
   141  			this.text = text;
   142  		}
   143  		if(this.icon != icon){
   144  			uninstallIconWhenNextPaint(this.icon);
   145  			this.icon = icon;
   146  		}
   147  		repaint();
   148  		invalidate();
   149  	}
   150  	
   151  	public function setText(text:String):Void{
   152  		if(this.text != text){
   153  			this.text = text;
   154  			repaint();
   155  			invalidate();
   156  		}
   157  	}
   158  	
   159  	public function getText():String{
   160  		return text;
   161  	}
   162  	
   163  	public function setIcon(icon:Icon):Void{
   164  		if(this.icon != icon){
   165  			uninstallIconWhenNextPaint(this.icon);
   166  			this.icon = icon;
   167  			repaint();
   168  			invalidate();
   169  		}
   170  	}
   171  
   172  	public function getIcon():Icon{
   173  		return icon;
   174  	}
   175  
   176      /**
   177       * Returns the icon used by the label when it's disabled.
   178       * If no disabled icon has been set, the button constructs
   179       * one from the default icon if defalut icon setted. otherwish 
   180       * return null; 
   181       * <p>
   182       * The disabled icon really should be created 
   183       * (if necessary) by the L&F.-->
   184       *
   185       * @return the <code>disabledIcon</code> property
   186       * @see #setDisabledIcon()
   187       * @see #getEnabled()
   188       */
   189      public function getDisabledIcon():Icon {
   190          if(disabledIcon == null) {
   191              if(icon != null) {
   192                  disabledIcon = new GrayFilteredIcon(icon);
   193              }
   194          }
   195          return disabledIcon;
   196      }
   197      
   198      /**
   199       * Sets the disabled icon for the label.
   200       * @param disabledIcon the icon used as the disabled image
   201       * @see #getDisabledIcon()
   202       * @see #setEnabled()
   203       */
   204      public function setDisabledIcon(disabledIcon:Icon):Void {
   205          var oldValue:Icon = this.disabledIcon;
   206          this.disabledIcon = disabledIcon;
   207          if (disabledIcon != oldValue) {
   208          	uninstallIconWhenNextPaint(oldValue);
   209              if (!isEnabled()) {
   210                  repaint();
   211  				invalidate();
   212              }
   213          }
   214      }
   215  
   216      /**
   217       * Returns the vertical alignment of the text and icon.
   218       *
   219       * @return the <code>verticalAlignment</code> property, one of the
   220       *		following values: 
   221       * <ul>
   222       * <li>ASWingConstants.CENTER (the default)
   223       * <li>ASWingConstants.TOP
   224       * <li>ASWingConstants.BOTTOM
   225       * </ul>
   226       */
   227      public function getVerticalAlignment():Number {
   228          return verticalAlignment;
   229      }
   230      
   231      /**
   232       * Sets the vertical alignment of the icon and text.
   233       * @param alignment  one of the following values:
   234       * <ul>
   235       * <li>ASWingConstants.CENTER (the default)
   236       * <li>ASWingConstants.TOP
   237       * <li>ASWingConstants.BOTTOM
   238       * </ul>
   239       */
   240      public function setVerticalAlignment(alignment:Number):Void {
   241          if (alignment == verticalAlignment){
   242          	return;
   243          }else{
   244          	verticalAlignment = alignment;
   245          	repaint();
   246          }
   247      }
   248      
   249      /**
   250       * Returns the horizontal alignment of the icon and text.
   251       * @return the <code>horizontalAlignment</code> property,
   252       *		one of the following values:
   253       * <ul>
   254       * <li>ASWingConstants.RIGHT (the default)
   255       * <li>ASWingConstants.LEFT
   256       * <li>ASWingConstants.CENTER
   257       * </ul>
   258       */
   259      public function getHorizontalAlignment():Number{
   260          return horizontalAlignment;
   261      }
   262      
   263      /**
   264       * Sets the horizontal alignment of the icon and text.
   265       * @param alignment  one of the following values:
   266       * <ul>
   267       * <li>ASWingConstants.RIGHT (the default)
   268       * <li>ASWingConstants.LEFT
   269       * <li>ASWingConstants.CENTER
   270       * </ul>
   271       */
   272      public function setHorizontalAlignment(alignment:Number):Void {
   273          if (alignment == horizontalAlignment){
   274          	return;
   275          }else{
   276          	horizontalAlignment = alignment;     
   277          	repaint();
   278          }
   279      }
   280  
   281      
   282      /**
   283       * Returns the vertical position of the text relative to the icon.
   284       * @return the <code>verticalTextPosition</code> property, 
   285       *		one of the following values:
   286       * <ul>
   287       * <li>ASWingConstants.CENTER  (the default)
   288       * <li>ASWingConstants.TOP
   289       * <li>ASWingConstants.BOTTOM
   290       * </ul>
   291       */
   292      public function getVerticalTextPosition():Number{
   293          return verticalTextPosition;
   294      }
   295      
   296      /**
   297       * Sets the vertical position of the text relative to the icon.
   298       * @param alignment  one of the following values:
   299       * <ul>
   300       * <li>ASWingConstants.CENTER (the default)
   301       * <li>ASWingConstants.TOP
   302       * <li>ASWingConstants.BOTTOM
   303       * </ul>
   304       */
   305      public function setVerticalTextPosition(textPosition:Number):Void {
   306          if (textPosition == verticalTextPosition){
   307  	        return;
   308          }else{
   309          	verticalTextPosition = textPosition;
   310          	repaint();
   311          }
   312      }
   313      
   314      /**
   315       * Returns the horizontal position of the text relative to the icon.
   316       * @return the <code>horizontalTextPosition</code> property, 
   317       * 		one of the following values:
   318       * <ul>
   319       * <li>ASWingConstants.RIGHT (the default)
   320       * <li>ASWingConstants.LEFT
   321       * <li>ASWingConstants.CENTER
   322       * </ul>
   323       */
   324      public function getHorizontalTextPosition():Number {
   325          return horizontalTextPosition;
   326      }
   327      
   328      /**
   329       * Sets the horizontal position of the text relative to the icon.
   330       * @param textPosition one of the following values:
   331       * <ul>
   332       * <li>ASWingConstants.RIGHT (the default)
   333       * <li>ASWingConstants.LEFT
   334       * <li>ASWingConstants.CENTER
   335       * </ul>
   336       */
   337      public function setHorizontalTextPosition(textPosition:Number):Void {
   338          if (textPosition == horizontalTextPosition){
   339          	return;
   340          }else{
   341          	horizontalTextPosition = textPosition;
   342          	repaint();
   343          }
   344      }
   345      
   346      /**
   347       * Returns the amount of space between the text and the icon
   348       * displayed in this button.
   349       *
   350       * @return an int equal to the number of pixels between the text
   351       *         and the icon.
   352       * @see #setIconTextGap()
   353       */
   354      public function getIconTextGap():Number {
   355          return iconTextGap;
   356      }
   357  
   358      /**
   359       * If both the icon and text properties are set, this property
   360       * defines the space between them.  
   361       * <p>
   362       * The default value of this property is 4 pixels.
   363       * 
   364       * @see #getIconTextGap()
   365       */
   366      public function setIconTextGap(iconTextGap:Number):Void {
   367          var oldValue:Number = this.iconTextGap;
   368          this.iconTextGap = iconTextGap;
   369          if (iconTextGap != oldValue) {
   370              revalidate();
   371              repaint();
   372          }
   373      }
   374  
   375  }
   376