/* CASA Framework for ActionScript 3.0 Copyright (c) 2008, Contributors of CASA Framework All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the CASA Framework nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.casaframework.util { import flash.display.Shape; import flash.display.Stage; import flash.events.Event; /** Stores a reference to Stage for classes that cannot easily access it. @author Aaron Clinger @version 04/12/08 @usageNote You must first initialize the class by adding an instace to the display list or passing setting a refrence to Stage. See example below: @example You can initalize StageReference in two ways. The first way is adding the instance to the display list: (It can be removed from the display list immediately after being added) package { import flash.display.MovieClip; import org.casaframework.util.StageReference; public class MyExample extends MovieClip { public function MyExample() { super(); this.addChild(StageReference.getInstance()); this.removeChild(StageReference.getInstance()); trace(StageReference.getInstance().stage.stageWidth); } } } The second way is by setting the {@link #stage} property directly: package { import flash.display.MovieClip; import org.casaframework.util.StageReference; public class MyExample extends MovieClip { public function MyExample() { super(); StageReference.getInstance().stage = this.stage; trace(StageReference.getInstance().stage.stageWidth); } } } */ public class StageReference extends Shape { protected static var _instance:StageReference; protected var _stage:Stage; /** @return StageReference instance. */ public static function getInstance():StageReference { if (StageReference._instance == null) StageReference._instance = new StageReference(new SingletonEnforcer()); return StageReference._instance; } /** @exclude */ public function StageReference(singletonEnforcer:SingletonEnforcer) { this.addEventListener(Event.ADDED_TO_STAGE, this._onAddToStage, false, 0, true); } /** The Stage object. @usageNote You must first initialize the class by adding an instace to the display list or passing setting a refrence to Stage. See class documentation for more details. @throws Error if you access this property before a Stage reference has been stored. */ override public function get stage():Stage { if (this._stage == null) throw new Error('A reference to Stage needs to be set before getting this property.'); return this._stage; } public function set stage(s:Stage):void { this._stage = s; this.removeEventListener(Event.ADDED_TO_STAGE, this._onAddToStage); } protected function _onAddToStage(e:Event):void { this.stage = super.stage; } } } class SingletonEnforcer {}