1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  import org.aswing.utils.HashMap;
     5  import org.aswing.Component;
     6  import org.aswing.utils.Delegate;
     7  
     8  /**
     9   * FocusManager manages all the when a component should receive focus,i.e if it
    10   * can .
    11   * @author firdosh
    12   */
    13  
    14  
    15  class org.aswing.FocusManager{
    16  	/**
    17  	 * Stores all the components created and uses their tabIndex as the 
    18  	 * key
    19  	 */
    20  	private var _componentList:HashMap;
    21  	/**
    22  	 * Instance of the FocusManager
    23  	 */
    24  	private static var _instance:FocusManager;
    25  	/**
    26  	 * Current Component that has focus
    27  	 */
    28  	private var _currentComp:Component=null;
    29  	/**
    30  	 * Determines which key will control the FocusManager
    31  	 */
    32      private var _keyType:Number=Key.TAB;
    33      /**
    34  	 * Listener for key events
    35  	 */
    36  	private static  var  _keyListener:Object;
    37  	 /**
    38  	 * Stores the current Index of the components
    39  	 */
    40  	private var _currentIndex:Number=0;
    41  	 /**
    42  	 * Stores the keys for quick access
    43  	 */
    44  	private var _indexes:Array;
    45  	
    46  	
    47  	private function FocusManager(Void){
    48  		_componentList=new HashMap();
    49  		_keyListener = new Object();
    50  		//_keyListener.onKeyDown=Delegate.create(this,onKeyPress);
    51  		Key.addListener(_keyListener);	
    52  		_indexes=new Array();
    53  	}
    54  	
    55  	/**
    56  	 * Returns an instance of FocusManager. 
    57  	 */
    58  	
    59  	public  static function getCurrentManager():FocusManager{		
    60  		if(_instance==null){
    61  			_instance=new FocusManager();	
    62  		}		
    63  		return _instance;
    64  	}
    65  	
    66  	public  static function setCurrentManager(manager:FocusManager):Void{
    67  		_instance=manager;	
    68  	}
    69  	
    70  	/**
    71  	 *Adds components to the HashMap and stores the key as the
    72  	 *TabIndex
    73  	 */
    74  	
    75  	/*public function append(compObj:Component):Void{
    76  		if(compObj.getTabIndex()==-1){
    77  			compObj.setTabIndex(_componentList.size());
    78  		}
    79  				
    80  		if(_currentComp==null){
    81  			_currentComp=compObj;
    82  		}
    83  		
    84  		_componentList.put(compObj.getTabIndex()+"",compObj);
    85  		_indexes.push(compObj.getTabIndex());
    86  		_indexes.sort(Array.NUMERIC );
    87  	  
    88  		
    89  	}*/
    90  	/**
    91  	 *Re-orders the list when the user changes the tabIndex of a component
    92  	 *if the tabIndex clashes with another components tabIndex it will exit the method
    93  	 */
    94  	public function sortKeys(oldVal:Number , newVal:Number,c:Component):Void{
    95  	  /**
    96  	 *Returns if component tabIndex clashes with another component tabIndex
    97  	 */
    98  		
    99  		for(var i:Number=0;i<_indexes.length;i++){
   100  				if(_indexes[i]==newVal){
   101  					return;	
   102  				}		
   103  		}
   104  		
   105  	 /**
   106  	 *If the component exists it changes its key with the new tabIndex
   107  	 */
   108  		for(var i:Number=0;i<_indexes.length;i++){
   109  				if(_indexes[i]==oldVal){
   110  					_indexes[i]=newVal;
   111  					_indexes.sort(Array.NUMERIC );
   112  					_componentList.remove(oldVal+"");
   113  					_componentList.put(newVal+"",c);
   114  					return;
   115  				}				
   116  		}
   117  		
   118  	}
   119  	
   120  	 /**
   121  	 *Disables / Enables the FocusManager
   122  	 */	
   123  	public  function enabled(val:Boolean):Void{
   124  		if(val ==false ){
   125  			Key.removeListener(_keyListener);			
   126  		}
   127  		else{
   128  			_keyListener = new Object();
   129  			//_keyListener.onKeyDown=Delegate.create(this,onKeyPress);
   130  			Key.addListener(_keyListener);				
   131  		}
   132  	}
   133  	
   134  	 /**
   135  	 *Triggers the onSetFocus focus of the next  component to receive focus
   136  	 */	
   137  	
   138  	/*private  function onKeyPress():Void{
   139  		if(Key.isDown(_keyType)){			
   140  			
   141  			if(	_currentIndex==	_indexes.length){
   142  				_currentIndex=0;	
   143  			}			
   144  			
   145  			var temp:Component=Component(_componentList.get(_indexes[_currentIndex]+""));
   146  			
   147  			while((!temp.isFocusable()) && temp.getTabIndex()!=null){
   148  				_currentIndex++;
   149  				temp=Component(_componentList.get(_indexes[_currentIndex]+""));				
   150  			}
   151  			
   152  			if(_currentComp!=null){
   153  				_currentComp.setFocus(false);
   154  			}
   155  			
   156  			_currentComp=temp;
   157  			temp.setFocus(true);
   158  			_currentIndex++;					
   159  			
   160  		}
   161  		
   162  	}*/
   163  	
   164  	 /**
   165  	 *Sets what key the user will use for focusing
   166  	 */
   167  	public function setKeyType(val:Number):Void{
   168  		_keyType=val;	
   169  	}
   170  	 /**
   171  	 *Resets the FocusManager
   172  	 */
   173  	public function resetFocusManager(Void):Void{	
   174  			_currentComp.setFocus(false);
   175  			_currentIndex=0;	
   176  		    _currentComp=Component(_componentList.get(_indexes[_currentIndex]+""));
   177  	}
   178  	
   179  	
   180  }
   181  
   182