1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4   
     5  import org.aswing.geom.Dimension;
     6  import org.aswing.JTextComponent;
     7  import org.aswing.plaf.TextUI;
     8  import org.aswing.UIManager;
     9  
    10  /**
    11   * JTextField is a component that allows the editing of a single line of text. 
    12   * @author Tomato, iiley
    13   */
    14  class org.aswing.JTextField extends JTextComponent {
    15  	
    16  	private var columns:Number;
    17  	
    18  	/**
    19  	 * JTextField(text:String, columns:Number)<br>
    20  	 * JTextField(text:String) columns default to 0<br>
    21  	 * JTextField() text default to null and columns default to 0<br>
    22  	 * <p>
    23  	 * Constructs a new TextField initialized with the specified text and columns.
    24  	 * 
    25  	 * @param text the text to be displayed, if it is null or undefined, it will be set to "";
    26  	 * @param columns the number of columns to use to calculate the preferred width;
    27  	 * if columns is set to zero or min than zero, the preferred width will be matched just to view all of the text.
    28  	 * default value is zero if missed this param.
    29  	 * @see #setColumns()
    30  	 */
    31  	public function JTextField(text:String, columns:Number){
    32  		super();
    33  		setName("JTextField");
    34  		this.text = (text == undefined ? "" : text);
    35  		setColumns(columns);
    36  		updateUI();
    37  	}
    38  	
    39      public function updateUI():Void{
    40      	setUI(TextUI(UIManager.getUI(this)));
    41      }	
    42  	
    43  	public function getUIClassID():String{
    44  		return "TextFieldUI";
    45  	}
    46  	
    47  	/**
    48  	 * Sets the number of columns in this JTextField, if it changed then call parent to do layout. 
    49  	 * @param columns the number of columns to use to calculate the preferred width;
    50  	 * if columns is set to zero or min than zero, the preferred width will be matched just to view all of the text.
    51  	 * default value is zero if missed this param.
    52  	 */
    53  	public function setColumns(columns:Number):Void{
    54  		if(columns == undefined) columns = 0;
    55  		if(columns < 0) columns = 0;
    56  		if(this.columns != columns){
    57  			this.columns = columns;
    58  			if(displayable){
    59  				revalidate();
    60  			}
    61  		}
    62  	}
    63  	
    64  	/**
    65  	 * @see #setColumns
    66  	 */
    67  	public function getColumns():Number{
    68  		return columns;
    69  	}
    70  	
    71  	/**
    72  	 * JTextComponent need count preferred size itself.
    73  	 */
    74  	private function countPreferredSize():Dimension{
    75  		if(columns > 0){
    76  			var columnWidth:Number = getColumnWidth();
    77  			var width:Number = columnWidth * columns + getWidthMargin();
    78  			var height:Number = getRowHeight() + getHeightMargin();
    79  			var size:Dimension = new Dimension(width, height);
    80  			return getInsets().roundsSize(size);
    81  		}else{
    82  			return getInsets().roundsSize(getTextFieldAutoSizedSize());
    83  		}
    84  	}
    85  }
    86