Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.app.utils
- {
- import com.fake.controller.utils.ControllerUtil;
- import com.fake.utils.DescribeUtil;
- import flash.display.DisplayObjectContainer;
- import mx.controls.CheckBox;
- import mx.controls.ComboBox;
- import mx.controls.NumericStepper;
- import mx.controls.RadioButton;
- import mx.controls.TextInput;
- import mx.core.UIComponent;
- public class ComponentSerializer
- {
- private static var _instance:ComponentSerializer = new ComponentSerializer();
- public var defaultFindTypes:Array = [NumericStepper, CheckBox, RadioButton, TextInput, ComboBox];
- public function ComponentSerializer()
- {
- }
- public static function get instance():ComponentSerializer {
- return _instance;
- }
- public function serialize(source:UIComponent, findTypes:Array = null):XML
- {
- if(!findTypes)
- findTypes = defaultFindTypes;
- var foundComponents:Array = ControllerUtil.findByType(findTypes, source);
- var output:XML = new XML("<"+ DescribeUtil.localName(source) +"/>");
- for each(var component:Object in foundComponents)
- {
- if(!component.id || component.id.split("_").length == 0)
- continue;
- var child:XML = serializeChild(component);
- if(child.localName() != "null")
- output.appendChild(child);
- }
- return output;
- }
- public function serializeChild(component:Object):XML
- {
- var componentID:String = component.id;
- var localName:String = componentID.split("_")[1];
- var prefix:String = componentID.split("_")[0];
- var node:XML = new XML("<"+ localName + "/>");
- node.@prefix = prefix
- if(component is NumericStepper)
- node.@value = component.value;
- else if(component is CheckBox)
- node.@value = component.selected;
- else if(component is RadioButton)
- node.@value = component.selected;
- else if(component is TextInput)
- node.@value = component.text;
- else if(component is ComboBox)
- node.@value = component.selectedIndex;
- return node;
- }
- public function deserialize(source:DisplayObjectContainer, xml:XMLList):void
- {
- for each(var node:XML in xml.children())
- {
- var name:String = node.localName().toString();
- var prefix:String = node.@prefix;
- var component:Object = ControllerUtil.findByID(prefix + "_" + name, source);
- if(component is NumericStepper)
- component.value = Number(node.@value);
- else if(component is CheckBox)
- component.selected = (node.@value == "true");
- else if(component is RadioButton)
- component.selected = (node.@value == "true");
- else if(component is TextInput)
- component.text = String(node.@value);
- else if(component is ComboBox)
- component.selectedIndex = Number(node.@value);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment