PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/OfflineClient-RSS/GuidanceExplorerNT/GuidanceExplorer/Controls/LibraryNode.cs

#
C# | 313 lines | 225 code | 35 blank | 53 comment | 39 complexity | 8a7f36574176e378699a073a663c49cd MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices
  3. // Guidance Explorer Offline Client
  4. //===============================================================================
  5. // Copyright Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Text;
  14. using System.Windows.Forms;
  15. using GuidanceExplorer.Model;
  16. namespace GuidanceExplorer.Controls
  17. {
  18. /// <summary>
  19. /// The node for a single library.
  20. /// </summary>
  21. [MenuItemAttribute(10, "&File\\&New Item", GuidanceExplorer.Commands.NewGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), typeof(NewItemMenuItemProvider), Keys.None)]
  22. [MenuItemAttribute(11, "&File\\New &Folder", GuidanceExplorer.Commands.NewFolder, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  23. [MenuItemAttribute(12, "&File\\New &View", GuidanceExplorer.Commands.NewView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  24. [MenuItemAttribute(13, "&File\\New Search Vie&w", GuidanceExplorer.Commands.NewSearchView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  25. [MenuItemAttribute(20, "&File\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  26. [MenuItemAttribute(30, "&File\\&Import Item...", GuidanceExplorer.Commands.ImportGuidanceItemToLibrary, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  27. [MenuItemAttribute(40, "&File\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  28. [MenuItemAttribute(50, "&File\\Save &Item as Xml...", "Save Items as Xml...", GuidanceExplorer.Commands.SaveItemAsXml, Keys.None)]
  29. [MenuItemAttribute(60, "&File\\-", null, Keys.None)]
  30. [MenuItemAttribute(70, "&File\\&Save Library as...", GuidanceExplorer.Commands.SaveLibraryAs, Keys.None)]
  31. [MenuItemAttribute(80, "&File\\Save Item &as...", "Save Items as...", GuidanceExplorer.Commands.SaveItemAs, Keys.None)]
  32. [MenuItemAttribute(90, "&File\\-", null, Keys.None)]
  33. [MenuItemAttribute(94, "&File\\Suscribe to &Library\\From &File...", GuidanceExplorer.Commands.SuscribeLibraryFromFile, Keys.None)]
  34. [MenuItemAttribute(95, "&File\\Suscribe to &Library\\From &Web...", GuidanceExplorer.Commands.SuscribeLibraryFromWeb, Keys.None)]
  35. [MenuItemAttribute(96, "&File\\Suscribe to &Library\\From &Rss...", GuidanceExplorer.Commands.SuscribeLibraryFromRss, Keys.None)]
  36. [MenuItemAttribute(97, "&File\\S&ynchronize Libraries", Commands.Synchronize, Keys.None)]
  37. [MenuItemAttribute(98, "&File\\-", null, Keys.None)]
  38. [MenuItemAttribute(100, "&File\\&Rename Library", GuidanceExplorer.Commands.RenameLibrary, Keys.None)]
  39. [MenuItemAttribute(110, "&File\\&Delete Library", GuidanceExplorer.Commands.DeleteLibrary, Keys.None)]
  40. [MenuItemAttribute(120, "&File\\-", null, Keys.None)]
  41. [MenuItemAttribute(128, "&File\\Add/Remove &Guidance Type...", GuidanceExplorer.Commands.AddRemoveGuidanceType, Keys.None)]
  42. [MenuItemAttribute(129, "&File\\-", null, Keys.None)]
  43. [MenuItemAttribute(130, "&File\\&Exit", GuidanceExplorer.Commands.FileExit, Keys.None)]
  44. [MenuItemAttribute(200, "&Edit\\&Undo drag/drop", GuidanceExplorer.Commands.UndoDragAndDrop, Keys.Control | Keys.Z)]
  45. [MenuItemAttribute(210, "&Edit\\-", null, Keys.None)]
  46. [MenuItemAttribute(230, "&Edit\\&Add To View...", GuidanceExplorer.Commands.MoveGuidanceItemToView, Keys.None)]
  47. [MenuItemAttribute(250, "&Edit\\Dele&te item", GuidanceExplorer.Commands.DeleteGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  48. [MenuItemAttribute(260, "&Edit\\-", null, Keys.None)]
  49. [MenuItemAttribute(270, "&Edit\\&Select All", GuidanceExplorer.Commands.SelectAll, Keys.Control | Keys.A)]
  50. [MenuItemAttribute(300, "&View\\Reset &Sort Order", GuidanceExplorer.Commands.ResetSortOrder, Keys.None)]
  51. [MenuItemAttribute(310, "&View\\&Reset Columns", GuidanceExplorer.Commands.ResetColumns, Keys.None)]
  52. [MenuItemAttribute(320, "&View\\Reset &Filters", GuidanceExplorer.Commands.ResetFilters, Keys.None)]
  53. [MenuItemAttribute(330, "&View\\&Column Chooser", GuidanceExplorer.Commands.ColumnChooser, Keys.None)]
  54. [MenuItemAttribute(400, "&Help\\&About", GuidanceExplorer.Commands.HelpAbout, Keys.None)]
  55. [MenuItemAttribute(500, "context\\New Item", GuidanceExplorer.Commands.NewGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), typeof(NewItemMenuItemProvider), Keys.None)]
  56. [MenuItemAttribute(501, "context\\New Folder", GuidanceExplorer.Commands.NewFolder, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  57. [MenuItemAttribute(502, "context\\New View", GuidanceExplorer.Commands.NewView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  58. [MenuItemAttribute(503, "context\\New Search View", GuidanceExplorer.Commands.NewSearchView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  59. [MenuItemAttribute(505, "context\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  60. [MenuItemAttribute(510, "context\\Save Library As...", GuidanceExplorer.Commands.SaveLibraryAs, Keys.None)]
  61. [MenuItemAttribute(515, "context\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  62. [MenuItemAttribute(520, "context\\Import Item...", GuidanceExplorer.Commands.ImportGuidanceItemToLibrary, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  63. [MenuItemAttribute(525, "context\\-", null, Keys.None)]
  64. [MenuItemAttribute(530, "context\\Export Library...", GuidanceExplorer.Commands.ExportLibrary, Keys.None)]
  65. [MenuItemAttribute(535, "context\\-", null, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
  66. [MenuItemAttribute(540, "context\\Rename Library", GuidanceExplorer.Commands.RenameLibrary, Keys.None)]
  67. [MenuItemAttribute(550, "context\\Delete Library", GuidanceExplorer.Commands.DeleteLibrary, Keys.None)]
  68. [MenuItemAttribute(560, "context\\-", null, typeof(WebSubscribedLibraryCondition), Keys.None)]
  69. [MenuItemAttribute(570, "context\\Synchronize Library", GuidanceExplorer.Commands.Synchronize, typeof(WebSubscribedLibraryCondition), Keys.None)]
  70. [MenuItemAttribute(580, "context\\Refresh Content", GuidanceExplorer.Commands.RefreshFileSuscribedLibrary, typeof(IsFileSuscribedLibraryCondition), Keys.None)]
  71. #if ONLINEENABLED
  72. [MenuItemAttribute(590, "context\\Publish (experimental)...", GuidanceExplorer.Commands.PublishLibrary, Keys.None)]
  73. #endif
  74. public class LibraryNode : ModelNode<Library>
  75. {
  76. private Dictionary<string, GuidanceTypeNode> _guidanceTypeNodes = new Dictionary<string, GuidanceTypeNode>();
  77. /// <summary>
  78. /// Factory.
  79. /// </summary>
  80. /// <returns></returns>
  81. public static LibraryNode New()
  82. {
  83. return new LibraryNode();
  84. }
  85. /// <summary>
  86. /// Default constructor.
  87. /// </summary>
  88. /// <param name="writeProtection">Defines the write protection of the library.</param>
  89. private LibraryNode()
  90. : base("<a library>", 2, 2)
  91. {
  92. }
  93. /// <summary>
  94. /// The Library instance.
  95. /// </summary>
  96. public override Library ModelInstance
  97. {
  98. set
  99. {
  100. base.ModelInstance = value;
  101. if (value.FileSuscribed || value.WebSuscribed)
  102. {
  103. if (value.ReadProtection == ReadProtection.ReadOnly)
  104. {
  105. this.SelectedImageIndex = this.ImageIndex = 7;
  106. }
  107. else
  108. {
  109. this.SelectedImageIndex = this.ImageIndex = 6;
  110. }
  111. }
  112. else
  113. {
  114. if (value.ReadProtection == ReadProtection.ReadOnly)
  115. {
  116. this.SelectedImageIndex = this.ImageIndex = 3;
  117. }
  118. else
  119. {
  120. this.SelectedImageIndex = this.ImageIndex = 2;
  121. }
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Add a new GuidanceType node to the LibraryNode
  127. /// </summary>
  128. /// <param name="typeNode"></param>
  129. public void AddGuidanceTypeNode(GuidanceTypeNode typeNode)
  130. {
  131. string typeKey;
  132. if (string.IsNullOrEmpty(typeNode.ModelInstance.NodeDisplayName))
  133. {
  134. typeKey = typeNode.ModelInstance.Name;
  135. }
  136. else
  137. {
  138. typeKey = typeNode.ModelInstance.NodeDisplayName;
  139. typeNode.Text = typeKey;
  140. }
  141. _guidanceTypeNodes[typeKey] = typeNode;
  142. int i = 0;
  143. if (this.ModelInstance.ReadProtection == ReadProtection.ReadWrite)
  144. {
  145. for (i = 0; i < Nodes.Count; i++)
  146. {
  147. if (string.Compare(typeNode.Text, Nodes[i].Text) < 0 || !(Nodes[i] is GuidanceTypeNode))
  148. {
  149. break;
  150. }
  151. }
  152. }
  153. else
  154. {
  155. while (i < Nodes.Count && !(Nodes[i] is GuidanceTypeNode))
  156. {
  157. i++;
  158. }
  159. for (; i < Nodes.Count; i++)
  160. {
  161. if (string.Compare(typeNode.Text, Nodes[i].Text) < 0 || !(Nodes[i] is GuidanceTypeNode))
  162. {
  163. break;
  164. }
  165. }
  166. }
  167. Nodes.Insert(i, typeNode);
  168. }
  169. /// <summary>
  170. /// Removes the GuidanceType node for the specifies GuidanceType
  171. /// </summary>
  172. /// <param name="guidanceType"></param>
  173. public void RemoveGuidanceTypeNode(GuidanceType guidanceType)
  174. {
  175. string typeKey;
  176. if (string.IsNullOrEmpty(guidanceType.NodeDisplayName))
  177. {
  178. typeKey = guidanceType.Name;
  179. }
  180. else
  181. {
  182. typeKey = guidanceType.NodeDisplayName;
  183. }
  184. if (_guidanceTypeNodes.ContainsKey(typeKey))
  185. {
  186. Nodes.Remove(_guidanceTypeNodes[typeKey]);
  187. _guidanceTypeNodes[typeKey] = null;
  188. }
  189. }
  190. /// <summary>
  191. /// Return whether the LibraryNode contains a GuidanceType node for the specified GuidanceType
  192. /// </summary>
  193. /// <param name="guidanceType"></param>
  194. /// <returns></returns>
  195. public bool HasGuidanceTypeNode(GuidanceType guidanceType)
  196. {
  197. string typeKey;
  198. if (String.IsNullOrEmpty(guidanceType.NodeDisplayName))
  199. {
  200. typeKey = guidanceType.Name;
  201. }
  202. else
  203. {
  204. typeKey = guidanceType.NodeDisplayName;
  205. }
  206. return _guidanceTypeNodes.ContainsKey(typeKey) && _guidanceTypeNodes[typeKey] != null;
  207. }
  208. /// <summary>
  209. /// Whether the node is editable. ReadOnly libraries are not editable.
  210. /// </summary>
  211. public override bool IsEditable
  212. {
  213. get { return (ModelInstance.ReadProtection == ReadProtection.ReadWrite); }
  214. }
  215. public override bool CanDrop(IModelItem modelNode)
  216. {
  217. //If the library is read only, then all operations are cancelled.
  218. if (ModelInstance.ReadProtection == ReadProtection.ReadOnly)
  219. {
  220. return false;
  221. }
  222. ViewNode viewNode = modelNode as ViewNode;
  223. FolderNode folderNode = modelNode as FolderNode;
  224. SearchViewNode searchNode = modelNode as SearchViewNode;
  225. if (viewNode != null)
  226. {
  227. /*
  228. * Test for views:
  229. * Only views of the same library can be drop in the library.
  230. * The root views can't be droped in the library.
  231. */
  232. Library otherLibrary = viewNode.ModelInstance.GetContainerLibrary();
  233. //Root views cant be dragged
  234. if (viewNode.ModelInstance.RootView)
  235. {
  236. return false;
  237. }
  238. if (!Object.ReferenceEquals(otherLibrary, ModelInstance))
  239. {
  240. return false;
  241. }
  242. if (Object.ReferenceEquals(viewNode.ModelInstance.Container, ModelInstance))
  243. {
  244. return false;
  245. }
  246. return true;
  247. }
  248. else if (folderNode != null)
  249. {
  250. /*
  251. * Test for folders:
  252. * Only folders of the same library can be drop in the library.
  253. * The root folders can't be droped in the library.
  254. */
  255. Library otherLibrary = folderNode.ModelInstance.GetContainerLibrary();
  256. if (!Object.ReferenceEquals(otherLibrary, ModelInstance))
  257. {
  258. return false;
  259. }
  260. if (Object.ReferenceEquals(folderNode.ModelInstance.Container, ModelInstance))
  261. {
  262. return false;
  263. }
  264. return true;
  265. }
  266. else if (searchNode != null)
  267. {
  268. //Root views cant be dragged
  269. if (searchNode.ModelInstance.RootView)
  270. {
  271. return false;
  272. }
  273. return true;
  274. }
  275. return false;
  276. }
  277. }
  278. }