1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  
     5  import org.aswing.ASColor;
     6  import org.aswing.ASFont;
     7  import org.aswing.border.Border;
     8  import org.aswing.Component;
     9  import org.aswing.Icon;
    10  import org.aswing.Insets;
    11  import org.aswing.plaf.ComponentUI;
    12  import org.aswing.utils.HashMap;
    13  
    14  /**
    15   * A table of defaults for Swing components.  Applications can set/get
    16   * default values via the <code>UIManager</code>.
    17   * 
    18   * @see UIManager
    19   * @author iiley
    20   */
    21  class org.aswing.UIDefaults extends HashMap{
    22  	
    23  	private static var CREATE_METHOD_NAME:String = "createInstance";
    24  	
    25  	
    26  	public function UIDefaults() {
    27  		super();
    28  	}
    29  	
    30      /**
    31       * Sets the value of <code>key</code> to <code>value</code>.
    32       * If value is <code>null</code>, the key is removed from the table.
    33       *
    34       * @param key    the unique <code>Object</code> who's value will be used
    35       *          to retrieve the data value associated with it
    36       * @param value  the new <code>Object</code> to store as data under
    37       *		that key
    38       * @return the previous <code>Object</code> value, or <code>null</code>
    39       * @see #putDefaults()
    40       * @see org.aswing.utils.HashMap#put()
    41       */	
    42   	public function put(key:String, value:Object){
    43   		var oldValue = (value == null) ? super.remove(key) : super.put(key, value);
    44   		return oldValue;
    45   	}
    46   	
    47  	/**
    48       * Puts all of the key/value pairs in the database.
    49       * @param keyValueList  an array of key/value pairs
    50       * @see put
    51       * @see org.aswing.utils.Hashtable#put
    52       */
    53  	public function putDefaults(keyValueList:Array):Void{
    54  		for(var i:Number = 0; i < keyValueList.length; i += 2) {
    55              var value = keyValueList[i + 1];
    56              if (value == null) {
    57                  super.remove(keyValueList[i]);
    58              }else {
    59                  super.put(keyValueList[i], value);
    60              }
    61          }
    62  	}
    63  	
    64  	/**
    65  	 * Returns the component LookAndFeel specified UI object
    66  	 * @return target's UI object, or null if there is not his UI object
    67  	 */
    68  	public function getUI(target:Component):ComponentUI{
    69  		return ComponentUI(getInstance(target.getUIClassID()));
    70  	}
    71  	
    72  	public function getBoolean(key:String):Boolean{
    73  		return (get(key) == true);
    74  	}
    75  	
    76  	public function getNumber(key:String):Number{
    77  		return get(key);
    78  	}
    79  	
    80  	public function getBorder(key:String):Border{
    81  		var border:Border = Border(getInstance(key));
    82  		if(border == null){
    83  			border = undefined; //make it to be an undefined property then can override by next LAF
    84  		}
    85  		return border;
    86  	}
    87  	
    88  	public function getIcon(key:String):Icon{
    89  		var icon:Icon = Icon(getInstance(key));
    90  		if(icon == null){
    91  			icon = undefined; //make it to be an undefined property then can override by next LAF
    92  		}
    93  		return icon;
    94  	}
    95  	
    96  	public function getColor(key:String):ASColor{
    97  		var color:ASColor = ASColor(getInstance(key));
    98  		if(color == null){
    99  			color = undefined; //make it to be an undefined property then can override by next LAF
   100  		}
   101  		return color;
   102  	}
   103  	
   104  	public function getFont(key:String):ASFont{
   105  		var font:ASFont = ASFont(getInstance(key));
   106  		if(font == null){
   107  			font = undefined; //make it to be an undefined property then can override by next LAF
   108  		}
   109  		return font;
   110  	}
   111  	
   112  	public function getInsets(key:String):Insets{
   113  		var i:Insets = Insets(getInstance(key));
   114  		if(i == null){
   115  			i = undefined; //make it to be an undefined property then can override by next LAF
   116  		}
   117  		return i;
   118  	}	
   119  	
   120  	//-------------------------------------------------------------
   121  	public function getConstructor(key:String):Function{
   122  		return Function(this.get(key));
   123  	}
   124  	
   125  	public function getInstance(key:String):Object{
   126  		var value = this.get(key);
   127  		if(value instanceof Function){
   128  			return getCreateInstance(Function(value));
   129  		}else{
   130  			return value;
   131  		}
   132  	}
   133  	
   134  	private function getCreateInstance(constructor:Function):Object{
   135  		var instance:Object = constructor[CREATE_METHOD_NAME]();
   136  		if(instance == null){
   137  			instance = new constructor();
   138  		}
   139  		return instance;
   140  	}	
   141  }
   142