PageRenderTime 56ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cn/itamt/utils/inspector/ui/PropertiesViewPanel.as

http://tcodes.googlecode.com/
ActionScript | 366 lines | 265 code | 52 blank | 49 comment | 47 complexity | 310de00f2db31431772b1d9f246de6a1 MD5 | raw file
  1. package cn.itamt.utils.inspector.ui {
  2. import cn.itamt.utils.inspector.lang.InspectorLanguageManager;
  3. import cn.itamt.utils.ClassTool;
  4. import cn.itamt.utils.inspector.events.PropertyEvent;
  5. import cn.itamt.utils.inspector.renders.BasePropertyEditor;
  6. import cn.itamt.utils.inspector.renders.MethodRender;
  7. import cn.itamt.utils.inspector.renders.PropertyAccessorRender;
  8. import cn.itamt.utils.inspector.ui.InspectorViewPanel;
  9. import flash.display.DisplayObject;
  10. import flash.display.Sprite;
  11. import flash.events.MouseEvent;
  12. import flash.utils.describeType;
  13. /**
  14. * ????
  15. * @author itamt@qq.com
  16. */
  17. public class PropertiesViewPanel extends InspectorViewPanel {
  18. private var list : Sprite;
  19. private var listMethod : Sprite;
  20. private var methodArray : Array;
  21. private var curTarget : DisplayObject;
  22. private var propList : Array;
  23. private var singletonBtn : InspectorViewSingletonButton;
  24. private var fullBtn : InspectorViewFullButton;
  25. private var renders : Array;
  26. //?????
  27. private var favProps : Array = ["x", "y", "width", "height", "scaleX", "scaleY", "alpha", "rotation"];
  28. private var viewPropBtn : InspectorTabLabelButton;
  29. private var viewMethodBtn : InspectorTabLabelButton;
  30. //???????
  31. public static const PROP_STATE : int = 1;
  32. //???????
  33. public static const METHOD_STATE : int = 2;
  34. //????
  35. private var state : int = PROP_STATE;
  36. public function PropertiesViewPanel(w : Number = 240, h : Number = 170) {
  37. super('Property', w, h);
  38. _title.mouseEnabled = _title.mouseWheelEnabled = false;
  39. this._padding = new Padding(33, 10, 40, 10);
  40. renders = [];
  41. this._minW = 240;
  42. this._minH = 170;
  43. list = new Sprite();
  44. this.setContent(list);
  45. //??????.
  46. fullBtn = new InspectorViewFullButton();
  47. fullBtn.addEventListener(MouseEvent.CLICK, onClickFull);
  48. addChild(fullBtn);
  49. //??????/?????
  50. singletonBtn = new InspectorViewSingletonButton();
  51. singletonBtn.addEventListener(MouseEvent.CLICK, onClickSingleton);
  52. addChild(singletonBtn);
  53. //??"??"tab??
  54. this.viewPropBtn = new InspectorTabLabelButton(InspectorLanguageManager.getStr('Property'), true);
  55. this.viewPropBtn.tip = InspectorLanguageManager.getStr('ViewProperties');
  56. this.viewPropBtn.addEventListener(MouseEvent.CLICK, onViewProp);
  57. addChild(this.viewPropBtn);
  58. //??"??"tab??
  59. this.viewMethodBtn = new InspectorTabLabelButton(InspectorLanguageManager.getStr('Method'));
  60. this.viewMethodBtn.tip = InspectorLanguageManager.getStr('ViewMethods');
  61. this.viewMethodBtn.addEventListener(MouseEvent.CLICK, onViewMethod);
  62. addChild(this.viewMethodBtn);
  63. this.addEventListener(PropertyEvent.UPDATE, onPropertyUpdate);
  64. this.addEventListener(PropertyEvent.FAV, onFavProperty); this.addEventListener(PropertyEvent.DEL_FAV, onDelFavProperty);
  65. }
  66. //???????
  67. private function onViewMethod(event : MouseEvent) : void {
  68. this.state = METHOD_STATE;
  69. if(listMethod == null) {
  70. listMethod = new Sprite();
  71. }
  72. this.setContent(listMethod);
  73. this.onInspectMethod(this.curTarget);
  74. this.viewMethodBtn.active = true;
  75. this.viewPropBtn.active = false;
  76. }
  77. //???????
  78. private function onViewProp(event : MouseEvent) : void {
  79. this.state = PROP_STATE;
  80. this.setContent(list);
  81. this.onInspectProp(this.curTarget);
  82. this.viewMethodBtn.active = false; this.viewPropBtn.active = true;
  83. }
  84. override public function relayout() : void {
  85. super.relayout();
  86. fullBtn.x = this.resizeBtn.x - this.fullBtn.width;
  87. fullBtn.y = 5;
  88. singletonBtn.x = this.fullBtn.x - this.singletonBtn.width - 2;
  89. singletonBtn.y = 5;
  90. // this.viewPropBtn.x = this._padding.left;
  91. // this.viewMethodBtn.x = this.viewPropBtn.x + this.viewPropBtn.width + 10;
  92. this.viewMethodBtn.x = _width - this._padding.right - this.viewMethodBtn.width;
  93. this.viewPropBtn.x = this.viewMethodBtn.x - 10 - this.viewPropBtn.width;
  94. this.viewPropBtn.y = this.viewMethodBtn.y = this._height - this.viewPropBtn.height - (_padding.bottom - viewPropBtn.height) / 2;
  95. //
  96. if(this.state == PROP_STATE) {
  97. this.fullBtn.enabled = this.fullBtn.mouseEnabled = true;
  98. } else {
  99. this.fullBtn.enabled = this.fullBtn.mouseEnabled = false;
  100. }
  101. }
  102. /*
  103. * ?????????
  104. */
  105. public function onInspectProp(object : DisplayObject) : void {
  106. var xml : XML = describeType(object);
  107. var tmp : XMLList = xml.accessor;
  108. propList = [];
  109. for each(var item:XML in tmp) {
  110. propList.push(item);
  111. }
  112. // favProps.reverse();
  113. propList.sort(compateAccessorName);
  114. this.onClickFull();
  115. }
  116. /**
  117. * ???????????.
  118. */
  119. public function onInspect(object : DisplayObject) : void {
  120. if(curTarget != object) {
  121. curTarget = object;
  122. //????
  123. if(this.state == PROP_STATE) {
  124. this.onInspectProp(object);
  125. } else {
  126. //????
  127. this.onInspectMethod(curTarget);
  128. }
  129. //??title
  130. this._title.htmlText = curTarget.name + '<font color="#cccccc">(' + ClassTool.getShortClassName(curTarget) + ')</font>';
  131. this.drawTitle();
  132. } else {
  133. onUpdate(object);
  134. }
  135. }
  136. override protected function drawTitle() : void {
  137. _title.x = _padding.left;
  138. _title.y = 7;
  139. _title.width = _title.textWidth + 4;
  140. if(_title.width > singletonBtn.x - _padding.left)_title.width = singletonBtn.x - _padding.left;
  141. }
  142. /*
  143. * ???????????(??????????????????)
  144. */ public function onInspectMethod(object : DisplayObject = null) : void {
  145. if(object != null) {
  146. if(curTarget != object) {
  147. curTarget = object;
  148. }
  149. var xml : XML = describeType(curTarget);
  150. var methods : XMLList = xml.method;
  151. methodArray = [];
  152. for each(var method:XML in methods) {
  153. methodArray.push(method);
  154. }
  155. methodArray.sort(compateAccessorName);
  156. this.drawList();
  157. }
  158. }
  159. private function compateAccessorName(a : XML, b : XML) : Number {
  160. var aN : String = String(a.@name); var bN : String = String(b.@name);
  161. if(favProps.indexOf(aN) > favProps.indexOf(bN)) {
  162. return -1;
  163. } else if(favProps.indexOf(aN) < favProps.indexOf(bN)) {
  164. return 1;
  165. } else {
  166. if(aN > bN) {
  167. return 1;
  168. }else if(aN < bN) {
  169. return -1;
  170. } else {
  171. return 0;
  172. }
  173. }
  174. }
  175. /**
  176. * ????????????????.
  177. */
  178. private function onPropertyUpdate(evt : PropertyEvent) : void {
  179. var editor : BasePropertyEditor = evt.target as BasePropertyEditor;
  180. curTarget[editor.getPropName()] = editor.getValue();
  181. }
  182. /**
  183. * ????
  184. */
  185. private function onFavProperty(evt : PropertyEvent) : void {
  186. var render : PropertyAccessorRender = evt.target as PropertyAccessorRender;
  187. var prop : String = String(render.xml.@name);
  188. if(this.favProps.indexOf(prop) < 0) {
  189. this.favProps.unshift(prop);
  190. this.propList.sort(compateAccessorName);
  191. drawList();
  192. }
  193. }
  194. /**
  195. * ??????
  196. */
  197. private function onDelFavProperty(evt : PropertyEvent) : void {
  198. var render : PropertyAccessorRender = evt.target as PropertyAccessorRender;
  199. var prop : String = String(render.xml.@name);
  200. var t : int = this.favProps.indexOf(prop);
  201. if(t >= 0) {
  202. this.favProps.splice(t, 1);
  203. this.propList.sort(compateAccessorName);
  204. drawList();
  205. }
  206. }
  207. /**
  208. * ????????????
  209. */
  210. public function onUpdate(obj : DisplayObject) : void {
  211. if(obj == curTarget) {
  212. var i : int = 0;
  213. var render : PropertyAccessorRender;
  214. while(i < list.numChildren) {
  215. render = list.getChildAt(i++) as PropertyAccessorRender;
  216. render.update();
  217. }
  218. }
  219. }
  220. private function drawList() : void {
  221. switch(this.state) {
  222. case PROP_STATE:
  223. this.propDrawList();
  224. break;
  225. case METHOD_STATE:
  226. this.methodDrawList();
  227. break;
  228. }
  229. this.relayout();
  230. }
  231. //???????
  232. private function propDrawList() : void {
  233. list.graphics.clear();
  234. list.graphics.lineTo(0, 0);
  235. while(list.numChildren) {
  236. list.removeChildAt(0);
  237. }
  238. var l : int = propList.length;
  239. for(var i : int = 0;i < l; i++) { var render : PropertyAccessorRender;
  240. if(i < favProps.length) {
  241. if(this.favProps.indexOf(String(propList[i].@name)) > -1) {
  242. render = new PropertyAccessorRender(250, 20, true);
  243. } else {
  244. if(fullBtn.normalMode) {
  245. break;
  246. } else {
  247. render = new PropertyAccessorRender(250, 20);
  248. }
  249. }
  250. } else {
  251. if(fullBtn.normalMode) {
  252. break;
  253. } else {
  254. render = new PropertyAccessorRender(250, 20);
  255. }
  256. }
  257. render.setXML(this.curTarget, propList[i]);
  258. render.y = list.height + 2;
  259. list.addChild(render);
  260. }
  261. }
  262. //???????
  263. private function methodDrawList() : void {
  264. this.listMethod.graphics.clear();
  265. listMethod.graphics.lineTo(0, 0);
  266. while(listMethod.numChildren) {
  267. listMethod.removeChildAt(0);
  268. }
  269. var length : int = this.methodArray.length;
  270. for(var i : int = 0;i < length;i++) {
  271. var render : MethodRender = new MethodRender(210, 20);
  272. render.setXML(this.curTarget, methodArray[i]);
  273. render.y = listMethod.height + 2;
  274. listMethod.addChild(render);
  275. }
  276. }
  277. /**
  278. * ???"?????????".
  279. */
  280. private function onClickFull(evt : MouseEvent = null) : void {
  281. if(evt)evt.stopImmediatePropagation();
  282. this.drawList();
  283. if(fullBtn.normalMode) {
  284. if(this.resizeBtn.normalMode)this.resize(this._width, 270);
  285. } else {
  286. if(this.resizeBtn.normalMode)this.resize(this._width, 400);
  287. }
  288. }
  289. public function getSingleMode() : Boolean {
  290. return singletonBtn.normalMode;
  291. }
  292. /**
  293. * ??/??? ??.
  294. */
  295. private function onClickSingleton(evt : MouseEvent = null) : void {
  296. if(evt)evt.stopImmediatePropagation();
  297. this.drawList();
  298. if(singletonBtn.normalMode) {
  299. } else {
  300. }
  301. }
  302. public function getCurTarget() : DisplayObject {
  303. return this.curTarget;
  304. }
  305. override public function open() : void {
  306. super.open();
  307. this.viewMethodBtn.visible = this.viewPropBtn.visible = true;
  308. }
  309. override public function hide() : void {
  310. super.hide();
  311. this.viewMethodBtn.visible = this.viewPropBtn.visible = false;
  312. }
  313. }
  314. }