code_junkie

What is the best practice for changing view states

Nov 13th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. private function pieTypeToggleButtonBar_itemClickHandler():void
  2. {
  3. // Show/Hide the appropriate Pie Charts based on the user's selection
  4. switch (pieTypeToggleButtonBar.selectedIndex)
  5. {
  6. // "Start Value" is selected
  7. case 0:
  8. {
  9. // Hide the End Value Pie Chart
  10. endValuePieChartVBox.visible = false;
  11. endValuePieChartVBox.includeInLayout = false;
  12.  
  13. // Show the Start Value Pie Chart
  14. startValuePieChartVBox.includeInLayout = true;
  15. startValuePieChartVBox.visible = true;
  16.  
  17. break;
  18. }
  19. // "End Value" is selected
  20. case 1:
  21. {
  22. // Hide the Start Value Pie Chart
  23. startValuePieChartVBox.visible = false;
  24. startValuePieChartVBox.includeInLayout = false;
  25.  
  26. // Show the End Value Pie Chart
  27. endValuePieChartVBox.includeInLayout = true;
  28. endValuePieChartVBox.visible = true;
  29.  
  30. break;
  31. }
  32. // "Both" is selected
  33. case 2:
  34. {
  35. // Show the Start Value Pie Chart
  36. startValuePieChartVBox.includeInLayout = true;
  37. startValuePieChartVBox.visible = true;
  38.  
  39. // Show the End Value Pie Chart
  40. endValuePieChartVBox.includeInLayout = true;
  41. endValuePieChartVBox.visible = true;
  42.  
  43. break;
  44. }
  45. }
  46. }
  47.  
  48. <mx:ToggleButtonBar id="pieTypeToggleButtonBar" selectedIndex="1"
  49. itemClick="pieTypeToggleButtonBar_itemClickHandler()">
  50. <mx:Array>
  51. <mx:Object name="startValue" label="Start Value" />
  52.  
  53. <mx:Object name="endValue" label="End Value" />
  54.  
  55. <mx:Object name="both" label="Both" />
  56. </mx:Array>
  57. </mx:ToggleButtonBar>
  58.  
  59. <mx:states>
  60. <mx:State name="View State 1">
  61. <mx:SetProperty target="{component1}" name="visible" value="true" />
  62. <mx:SetProperty target="{component2}" name="visible" value="false" />
  63. <mx:SetProperty target="{component3}" name="visible" value="false" />
  64. <mx:SetProperty target="{component1}" name="includeInLayout" value="true" />
  65. <mx:SetProperty target="{component2}" name="includeInLayout" value="false" />
  66. <mx:SetProperty target="{component3}" name="includeInLayout" value="false" />
  67. </mx:State>
  68. <mx:State name="View State 2">
  69. <mx:SetProperty target="{component1}" name="visible" value="false" />
  70. <mx:SetProperty target="{component2}" name="visible" value="true" />
  71. <mx:SetProperty target="{component3}" name="visible" value="false" />
  72. <mx:SetProperty target="{component1}" name="includeInLayout" value="false" />
  73. <mx:SetProperty target="{component2}" name="includeInLayout" value="true" />
  74. <mx:SetProperty target="{component3}" name="includeInLayout" value="false" />
  75. </mx:State>
  76. <mx:State name="View State 3">
  77. <mx:SetProperty target="{component1}" name="visible" value="false" />
  78. <mx:SetProperty target="{component2}" name="visible" value="false" />
  79. <mx:SetProperty target="{component3}" name="visible" value="true" />
  80. <mx:SetProperty target="{component1}" name="includeInLayout" value="false" />
  81. <mx:SetProperty target="{component2}" name="includeInLayout" value="false" />
  82. <mx:SetProperty target="{component3}" name="includeInLayout" value="true" />
  83. </mx:State>
  84. </mx:states>
  85.  
  86. <mx:Script>
  87. <![CDATA[
  88.  
  89. import mx.binding.utils.BindingUtils;
  90. import mx.binding.utils.ChangeWatcher;
  91.  
  92. private function this_creationComplete(event:Event):void
  93. {
  94. // Use BindingUtils.bindSetter to hook into selectedIndex-change events
  95. var cw:ChangeWatcher = BindingUtils.bindSetter(setState, myBar, "selectedIndex");
  96. }
  97.  
  98. private function setState(index:int):void
  99. {
  100. currentState = myBar.getChildren()[index].label;
  101. }
  102.  
  103. ]]>
  104. </mx:Script>
  105.  
  106. <mx:ToggleButtonBar id="myBar">
  107. <mx:dataProvider>
  108. <mx:Array>
  109. <mx:String>View State 1</mx:String>
  110. <mx:String>View State 2</mx:String>
  111. <mx:String>View State 3</mx:String>
  112. </mx:Array>
  113. </mx:dataProvider>
  114. </mx:ToggleButtonBar>
  115.  
  116. <mx:Canvas id="component1">
  117. <mx:Text text="Component 1" />
  118. </mx:Canvas>
  119.  
  120. <mx:Canvas id="component2">
  121. <mx:Text text="Component 2" />
  122. </mx:Canvas>
  123.  
  124. <mx:Canvas id="component3">
  125. <mx:Text text="Component 3" />
  126. </mx:Canvas>
Add Comment
Please, Sign In to add comment