Guest User

Untitled

a guest
May 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. package com.app.screen
  2. {
  3. import com.fake.utils.TraceUtil;
  4. import com.fake.utils.actions.ActionDirector;
  5. import com.fake.utils.actions.PropertyChangeAction;
  6. import com.fake.utils.lso.LSObject;
  7.  
  8. import flash.events.MouseEvent;
  9.  
  10. import mx.containers.Canvas;
  11. import mx.containers.ViewStack;
  12. import mx.controls.Button;
  13. import mx.controls.LinkButton;
  14. import mx.controls.Text;
  15. import mx.core.Application;
  16. import mx.events.FlexEvent;
  17.  
  18. public class ScreenContainerCtrl extends Canvas
  19. {
  20. /**
  21. * All ScreenComponents are stored here
  22. */
  23. public var viewstack:ViewStack;
  24.  
  25. /**
  26. * Contains references to all ScreenComponents. Keyed by their label property.
  27. */
  28. public var screenMap:Object = {};
  29.  
  30. public var btn_next:Button;
  31. public var btn_back:Button;
  32. public var btn_summary:LinkButton;
  33.  
  34. public var txt_failure:Text;
  35.  
  36. public var dataStore:XML = <ScreenContainerCtrl/>;
  37.  
  38. public function ScreenContainerCtrl()
  39. {
  40. super();
  41.  
  42. addEventListener(FlexEvent.CREATION_COMPLETE, init);
  43. }
  44.  
  45. /**
  46. * Attaches the event handlers to the next, summary and back buttons.
  47. * Creates the screen map.
  48. */
  49. protected function init(event:FlexEvent):void
  50. {
  51. btn_next.addEventListener(MouseEvent.CLICK, onNextClick);
  52. btn_back.addEventListener(MouseEvent.CLICK, onBackClick);
  53. btn_summary.addEventListener(MouseEvent.CLICK, onSummaryClick);
  54.  
  55. createScreenMap();
  56.  
  57. TraceUtil.instance.codeBehindVariables();
  58.  
  59. currentScreen.onScreenEnter();
  60. }
  61.  
  62. /**
  63. * Each ScreenComponent has a corresponding abstract method
  64. * with the same name. It is called before the current screen
  65. * is changed. The ScreenContainer then grabs the nextScreen property
  66. * of the currentScreen and changes the current screen. The abstract
  67. * onScreenEnter method is called on the now current screen.
  68. *
  69. * @param event Click Event from the next button.
  70. */
  71. public function onNextClick(event:MouseEvent):void
  72. {
  73. failureText = currentScreen.validate();
  74.  
  75. if(failureText == "")
  76. {
  77. currentScreen.onNextClick();
  78. gotoScreen(currentScreen.nextScreen);
  79. }
  80. }
  81.  
  82. /**
  83. * The onNextClick method of the ScreenComponent is called before
  84. * the current screen is changed. The ScreenContainer then grabs
  85. * the summaryScreen property of the currentScreen and changes the
  86. * onScreenEnter method is called on the now current screen.
  87. *
  88. * @param event Click Event from the summary link button.
  89. */
  90. public function onSummaryClick(event:MouseEvent):void
  91. {
  92. failureText = currentScreen.validate();
  93.  
  94. if(failureText == "")
  95. {
  96. currentScreen.onNextClick();
  97. gotoScreen(currentScreen.summaryScreen);
  98. }
  99. }
  100.  
  101. /**
  102. * Each ScreenComponent has a corresponding abstract method
  103. * with the same name. It is called before the current screen
  104. * is changed. The currentScreen is then changed to the previous
  105. * screen. The onScreenEnter method is called on the now current screen.
  106. *
  107. * @param event Click Event from the back button.
  108. */
  109. public function onBackClick(event:MouseEvent):void
  110. {
  111. currentScreen.onBackClick();
  112.  
  113. ActionDirector.undoAction();
  114.  
  115. currentScreen.onScreenEnter(false);
  116. }
  117.  
  118. /**
  119. * Creates an undo/redo entry and changes the currentScreen. If clear is true
  120. * the onClear function is called on the new currentScreen. Then calls the
  121. * onScreenEnter function.
  122. *
  123. * @param screenLabel What screen should become the currentScreen.
  124. * @param clear Determines if the onClear function of the new currentScreen is called.
  125. *
  126. */
  127. public function gotoScreen(screenLabel:String, clear:Boolean = false):void
  128. {
  129. PropertyChangeAction.start("uid", ["currentScreenLabel"], this);
  130.  
  131. currentScreenLabel = screenLabel;
  132.  
  133. PropertyChangeAction.end();
  134.  
  135. if(clear)
  136. currentScreen.onClear();
  137.  
  138. currentScreen.onScreenEnter();
  139. }
  140.  
  141. /**
  142. * Retrieves the ScreenComponent by its label property.
  143. *
  144. * @param screenLabel Label property of the requested ScreenComponent.
  145. */
  146. public function getScreenByLabel(screenLabel:String):ScreenComponentCtrl
  147. {
  148. return screenMap[screenLabel];
  149. }
  150.  
  151. /**
  152. * Populates a map object with all ScreenComponents within the
  153. * viewstack property. The map is keyed by the ScreenComponent's
  154. * label property.
  155. */
  156. public function createScreenMap():void
  157. {
  158. for each(var screen:Object in viewstack.getChildren())
  159. {
  160. screenMap[screen.label] = screen;
  161. }
  162. }
  163.  
  164. /**
  165. * Returns the selectedChild of the viewstack. Read only.
  166. */
  167. public function get currentScreen():ScreenComponentCtrl
  168. {
  169. return viewstack.selectedChild as ScreenComponentCtrl;
  170. }
  171.  
  172. /**
  173. * Returns the label property of the current ScreenComponent.
  174. */
  175. public function get currentScreenLabel():String
  176. {
  177. return currentScreen.label;
  178. }
  179.  
  180. /**
  181. * Changes the selectedChild property of the viewstack.
  182. */
  183. public function set currentScreenLabel(screenLabel:String):void
  184. {
  185. viewstack.selectedChild = screenMap[screenLabel];
  186. }
  187.  
  188. public function save():void {
  189. LSObject.instance.dataStore = dataStore;
  190. }
  191.  
  192. public function load():void
  193. {
  194. if(!LSObject.instance.dataStore)
  195. reset();
  196.  
  197. dataStore = LSObject.instance.dataStore;
  198. }
  199.  
  200. public function reset():void {
  201. dataStore = <ScreenContainerCtrl/>;
  202. save();
  203. }
  204.  
  205. /**
  206. * Allows the screen container to retrieved like a singleton.
  207. */
  208. public static function get instance():ScreenContainerCtrl {
  209. return bef(Application.application).screenContainer as ScreenContainerCtrl;
  210. }
  211.  
  212. public function get failureText():String {
  213. return txt_failure.text;
  214. }
  215.  
  216. public function set failureText(value:String):void {
  217. txt_failure.text = value;
  218. }
  219. }
  220. }
Add Comment
Please, Sign In to add comment