/DeveloperBranches/Ken/Previous/TreeBuilder/ViewModels/TreeBuilderTreeNodeFolder.cs

# · C# · 300 lines · 255 code · 45 blank · 0 comment · 12 complexity · 3afe51a3b56ec981a977c81365415d43 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UIObjects.TreeViewInterfaces;
  6. using AbstraX.ClientInterfaces;
  7. using System.Collections.ObjectModel;
  8. using TreeBuilder.Contracts;
  9. using MvvmTreeView;
  10. using System.Windows.Controls;
  11. using System.Diagnostics;
  12. using SL4PopupMenu;
  13. using System.Windows;
  14. namespace TreeBuilder.ViewModels
  15. {
  16. public class TreeBuilderTreeNodeFolder : TreeBuilderTreeNodeBase
  17. {
  18. private ITreeNodeFolder folder;
  19. private bool allowDrop;
  20. public TreeBuilderTreeNodeFolder(ITreeBuilderService treeBuilderService, ITreeView treeView, ITreeNodeFolder folder, ITreeNode parent) : base(treeBuilderService, treeView, parent)
  21. {
  22. this.folder = folder;
  23. this.Tag = folder;
  24. this.allowDrop = true;
  25. }
  26. public override ITreeNodeBase InternalNodeObject
  27. {
  28. get
  29. {
  30. return folder;
  31. }
  32. }
  33. public override AbstraX.ClientInterfaces.IBase BaseObject
  34. {
  35. get
  36. {
  37. return folder.BaseObject;
  38. }
  39. set
  40. {
  41. folder.BaseObject = value;
  42. }
  43. }
  44. public override void Expand()
  45. {
  46. if (!this.IsChildrenLoaded)
  47. {
  48. folder.ChildrenLoaded += new TreeBuilder.Contracts.ChildrenLoadedHandler(OnChildrenLoaded);
  49. folder.LoadChildren();
  50. }
  51. base.Expand();
  52. }
  53. public override string Text
  54. {
  55. get
  56. {
  57. return folder.Text;
  58. }
  59. set
  60. {
  61. folder.Text = value;
  62. }
  63. }
  64. public void AddMemberAsNode(object sender, RoutedEventArgs r)
  65. {
  66. var rootElement = (IElement)folder.FolderItemSource;
  67. var menu = (PopupMenuItem)sender;
  68. if (rootElement.Attributes.Any(a => a.Name == (string)menu.Tag))
  69. {
  70. var attribute = rootElement.Attributes.Single(a => a.Name == (string)menu.Tag);
  71. var createOperation = treeBuilderService.CreateAttributeNode(folder, attribute);
  72. createOperation.NodeCreated += (parent, e) =>
  73. {
  74. this.Nodes.Add(new TreeBuilderTreeNodeAttributeNode(treeBuilderService, treeView, createOperation.NewNode, this) { ChildOrdinal = this.Nodes.Count });
  75. base.Expand();
  76. };
  77. }
  78. else if (rootElement.ChildElements.Any(e => e.Name == (string)menu.Tag))
  79. {
  80. var element = rootElement.ChildElements.Single(a => a.Name == (string)menu.Tag);
  81. var createOperation = treeBuilderService.CreateElementNode(folder, element);
  82. createOperation.NodeCreated += (parent, e) =>
  83. {
  84. this.Nodes.Add(new TreeBuilderTreeNodeElementNode(treeBuilderService, treeView, createOperation.NewNode, this) { ChildOrdinal = this.Nodes.Count });
  85. base.Expand();
  86. };
  87. }
  88. }
  89. private List<IContextMenuItem> GetSubMenus()
  90. {
  91. List<IContextMenuItem> subMenus = new List<IContextMenuItem>();
  92. if (folder.FolderItemSource is IElement)
  93. {
  94. IElement element = (IElement)folder.FolderItemSource;
  95. foreach (var childElement in element.ChildElements)
  96. {
  97. subMenus.Add(new ContextMenuItem(null, childElement.Name, childElement.Name, AddMemberAsNode));
  98. }
  99. foreach (var childElement in element.ChildElements)
  100. {
  101. subMenus.Add(new ContextMenuItem(null, childElement.Name, childElement.Name, AddMemberAsNode));
  102. }
  103. }
  104. return subMenus;
  105. }
  106. public void AddFolder(object sender, RoutedEventArgs r)
  107. {
  108. var createOperation = treeBuilderService.CreateFolderNode(folder, folder.FolderItemSource);
  109. createOperation.NodeCreated += (parent, e) =>
  110. {
  111. this.Nodes.Add(new TreeBuilderTreeNodeFolder(treeBuilderService, treeView, createOperation.NewNode, this) { ChildOrdinal = this.Nodes.Count });
  112. base.Expand();
  113. };
  114. }
  115. public override ContextMenu ContextMenu
  116. {
  117. get
  118. {
  119. if (contextMenu == null)
  120. {
  121. contextMenu = new ContextMenu
  122. {
  123. Items = new List<IContextMenuItem>()
  124. {
  125. new ContextMenuItem
  126. {
  127. Text = "Add Member as Node",
  128. Items = GetSubMenus(),
  129. },
  130. new ContextMenuItem(null, "Add Folder", null, AddFolder),
  131. new ContextMenuItem(null, "Add Query", null, AddQuery),
  132. new ContextMenuItem(null, "Add Advanced Query...", null, AddAdvancedQuery),
  133. }
  134. };
  135. }
  136. return contextMenu;
  137. }
  138. set
  139. {
  140. contextMenu = value;
  141. }
  142. }
  143. public override bool? HasChildren
  144. {
  145. get { throw new NotImplementedException(); }
  146. }
  147. public override string ID
  148. {
  149. get
  150. {
  151. return folder.ID;
  152. }
  153. }
  154. public override void LoadChildren()
  155. {
  156. folder.ChildrenLoaded += new TreeBuilder.Contracts.ChildrenLoadedHandler(OnChildrenLoaded);
  157. folder.LoadChildren();
  158. }
  159. public override IEnumerable<ITreeNodeBase> Children
  160. {
  161. get
  162. {
  163. return folder.Children;
  164. }
  165. }
  166. public override void Remove()
  167. {
  168. folder.RemovedSuccessfully += (node, e) =>
  169. {
  170. this.parent.Nodes.Remove(this);
  171. var op = treeBuilderService.OrphansExist();
  172. op.Completed += (sender, e2) =>
  173. {
  174. if (op.Value)
  175. {
  176. MessageBox.Show("Orphans exist!");
  177. }
  178. };
  179. };
  180. folder.Remove();
  181. }
  182. public override bool AllowDrop
  183. {
  184. get
  185. {
  186. return allowDrop;
  187. }
  188. set
  189. {
  190. allowDrop = value;
  191. }
  192. }
  193. public override ITreeNode ShallowClone()
  194. {
  195. throw new NotImplementedException();
  196. }
  197. public override void OnSelect()
  198. {
  199. }
  200. public override ITreeNode DeepClone()
  201. {
  202. throw new NotImplementedException();
  203. }
  204. public override bool AllowRename
  205. {
  206. get
  207. {
  208. throw new NotImplementedException();
  209. }
  210. set
  211. {
  212. throw new NotImplementedException();
  213. }
  214. }
  215. public override bool HasLayoverImage<T>(out T image)
  216. {
  217. return folder.HasLayoverImage<T>(out image);
  218. }
  219. public override void AddNew(string shortCut)
  220. {
  221. }
  222. public override void DragEnter(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
  223. {
  224. }
  225. public override void DragLeave(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
  226. {
  227. }
  228. public override void DragOver(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
  229. {
  230. }
  231. public override void DragStarting(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
  232. {
  233. }
  234. public override void DragCompleted(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
  235. {
  236. }
  237. public override void ItemDropped(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e, int insertionIndex)
  238. {
  239. }
  240. public override void GiveFeedback(object sender, UIObjects.DragDropInterfaces.GiveFeedbackEventArgs e)
  241. {
  242. }
  243. public override float ChildOrdinal
  244. {
  245. get
  246. {
  247. return folder.ChildOrdinal;
  248. }
  249. set
  250. {
  251. folder.ChildOrdinal = value;
  252. }
  253. }
  254. }
  255. }