1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  import org.aswing.ASWingUtils;
     5  import org.aswing.geom.Rectangle;
     6  import org.aswing.Icon;
     7  import org.aswing.JWindow;
     8  import org.aswing.plaf.WindowUI;
     9  import org.aswing.Resizer;
    10  import org.aswing.UIManager;
    11  import org.aswing.utils.StringUtils;
    12  
    13  /**
    14   *
    15   * @author iiley
    16   */
    17  class org.aswing.JFrame extends JWindow {
    18  	/**
    19  	 * When the frame's state changed.
    20  	 *<br>
    21  	 * onStateChanged Event{source:JFrame}
    22  	 */	
    23  	public static var ON_STATE_CHANGED:String = "onStateChanged";//Component.ON_STATE_CHANGED; 
    24  	
    25  	/**
    26  	 * When the frame's ability changed. Include:
    27  	 * <ul>
    28  	 * <li> resizable
    29  	 * <li> closable
    30  	 * <li> dragable
    31  	 * </ul>
    32  	 *<br>
    33  	 * onAbilityChanged Event{source:JFrame}
    34  	 */	
    35  	public static var ON_ABILITY_CHANGED:String = "onAbilityChanged"; 
    36  	
    37  	
    38  	/**
    39  	 * @see #setState()
    40  	 */
    41  	public static var NORMAL:Number = 0; //0
    42  	/**
    43  	 * @see #setState()
    44  	 */
    45  	public static var ICONIFIED:Number = 2; //10
    46  	/**
    47  	 * @see #setState()
    48  	 */
    49  	public static var MAXIMIZED_HORIZ:Number = 4;  //100
    50  	/**
    51  	 * @see #setState()
    52  	 */
    53  	public static var MAXIMIZED_VERT:Number = 8;  //1000
    54  	/**
    55  	 * @see #setState()
    56  	 */
    57  	public static var MAXIMIZED:Number = 12;  //1100
    58  	//-----------------------------------------
    59  	
    60  	/**
    61  	 * @see #setDefaultCloseOperation()
    62  	 */
    63  	public static var DO_NOTHING_ON_CLOSE:Number = 0;
    64  	/**
    65  	 * @see #setDefaultCloseOperation()
    66  	 */
    67  	public static var HIDE_ON_CLOSE:Number = 1;
    68  	/**
    69  	 * @see #setDefaultCloseOperation()
    70  	 */
    71  	public static var DISPOSE_ON_CLOSE:Number = 2;
    72  		
    73  	//--------------------------------------------------------
    74  	
    75  	private var title:String;
    76  	private var icon:Icon;
    77  	private var state:Number;
    78  	private var defaultCloseOperation:Number;
    79  	private var maximizedBounds:Rectangle;
    80  	
    81  	private var dragable:Boolean;
    82  	private var resizable:Boolean;
    83  	private var closable:Boolean;
    84  	private var dragDirectly:Boolean;
    85  	
    86  	private var resizer:Resizer;
    87  	
    88  	/**
    89  	 * JFrame(title:String, modal:Boolean)<br>
    90  	 * JFrame(title:String)<br>
    91  	 * JFrame(owner, title:String, modal:Boolean)<br>
    92  	 * JFrame(owner, title:String)<br>
    93  	 * JFrame(owner)<br>
    94  	 * JFrame()<br>
    95  	 * 
    96  	 * @param owner the owner of this window, a <code>JWindow</code> 
    97  	 * or a <code>MovieClip</code>, default is <code>ASWingUtils.getRootMovieClip()</code> 
    98  	 * @param title the String to display in the dialog's title bar. default is undefined.
    99  	 * @param modal true for a modal dialog, false for one that allows other windows to be active at the same time,
   100  	 *  default is false.
   101  	 * @see org.aswing.JWindow
   102  	 * @see org.aswing.ASWingUtils#getRootMovieClip()
   103  	 */	
   104  	public function JFrame(owner, title, modal : Boolean) {
   105  		super(StringUtils.isString(title) ? owner : (StringUtils.isString(owner) ? undefined : owner), //judge owner parameter for JWindow
   106  		 	  StringUtils.isString(title) ? modal : title); //judge modal parameter for JWindow
   107  		 	  
   108  		if(StringUtils.isString(title)){
   109  			this.title = StringUtils.castString(title);
   110  		}else if(StringUtils.isString(owner)){
   111  			this.title = StringUtils.castString(owner);
   112  		}
   113  		
   114  		state = NORMAL;
   115  		defaultCloseOperation = HIDE_ON_CLOSE;
   116  		dragable  = true;
   117  		resizable = true;
   118  		closable  = true;
   119  		
   120  		setName("JFrame");
   121  		
   122  		updateUINow = true;
   123  		updateUI();
   124  	}
   125  	
   126  	private var updateUINow:Boolean;
   127  	public function updateUI():Void{
   128  		if(updateUINow){
   129      		setUI(WindowUI(UIManager.getUI(this)));
   130  		}
   131      }
   132      
   133  	public function getUIClassID():String{
   134  		return "FrameUI";
   135  	}
   136  		
   137  	/**
   138  	 * Sets the text to be displayed in the title bar for this frame.
   139  	 * @param t the text to be displayed in the title bar, 
   140  	 * null to display no text in the title bar.
   141  	 */
   142  	public function setTitle(t:String):Void{
   143  		if(title != t){
   144  			title = t;
   145  			repaint();
   146  			revalidate();
   147  		}
   148  	}
   149  	
   150  	/**
   151  	 * Returns the text displayed in the title bar for this frame.
   152  	 * @return the text displayed in the title bar for this frame.
   153  	 */
   154  	public function getTitle():String{
   155  		return title;
   156  	}
   157  	
   158  	/**
   159  	 * Sets the icon to be displayed in the title bar for this frame.
   160  	 * @param ico the icon to be displayed in the title bar, 
   161  	 * null to display no icon in the title bar.
   162  	 */	
   163  	public function setIcon(ico:Icon):Void{
   164  		if(icon != ico){
   165  			icon = ico;
   166  			repaint();
   167  			revalidate();
   168  		}
   169  	}
   170  	
   171  	/**
   172  	 * Returns the icon displayed in the title bar for this frame.
   173  	 * @return the icon displayed in the title bar for this frame.
   174  	 */
   175  	public function getIcon():Icon{
   176  		return icon;
   177  	}
   178  		
   179  	/**
   180  	 * Sets whether this frame is resizable by the user.
   181  	 * 
   182  	 * <p>"resizable" means include capability of restore normal resize, maximize, iconified and resize by drag.
   183  	 * @param b true user can resize the frame by click resize buttons or drag to scale the frame, false user can't.
   184  	 * @see #isResizable()
   185  	 */
   186  	public function setResizable(b:Boolean):Void{
   187  		if(resizable != b){
   188  			resizable = b;
   189  			getResizer().setEnabled(b);
   190  			repaint();
   191  			revalidate();
   192  		}
   193  	}
   194  	
   195  	/**
   196  	 * Returns whether this frame is resizable by the user. By default, all frames are initially resizable. 
   197  	 * 
   198  	 * <p>"resizable" means include capability of restore normal resize, maximize, iconified and resize by drag.
   199  	 * @see #setResizable()
   200  	 */
   201  	public function isResizable():Boolean{
   202  		return resizable;
   203  	}
   204  	
   205  	/**
   206  	 * Sets whether this frame can be dragged by the user.  By default, it's true.
   207  	 * 
   208  	 * <p>"dragable" means drag to move the frame.
   209  	 * @param b 
   210  	 * @see #isDragable()
   211  	 */
   212  	public function setDragable(b:Boolean):Void{
   213  		if(dragable != b){
   214  			dragable = b;
   215  			repaint();
   216  			revalidate();
   217  		}
   218  	}
   219  	
   220  	/**
   221  	 * Returns whether this frame can be dragged by the user. By default, it's true.
   222  	 * @see #setDragnable()
   223  	 */
   224  	public function isDragable():Boolean{
   225  		return dragable;
   226  	}
   227  	
   228  
   229  	/**
   230  	 * Sets whether this frame can be closed by the user. By default, it's true.
   231  	 * Whether the frame will be hide or dispose, depend on the value returned by <code>getDefaultCloseOperation</code>.
   232  	 * @param b true user can click close button to generate the close event, false user can't.
   233  	 * @see #getClosable()
   234  	 */	
   235  	public function setClosable(b:Boolean):Void{
   236  		if(closable != b){
   237  			closable = b;
   238  			repaint();
   239  			revalidate();
   240  		}
   241  	}
   242  	
   243  	/**
   244  	 * Returns whether this frame can be closed by the user. By default, it's true.
   245  	 * @see #setClosable()
   246  	 */		
   247  	public function isClosable():Boolean{
   248  		return closable;
   249  	}
   250  	
   251  	/**
   252  	 * Only did effect when state is <code>NORMAL</code>
   253  	 */
   254  	public function pack():Void{
   255  		if(getState() == NORMAL){
   256  			super.pack();
   257  		}
   258  	}	
   259  	
   260  	/**
   261  	 * Gets maximized bounds for this frame.<br>
   262  	 * If the maximizedBounds was setted by setMaximizedBounds it will return the setted value.
   263  	 * else if the owner is a JWindow it will return the owner's content pane's bounds, if
   264  	 * the owner is a movieclip it will return the movie's stage bounds.
   265  	 */
   266  	public function getMaximizedBounds():Rectangle{
   267  		if(maximizedBounds == null){
   268  			return ASWingUtils.getVisibleMaximizedBounds(root_mc);
   269  		}else{
   270  			return maximizedBounds;
   271  		}
   272  	}
   273  	
   274  	/**
   275  	 * Sets the maximized bounds for this frame. 
   276  	 * <br>
   277  	 * @param b bounds for the maximized state, null to back to use default bounds descripted in getMaximizedBounds's comments.
   278  	 * @see #getMaximizedBounds()
   279  	 */
   280  	public function setMaximizedBounds(b:Rectangle):Void{
   281  		if(b != null){
   282  			maximizedBounds = new Rectangle(b);
   283  			revalidate();
   284  		}else{
   285  			maximizedBounds = null;
   286  		}
   287  	}	
   288  
   289      /**                   
   290       * Sets the operation that will happen by default when
   291       * the user initiates a "close" on this frame.
   292       * You must specify one of the following choices:
   293       * <p>
   294       * <ul>
   295       * <li><code>DO_NOTHING_ON_CLOSE</code>
   296       * (defined in <code>WindowConstants</code>):
   297       * Don't do anything; require the
   298       * program to handle the operation in the <code>windowClosing</code>
   299       * method of a registered EventListener object.
   300       *
   301       * <li><code>HIDE_ON_CLOSE</code>
   302       * (defined in <code>WindowConstants</code>):
   303       * Automatically hide the frame after
   304       * invoking any registered EventListener objects.
   305       *
   306       * <li><code>DISPOSE_ON_CLOSE</code>
   307       * (defined in <code>WindowConstants</code>):
   308       * Automatically hide and dispose the 
   309       * frame after invoking any registered EventListener objects.
   310       * </ul>
   311       * <p>
   312       * The value is set to <code>HIDE_ON_CLOSE</code> by default.
   313       * if you set a value is not three of them, think of it is will be changed to default value.
   314       * @param operation the operation which should be performed when the
   315       *        user closes the frame
   316       * @see org.aswing.Component#addEventListener()
   317       * @see #getDefaultCloseOperation()
   318       */
   319      public function setDefaultCloseOperation(operation:Number):Void {
   320      	if(operation != DO_NOTHING_ON_CLOSE 
   321      		&& operation != HIDE_ON_CLOSE
   322      		&& operation != DISPOSE_ON_CLOSE)
   323      	{
   324      			operation = HIDE_ON_CLOSE;
   325      	}
   326      	defaultCloseOperation = operation;
   327      }
   328      
   329  	/**
   330  	 * Returns the operation that will happen by default when
   331       * the user initiates a "close" on this frame.
   332  	 * @see #setDefaultCloseOperation()
   333  	 */
   334  	public function getDefaultCloseOperation():Number{
   335  		return defaultCloseOperation;
   336  	}
   337  	
   338  	public function setState(s:Number):Void{
   339  		if(state != s){
   340  			state = s;
   341  			fireStateChanged();
   342  			if(state == ICONIFIED){
   343  				dispatchEvent(ON_WINDOW_ICONIFIED, createEventObj(ON_WINDOW_ICONIFIED));
   344  			}else if((state & MAXIMIZED_HORIZ == MAXIMIZED_HORIZ) || (state & MAXIMIZED_VERT == MAXIMIZED_VERT)){
   345  				dispatchEvent(ON_WINDOW_MAXIMIZED, createEventObj(ON_WINDOW_MAXIMIZED));
   346  			}else{
   347  				dispatchEvent(ON_WINDOW_RESTORED, createEventObj(ON_WINDOW_RESTORED));
   348  			}
   349  			return;
   350  		}
   351  	}
   352  	
   353  	public function getState():Number{
   354  		return state;
   355  	}
   356  		
   357  	private function create():Void{
   358  		super.create();
   359  		resizer.createTo(root_mc);
   360  	}
   361  	
   362  	public function setResizer(r:Resizer):Void{
   363  		if(r != resizer && r != null){
   364  			resizer = r;
   365  			r.setOwner(this);
   366  			if(isDisplayable()){
   367  				r.createTo(root_mc);
   368  			}
   369  			r.setEnabled(isResizable());
   370  		}
   371  	}
   372  	
   373  	public function getResizer():Resizer{
   374  		return resizer;
   375  	}
   376  	
   377  	/**
   378  	 * Indicate whether need resize frame directly when drag the resizer arrow.
   379  	 * if set to false, there will be a rectange to represent then size what will be resized to.
   380  	 * if set to true, the frame will be resize directly when drag, but this is need more cpu counting.<br>
   381  	 * Default is false.
   382  	 * @see org.aswing.Resizer#setResizeDirectly()
   383  	 */
   384  	public function setResizeDirectly(b:Boolean):Void{
   385  		resizer.setResizeDirectly(b);
   386  	}
   387  	
   388  	/**
   389  	 * Return whether need resize frame directly when drag the resizer arrow.
   390  	 * @see #setResizeDirectly()
   391  	 */
   392  	public function isResizeDirectly():Boolean{
   393  		return resizer.isResizeDirectly();
   394  	}
   395  	
   396  	/**
   397  	 * Indicate whether need move frame directly when drag the frame.
   398  	 * if set to false, there will be a rectange to represent then bounds what will be move to.
   399  	 * if set to true, the frame will be move directly when drag, but this is need more cpu counting.<br>
   400  	 * Default is false.
   401  	 */	
   402  	public function setDragDirectly(b:Boolean):Void{
   403  		dragDirectly = b;
   404  	}
   405  	
   406  	/**
   407  	 * Return whether need move frame directly when drag the frame.
   408  	 * @see #setDragDirectly()
   409  	 */	
   410  	public function isDragDirectly():Boolean{
   411  		return dragDirectly;
   412  	}
   413  	
   414  	/**
   415  	 * Call this method tring to close the Frame depend on the <code>defaultCloseOperation</code>
   416  	 */
   417  	public function closeReleased():Void{
   418  		dispatchEvent(ON_WINDOW_CLOSING, createEventObj(ON_WINDOW_CLOSING));
   419  		if(defaultCloseOperation == HIDE_ON_CLOSE){
   420  			hide();
   421  		}else if(defaultCloseOperation == DISPOSE_ON_CLOSE){
   422  			dispose();
   423  		}
   424  	}
   425  	
   426  	/**
   427  	 * This is just for FrameUI
   428  	 */
   429  	public function createDragRepresentMC():MovieClip{
   430  		return creater.createMC(root_mc, "drag_mc");
   431  	}
   432  }
   433