Guest User

Untitled

a guest
May 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. package com.app.utils
  2. {
  3. import com.fake.controller.utils.ControllerUtil;
  4. import com.fake.utils.DescribeUtil;
  5.  
  6. import flash.display.DisplayObjectContainer;
  7.  
  8. import mx.controls.CheckBox;
  9. import mx.controls.ComboBox;
  10. import mx.controls.NumericStepper;
  11. import mx.controls.RadioButton;
  12. import mx.controls.TextInput;
  13. import mx.core.UIComponent;
  14.  
  15. public class ComponentSerializer
  16. {
  17. private static var _instance:ComponentSerializer = new ComponentSerializer();
  18.  
  19. public var defaultFindTypes:Array = [NumericStepper, CheckBox, RadioButton, TextInput, ComboBox];
  20.  
  21. public function ComponentSerializer()
  22. {
  23. }
  24.  
  25. public static function get instance():ComponentSerializer {
  26. return _instance;
  27. }
  28.  
  29. public function serialize(source:UIComponent, findTypes:Array = null):XML
  30. {
  31. if(!findTypes)
  32. findTypes = defaultFindTypes;
  33.  
  34. var foundComponents:Array = ControllerUtil.findByType(findTypes, source);
  35.  
  36. var output:XML = new XML("<"+ DescribeUtil.localName(source) +"/>");
  37.  
  38. for each(var component:Object in foundComponents)
  39. {
  40. if(!component.id || component.id.split("_").length == 0)
  41. continue;
  42.  
  43. var child:XML = serializeChild(component);
  44.  
  45. if(child.localName() != "null")
  46. output.appendChild(child);
  47. }
  48.  
  49. return output;
  50. }
  51.  
  52. public function serializeChild(component:Object):XML
  53. {
  54. var componentID:String = component.id;
  55.  
  56. var localName:String = componentID.split("_")[1];
  57. var prefix:String = componentID.split("_")[0];
  58.  
  59. var node:XML = new XML("<"+ localName + "/>");
  60.  
  61. node.@prefix = prefix
  62.  
  63. if(component is NumericStepper)
  64. node.@value = component.value;
  65. else if(component is CheckBox)
  66. node.@value = component.selected;
  67. else if(component is RadioButton)
  68. node.@value = component.selected;
  69. else if(component is TextInput)
  70. node.@value = component.text;
  71. else if(component is ComboBox)
  72. node.@value = component.selectedIndex;
  73.  
  74. return node;
  75. }
  76.  
  77. public function deserialize(source:DisplayObjectContainer, xml:XMLList):void
  78. {
  79. for each(var node:XML in xml.children())
  80. {
  81. var name:String = node.localName().toString();
  82. var prefix:String = node.@prefix;
  83.  
  84. var component:Object = ControllerUtil.findByID(prefix + "_" + name, source);
  85.  
  86. if(component is NumericStepper)
  87. component.value = Number(node.@value);
  88. else if(component is CheckBox)
  89. component.selected = (node.@value == "true");
  90. else if(component is RadioButton)
  91. component.selected = (node.@value == "true");
  92. else if(component is TextInput)
  93. component.text = String(node.@value);
  94. else if(component is ComboBox)
  95. component.selectedIndex = Number(node.@value);
  96. }
  97. }
  98. }
  99. }
Add Comment
Please, Sign In to add comment