1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  import org.aswing.ASColor;
     5  import org.aswing.Component;
     6  import org.aswing.geom.Point;
     7  import org.aswing.geom.Rectangle;
     8  import org.aswing.graphices.Graphics;
     9  import org.aswing.graphices.Pen;
    10  import org.aswing.JFrame;
    11  import org.aswing.LookAndFeel;
    12  import org.aswing.plaf.basic.BasicWindowUI;
    13  import org.aswing.plaf.basic.frame.FrameTitleBar;
    14  import org.aswing.plaf.UIResource;
    15  import org.aswing.Resizer;
    16  import org.aswing.UIManager;
    17  import org.aswing.utils.Delegate;
    18  import org.aswing.WindowLayout;
    19  
    20  /**
    21   *
    22   * @author iiley
    23   */
    24  class org.aswing.plaf.basic.BasicFrameUI extends BasicWindowUI {
    25  	
    26  	private var frame:JFrame;
    27  	private var titleBar:FrameTitleBar;
    28  	private var titleBarListener:Object;
    29  	private var frameListener:Object;
    30  	
    31  	private var resizeArrowColor:ASColor;
    32  	private var resizeArrowLightColor:ASColor;
    33  	private var resizeArrowDarkColor:ASColor;
    34  	
    35  	private var mouseMoveListener:Object;
    36  	private var boundsMC:MovieClip;
    37  	
    38  	public function BasicFrameUI() {
    39  		super();
    40  	}
    41  
    42      // Do not Shared UI object
    43      //public static function createInstance(c:Component):ComponentUI {
    44      //    return new BasicFrameUI();
    45      //}
    46  
    47      public function installUI(c:Component):Void {
    48          frame = JFrame(c);
    49          installDefaults();
    50  		installComponents();
    51  		installListeners();
    52      }
    53  
    54      private function installDefaults():Void {
    55      	super.installDefaults(frame);
    56      	var pp:String = "Frame.";
    57          LookAndFeel.installColorsAndFont(frame, pp + "background", pp + "foreground", pp + "font");
    58          LookAndFeel.installBorder(frame, pp + "border");
    59  	    resizeArrowColor = UIManager.getColor("Frame.resizeArrow");
    60  	    resizeArrowLightColor = UIManager.getColor("Frame.resizeArrowLight");
    61  	    resizeArrowDarkColor = UIManager.getColor("Frame.resizeArrowDark");
    62      }
    63      
    64      private function installComponents():Void {
    65      	titleBar = new FrameTitleBar(frame);
    66      	frame.insert(0, titleBar, WindowLayout.TITLE);
    67      	
    68      	if(frame.getResizer() == null || frame.getResizer() instanceof UIResource){
    69  	    	var resizer:Resizer = Resizer(UIManager.getInstance("Frame.resizer"));
    70  	    	frame.setResizer(resizer);
    71      	}
    72  	}
    73  	private function installListeners():Void{
    74  		var titleLis:Object = new Object();
    75  		titleLis[Component.ON_PRESS] = Delegate.create(this, __onTitleBarPress);
    76  		titleLis[Component.ON_RELEASE] = Delegate.create(this, __onTitleBarRelease);
    77  		titleLis[Component.ON_RELEASEOUTSIDE] = titleLis[Component.ON_RELEASE];
    78  		titleBarListener = titleLis;
    79  		titleBar.addEventListener(titleBarListener);
    80  		mouseMoveListener = new Object();
    81  		mouseMoveListener.onMouseMove = Delegate.create(this, __onMouseMove);
    82  		frame.addEventListener(JFrame.ON_WINDOW_ACTIVATED, frame.repaint, frame);
    83  		frame.addEventListener(JFrame.ON_WINDOW_DEACTIVATED, frame.repaint, frame);
    84  	}
    85  
    86      public function uninstallUI(c:Component):Void {
    87          var frame:JFrame = JFrame(c);
    88          uninstallDefaults();
    89  		uninstallComponents();
    90  		uninstallListeners();
    91  		boundsMC.unloadMovie();
    92  		boundsMC.removeMovieClip();
    93      }
    94      
    95      private function uninstallDefaults():Void {
    96          LookAndFeel.uninstallBorder(frame);
    97      }
    98  	private function uninstallComponents():Void{
    99  		titleBar.setUI(null); //uninstall its listeners
   100  		frame.remove(titleBar);
   101  	}
   102  	private function uninstallListeners():Void{
   103  		titleBar.removeEventListener(titleBarListener);
   104  	}
   105      
   106      public function create(c:Component):Void{
   107      	super.create(c);
   108      	boundsMC = frame.createDragRepresentMC();
   109      }
   110      
   111      //----------------------------------------------------------
   112  	
   113  	private function isMaximizedFrame():Boolean{
   114  		var state:Number = frame.getState();
   115  		return ((state & JFrame.MAXIMIZED_HORIZ) == JFrame.MAXIMIZED_HORIZ)
   116  				|| ((state & JFrame.MAXIMIZED_VERT) == JFrame.MAXIMIZED_VERT);
   117  	}
   118  	
   119  	private var startPos:Point;
   120      private function __onTitleBarPress():Void{
   121      	if(frame.isDragable() && !isMaximizedFrame()){
   122      		if(frame.isDragDirectly()){
   123      			frame.startDrag();
   124      		}else{
   125      			startPos = frame.getMousePosition();
   126      			Mouse.addListener(mouseMoveListener);
   127      		}
   128      	}
   129      }
   130      
   131      private function __onTitleBarRelease():Void{
   132      	frame.stopDrag();
   133      	Mouse.removeListener(mouseMoveListener);
   134      	if(frame.isDragable() && !isMaximizedFrame()){
   135  	    	var delta:Point = representMoveBounds();
   136  	    	boundsMC.clear();
   137  	    	boundsMC._visible = false;
   138  	    	var loc:Point = frame.getLocation();
   139  	    	frame.setLocation(loc.move(delta.x, delta.y));
   140  	    	frame.revalidate();
   141      	}
   142      }
   143      
   144      private function representMoveBounds():Point{
   145      	boundsMC._visible = true;
   146      	var currentPos:Point = frame.getMousePosition();
   147      	var bounds:Rectangle = frame.getBounds();
   148      	bounds.x = currentPos.x - startPos.x;
   149      	bounds.y = currentPos.y - startPos.y;
   150      	
   151      	//these make user can't drag frames out the stage
   152      	var gap:Number = titleBar.getHeight();
   153      	var frameMaxBounds:Rectangle = frame.getMaximizedBounds();
   154      	frameMaxBounds.x -= frame.getX();
   155      	frameMaxBounds.y -= frame.getY();
   156      	
   157      	var topLeft:Point = frameMaxBounds.leftTop();
   158      	var topRight:Point = frameMaxBounds.rightTop();
   159      	var bottomLeft:Point = frameMaxBounds.leftBottom();
   160      	if(bounds.x < topLeft.x - bounds.width + gap){
   161      		bounds.x = topLeft.x - bounds.width + gap;
   162      	}
   163      	if(bounds.x > topRight.x - gap){
   164      		bounds.x = topRight.x - gap;
   165      	}
   166      	if(bounds.y < topLeft.y){
   167      		bounds.y = topLeft.y;
   168      	}
   169      	if(bounds.y > bottomLeft.y - gap){
   170      		bounds.y = bottomLeft.y - gap;
   171      	}
   172      	
   173  		var x:Number = bounds.x;
   174  		var y:Number = bounds.y;
   175  		var w:Number = bounds.width;
   176  		var h:Number = bounds.height;
   177  		var g:Graphics = new Graphics(boundsMC);
   178  		boundsMC.clear();
   179  		g.drawRectangle(new Pen(resizeArrowLightColor, 1), x-1,y-1,w+2,h+2);
   180  		g.drawRectangle(new Pen(resizeArrowColor, 1), x,y,w,h);
   181  		g.drawRectangle(new Pen(resizeArrowDarkColor, 1), x+1,y+1,w-2,h-2);
   182  		updateAfterEvent();
   183  		return bounds.leftTop();
   184      }
   185      private function __onMouseMove():Void{
   186      	representMoveBounds();
   187      }
   188  }
   189