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

/ide/src/com/moonshineproject/plugin/project/view/FTETreeItemRenderer.as

http://moonshineproject.googlecode.com/
ActionScript | 411 lines | 331 code | 72 blank | 8 comment | 32 complexity | 18c0e535bb4259b060e813cff7ad5788 MD5 | raw file
Possible License(s): LGPL-3.0, MIT, Apache-2.0, GPL-3.0
  1. package com.moonshineproject.plugin.project.view
  2. {
  3. import com.moonshineproject.GlobalEventDispatcher;
  4. import com.moonshineproject.IDEModel;
  5. import com.moonshineproject.editor.BasicTextEditor;
  6. import com.moonshineproject.plugin.project.event.TreeMenuItemEvent;
  7. import com.moonshineproject.plugin.project.vo.FileWrapper;
  8. import com.moonshineproject.plugin.templating.TemplatingHelper;
  9. import com.moonshineproject.plugin.templating.event.RequestTemplatesEvent;
  10. import flash.display.NativeMenu;
  11. import flash.display.NativeMenuItem;
  12. import flash.display.Sprite;
  13. import flash.events.Event;
  14. import flash.events.FocusEvent;
  15. import flash.events.KeyboardEvent;
  16. import flash.filesystem.File;
  17. import flash.filters.GlowFilter;
  18. import flash.ui.ContextMenu;
  19. import flash.ui.Keyboard;
  20. import mx.binding.utils.ChangeWatcher;
  21. import mx.controls.treeClasses.TreeItemRenderer;
  22. import mx.core.UIComponent;
  23. import mx.core.mx_internal;
  24. import spark.components.Label;
  25. import spark.components.TextInput;
  26. use namespace mx_internal;
  27. public class FTETreeItemRenderer extends TreeItemRenderer
  28. {
  29. private var label2:Label;
  30. private var editText:TextInput;
  31. private var model:IDEModel;
  32. private var isOpenIcon:Sprite;
  33. private var hitareaSprite:Sprite;
  34. private var sourceControlBackground:UIComponent;
  35. private var sourceControlText:Label;
  36. private var sourceControlSystem:Label;
  37. public function FTETreeItemRenderer()
  38. {
  39. super();
  40. model = IDEModel.getInstance();
  41. ChangeWatcher.watch(model, 'activeEditor', onActiveEditorChange);
  42. }
  43. private function onActiveEditorChange(event:Event):void
  44. {
  45. invalidateDisplayList();
  46. }
  47. public function startEdit():void
  48. {
  49. label2.visible = false;
  50. editText = new TextInput();
  51. editText.x = label2.x;
  52. editText.y = -2;
  53. editText.width = width;
  54. editText.height = height+4;
  55. editText.styleName = 'uiText';
  56. editText.setStyle('fontSize', 12);
  57. editText.setStyle('focusAlpha', 0);
  58. editText.setStyle('color', 0xe0e0e0);
  59. editText.setStyle('paddingTop', 3);
  60. editText.setStyle('contentBackgroundAlpha', 0);
  61. editText.setStyle('focusedTextSelectionColor', 0x2f0727);
  62. editText.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
  63. editText.addEventListener(FocusEvent.FOCUS_OUT, handleFocusOut);
  64. editText.text = label2.text;
  65. addChild(editText);
  66. editText.setFocus();
  67. // Normally you don't want to change the file ending
  68. if (editText.text.indexOf(".") > -1)
  69. {
  70. editText.selectRange(0, editText.text.indexOf("."));
  71. }
  72. }
  73. public function stopEdit():void
  74. {
  75. editDone();
  76. }
  77. public function cancelEdit():void
  78. {
  79. if (!editText) return;
  80. label2.visible = true;
  81. editText.removeEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
  82. editText.removeEventListener(FocusEvent.FOCUS_OUT, handleFocusOut);
  83. removeChild(editText);
  84. editText = null;
  85. }
  86. protected function handleKeyDown(event:KeyboardEvent):void
  87. {
  88. if (event.charCode == Keyboard.ENTER)
  89. {
  90. editDone();
  91. }
  92. else if (event.charCode == Keyboard.ESCAPE)
  93. {
  94. editCancel();
  95. }
  96. }
  97. protected function handleFocusOut(event:Event):void
  98. {
  99. editDone();
  100. }
  101. protected function editDone():void
  102. {
  103. dispatchEvent( new TreeMenuItemEvent(TreeMenuItemEvent.EDIT_END, editText.text, FileWrapper(data)) );
  104. cancelEdit();
  105. data = data;
  106. }
  107. protected function editCancel():void
  108. {
  109. dispatchEvent( new TreeMenuItemEvent(TreeMenuItemEvent.EDIT_CANCEL, editText.text, FileWrapper(data)) );
  110. cancelEdit();
  111. }
  112. override public function set data(value:Object):void
  113. {
  114. super.data = value;
  115. var fw:FileWrapper = value as FileWrapper;
  116. if (fw)
  117. {
  118. contextMenu = new ContextMenu();
  119. var open:NativeMenuItem = new NativeMenuItem('Open');
  120. open.addEventListener(Event.SELECT, redispatch);
  121. contextMenu.addItem(open);
  122. var newMenu:NativeMenuItem = new NativeMenuItem('New');
  123. newMenu.submenu = new NativeMenu();
  124. contextMenu.addItem(newMenu);
  125. newMenu.addEventListener(Event.DISPLAYING, populateTemplatingMenu);
  126. if (fw.sourceController)
  127. {
  128. contextMenu.addItem( fw.sourceController.getTreeRightClickMenu(fw.file) );
  129. }
  130. if (!fw.isRoot)
  131. {
  132. var rename:NativeMenuItem = new NativeMenuItem('Rename');
  133. rename.addEventListener(Event.SELECT, redispatch);
  134. contextMenu.addItem(rename);
  135. contextMenu.addItem(new NativeMenuItem(null, true));
  136. var del:NativeMenuItem = new NativeMenuItem('Delete');
  137. del.addEventListener(Event.SELECT, redispatch);
  138. contextMenu.addItem(del)
  139. contextMenu.addItem(new NativeMenuItem(null, true));
  140. }
  141. var refresh:NativeMenuItem = new NativeMenuItem('Refresh');
  142. refresh.addEventListener(Event.SELECT, redispatch);
  143. contextMenu.addItem(refresh);
  144. if (fw.isRoot)
  145. {
  146. contextMenu.addItem(new NativeMenuItem(null, true));
  147. var settings:NativeMenuItem = new NativeMenuItem('Settings');
  148. settings.addEventListener(Event.SELECT, redispatch);
  149. contextMenu.addItem(settings);
  150. var close:NativeMenuItem = new NativeMenuItem('Close');
  151. close.addEventListener(Event.SELECT, redispatch);
  152. contextMenu.addItem(close);
  153. }
  154. //contextMenu.addEventListener(Event.SELECT, redispatch);
  155. }
  156. isOpenIcon.visible = false;
  157. }
  158. private function populateTemplatingMenu(e:Event):void
  159. {
  160. var menuItem:NativeMenuItem = e.target as NativeMenuItem;
  161. menuItem.submenu = new NativeMenu();
  162. var event:RequestTemplatesEvent = new RequestTemplatesEvent();
  163. GlobalEventDispatcher.getInstance().dispatchEvent(event);
  164. for each (var file:File in event.fileTemplates)
  165. {
  166. var label:String = TemplatingHelper.getTemplateLabel(file);
  167. var item:NativeMenuItem = new NativeMenuItem(label);
  168. item.data = file;
  169. item.addEventListener(Event.SELECT, redispatch);
  170. menuItem.submenu.addItem(item);
  171. }
  172. var folder:NativeMenuItem = new NativeMenuItem('Folder');
  173. folder.addEventListener(Event.SELECT, redispatch);
  174. menuItem.submenu.addItem(folder);
  175. }
  176. private function redispatch(event:Event):void
  177. {
  178. var type:String = NativeMenuItem(event.target).label;
  179. if (type == "New") return;
  180. var e:TreeMenuItemEvent = new TreeMenuItemEvent(TreeMenuItemEvent.RIGHT_CLICK_ITEM_SELECTED,
  181. type,
  182. FileWrapper(data));
  183. e.renderer = this;
  184. if (event.target.data)
  185. {
  186. e.menuLabel = "New File";
  187. e.extra = event.target.data;
  188. }
  189. dispatchEvent(e);
  190. }
  191. override protected function createChildren():void
  192. {
  193. super.createChildren();
  194. isOpenIcon = new Sprite();
  195. isOpenIcon.mouseEnabled = false;
  196. isOpenIcon.mouseChildren = false;
  197. isOpenIcon.graphics.clear();
  198. isOpenIcon.graphics.beginFill(0xe15fd5);
  199. isOpenIcon.graphics.drawCircle(1, 7, 2);
  200. isOpenIcon.graphics.endFill();
  201. isOpenIcon.visible = false;
  202. var glow:GlowFilter = new GlowFilter(0xff00e4, .4, 6, 6, 2);
  203. isOpenIcon.filters = [glow];
  204. addChild(isOpenIcon);
  205. sourceControlBackground = new UIComponent();
  206. sourceControlBackground.mouseEnabled = false;
  207. sourceControlBackground.mouseChildren = false;
  208. sourceControlBackground.visible = false;
  209. sourceControlBackground.graphics.beginFill(0x484848, .9);
  210. sourceControlBackground.graphics.drawRect(0, -2, 30, 17);
  211. sourceControlBackground.graphics.endFill();
  212. sourceControlBackground.graphics.lineStyle(1, 0x0, .3);
  213. sourceControlBackground.graphics.moveTo(-1, -2);
  214. sourceControlBackground.graphics.lineTo(-1, 16);
  215. sourceControlBackground.graphics.lineStyle(1, 0xEEEEEE, .1);
  216. sourceControlBackground.graphics.moveTo(0, -2);
  217. sourceControlBackground.graphics.lineTo(0, 16);
  218. addChild(sourceControlBackground);
  219. // For drawing SVN/GIT/HG/CVS etc
  220. sourceControlSystem = new Label();
  221. sourceControlSystem.width = 30;
  222. sourceControlSystem.height = 16;
  223. sourceControlSystem.mouseEnabled = false;
  224. sourceControlSystem.mouseChildren = false;
  225. sourceControlSystem.styleName = 'uiText';
  226. sourceControlSystem.setStyle('fontSize', 10);
  227. sourceControlSystem.setStyle('color', 0xe0e0e0);
  228. sourceControlSystem.setStyle('textAlign', 'center');
  229. sourceControlSystem.setStyle('paddingTop', 3);
  230. sourceControlSystem.maxDisplayedLines = 1;
  231. sourceControlSystem.visible = false;
  232. addChild(sourceControlSystem);
  233. // For displaying source control status
  234. sourceControlText = new Label();
  235. sourceControlText.width = 20;
  236. sourceControlText.height = 16;
  237. sourceControlText.mouseEnabled = false;
  238. sourceControlText.mouseChildren = false;
  239. sourceControlText.styleName = 'uiText';
  240. sourceControlText.setStyle('fontSize', 9);
  241. sourceControlText.setStyle('color', 0xcdcdcd);
  242. sourceControlText.setStyle('textAlign', 'center');
  243. sourceControlText.setStyle('paddingTop', 3);
  244. sourceControlText.maxDisplayedLines = 1;
  245. sourceControlText.visible = false;
  246. addChild(sourceControlText);
  247. hitareaSprite = new Sprite();
  248. hitArea = hitareaSprite;
  249. addChild(hitareaSprite);
  250. }
  251. override mx_internal function createLabel(childIndex:int):void
  252. {
  253. super.createLabel(childIndex);
  254. label.visible = false;
  255. if (!label2)
  256. {
  257. label2 = new Label();
  258. label2.mouseEnabled = false;
  259. label2.mouseChildren = false;
  260. label2.styleName = 'uiText';
  261. label2.setStyle('fontSize', 12);
  262. label2.setStyle('color', 0xe0e0e0);
  263. label2.maxDisplayedLines = 1;
  264. if (childIndex == -1)
  265. addChild(label2);
  266. else
  267. addChildAt(label2, childIndex);
  268. }
  269. }
  270. override mx_internal function removeLabel():void
  271. {
  272. super.removeLabel();
  273. if (label2 != null)
  274. {
  275. removeChild(label2);
  276. label2 = null;
  277. }
  278. }
  279. override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
  280. {
  281. super.updateDisplayList(unscaledWidth, unscaledHeight);
  282. hitareaSprite.graphics.clear();
  283. hitareaSprite.graphics.beginFill(0x0, 0);
  284. hitareaSprite.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
  285. hitareaSprite.graphics.endFill();
  286. hitArea = hitareaSprite;
  287. // Draw our own FTE label
  288. label2.width = label.width;
  289. label2.height = label.height;
  290. label2.x = label.x;
  291. label2.y = label.y+5;
  292. label2.text = label.text;
  293. if (label) label.visible = false;
  294. // Show lil' dot if we are the currently opened file
  295. if (model.activeEditor is BasicTextEditor
  296. && BasicTextEditor(model.activeEditor).currentFile)
  297. {
  298. if (data.nativePath
  299. && data.nativePath == BasicTextEditor(model.activeEditor).currentFile.nativePath)
  300. {
  301. isOpenIcon.visible = true;
  302. isOpenIcon.x = label2.x-8;
  303. }
  304. else isOpenIcon.visible = false;
  305. }
  306. else isOpenIcon.visible = false;
  307. // Update source control status
  308. sourceControlSystem.visible = false;
  309. sourceControlText.visible = false;
  310. sourceControlBackground.visible = false;
  311. if (data.sourceController)
  312. {
  313. if (data.isRoot)
  314. {
  315. // Show source control system name (SVN/CVS/HG/GIT)
  316. sourceControlSystem.text = FileWrapper(data).sourceController.systemNameShort;
  317. sourceControlBackground.visible = true;
  318. sourceControlSystem.visible = true;
  319. sourceControlBackground.x = unscaledWidth-30;
  320. sourceControlSystem.x = sourceControlBackground.x;
  321. sourceControlSystem.y = label2.y;
  322. }
  323. else
  324. {
  325. var st:String = data.sourceController.getStatus(data.nativePath);
  326. if (st)
  327. {
  328. sourceControlText.text = st;
  329. sourceControlBackground.visible = true;
  330. sourceControlText.visible = true;
  331. sourceControlBackground.x = unscaledWidth-20;
  332. sourceControlText.x = sourceControlBackground.x;
  333. sourceControlText.y = label2.y;
  334. }
  335. }
  336. }
  337. } // updateDisplayList
  338. }
  339. }