PageRenderTime 64ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/extras/MonoDevelop.AddinAuthoring/MonoDevelop.AddinAuthoring.NodeBuilders/ExtensionNodeNodeBuilder.cs

https://github.com/jfcantin/monodevelop
C# | 316 lines | 282 code | 8 blank | 26 comment | 1 complexity | c94a48c23ddcf56cf60b2ef9abb32a4a MD5 | raw file
  1. // ExtensionPointsNodeBuilder.cs
  2. //
  3. // Author:
  4. // Lluis Sanchez Gual <lluis@novell.com>
  5. //
  6. // Copyright (c) 2007 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. //
  27. using System;
  28. using MonoDevelop.Projects;
  29. using MonoDevelop.Core;
  30. using MonoDevelop.Ide;
  31. using MonoDevelop.Ide.Gui;
  32. using MonoDevelop.Ide.Gui.Pads;
  33. using MonoDevelop.Ide.Gui.Pads.ProjectPad;
  34. using MonoDevelop.Components.Commands;
  35. using Mono.Addins;
  36. using Mono.Addins.Description;
  37. using MonoDevelop.Ide.Gui.Components;
  38. using Gdk;
  39. using System.Text;
  40. namespace MonoDevelop.AddinAuthoring.NodeBuilders
  41. {
  42. public class ExtensionNodeNodeBuilder: ExtensionModelTypeNodeBuilder
  43. {
  44. public override Type NodeDataType {
  45. get { return typeof(ExtensionNodeInfo); }
  46. }
  47. public override Type CommandHandlerType {
  48. get { return typeof(ExtensionNodeCommandHandler); }
  49. }
  50. public override string ContextMenuAddinPath {
  51. get { return "/MonoDevelop/AddinAuthoring/ContextMenu/ExtensionModelPad/ExtensionNode"; }
  52. }
  53. public override string GetNodeName (ITreeNavigator thisNode, object dataObject)
  54. {
  55. ExtensionNodeInfo ep = (ExtensionNodeInfo) dataObject;
  56. return ep.Node.Id;
  57. }
  58. public override void BuildNode (ITreeBuilder treeBuilder, object dataObject, ref string label, ref Pixbuf icon, ref Pixbuf closedIcon)
  59. {
  60. ExtensionNodeInfo ninfo = (ExtensionNodeInfo) dataObject;
  61. ExtensionNodeDescription node = ninfo.Node;
  62. label = GLib.Markup.EscapeText (node.NodeName);
  63. StringBuilder desc = new StringBuilder ();
  64. foreach (NodeAttribute at in node.Attributes) {
  65. if (desc.Length > 0)
  66. desc.Append (" ");
  67. desc.Append (at.Name).Append ("=\"").Append (GLib.Markup.EscapeText (at.Value)).Append ('"');
  68. }
  69. if (desc.Length > 0)
  70. label += "(<i>" + desc + "</i>)";
  71. icon = Context.GetIcon ("md-extension-node");
  72. if (treeBuilder.Options ["ShowExistingNodes"] && !ninfo.CanModify) {
  73. Gdk.Pixbuf gicon = Context.GetComposedIcon (icon, "fade");
  74. if (gicon == null) {
  75. gicon = ImageService.MakeTransparent (icon, 0.5);
  76. Context.CacheComposedIcon (icon, "fade", gicon);
  77. }
  78. icon = gicon;
  79. }
  80. }
  81. public override void BuildChildNodes (ITreeBuilder treeBuilder, object dataObject)
  82. {
  83. ExtensionNodeInfo en = (ExtensionNodeInfo) dataObject;
  84. foreach (ExtensionNodeInfo child in en.Expand ())
  85. treeBuilder.AddChild (child);
  86. }
  87. public override bool HasChildNodes (ITreeBuilder builder, object dataObject)
  88. {
  89. ExtensionNodeInfo en = (ExtensionNodeInfo) dataObject;
  90. return en.HasChildren;
  91. }
  92. public override int CompareObjects (ITreeNavigator thisNode, ITreeNavigator otherNode)
  93. {
  94. ExtensionNodeInfo n1 = thisNode.DataItem as ExtensionNodeInfo;
  95. ExtensionNodeInfo n2 = otherNode.DataItem as ExtensionNodeInfo;
  96. if (n1 != null && n2 != null)
  97. return n1.Order.CompareTo (n2.Order);
  98. else
  99. return base.CompareObjects (thisNode, otherNode);
  100. }
  101. public override void OnNodeAdded (object dataObject)
  102. {
  103. ExtensionNodeInfo en = (ExtensionNodeInfo) dataObject;
  104. en.Changed += HandleNodeChanged;
  105. }
  106. public override void OnNodeRemoved (object dataObject)
  107. {
  108. ExtensionNodeInfo en = (ExtensionNodeInfo) dataObject;
  109. en.Changed -= HandleNodeChanged;
  110. }
  111. void HandleNodeChanged (object sender, EventArgs e)
  112. {
  113. ITreeBuilder builder = Context.GetTreeBuilder (sender);
  114. if (builder != null)
  115. builder.UpdateAll ();
  116. }
  117. }
  118. class ExtensionNodeCommandHandler: NodeCommandHandler
  119. {
  120. public override DragOperation CanDragNode ()
  121. {
  122. ExtensionNodeInfo en = (ExtensionNodeInfo) CurrentNode.DataItem;
  123. if (en.CanModify)
  124. return DragOperation.Copy | DragOperation.Move;
  125. else
  126. return DragOperation.None;
  127. }
  128. public override bool CanDropNode (object dataObject, DragOperation operation, DropPosition pos)
  129. {
  130. object parent1 = CurrentNode.GetParentDataItem (typeof(Extension), false);
  131. if (parent1 == null)
  132. parent1 = CurrentNode.GetParentDataItem (typeof(ExtensionPoint), false);
  133. ITreeNavigator nav = CurrentNode.Clone ();
  134. if (!nav.MoveToObject (dataObject))
  135. return false;
  136. object parent2 = nav.GetParentDataItem (parent1.GetType (), false);
  137. if (parent2 != parent1)
  138. return false;
  139. return true;
  140. }
  141. public override void OnMultipleNodeDrop (object[] dataObjects, DragOperation operation, DropPosition pos)
  142. {
  143. DotNetProject p = (DotNetProject) CurrentNode.GetParentDataItem (typeof(Project), false);
  144. AddinData adata = p.GetAddinData ();
  145. ExtensionNodeInfo en = (ExtensionNodeInfo) CurrentNode.DataItem;
  146. foreach (ExtensionNodeInfo newNode in dataObjects) {
  147. if (newNode.Node.Parent is ExtensionNodeDescription)
  148. ((ExtensionNodeDescription)newNode.Node.Parent).ChildNodes.Remove (en.Node);
  149. else
  150. ((Extension)newNode.Node.Parent).ExtensionNodes.Remove (newNode.Node);
  151. InsertNode (adata, en, pos, newNode.Node);
  152. // Add all other nodes after the first node
  153. en = newNode;
  154. pos = DropPosition.After;
  155. }
  156. adata.CachedAddinManifest.Save ();
  157. adata.NotifyChanged (false);
  158. }
  159. [CommandUpdateHandler (Commands.AddNodeAfter)]
  160. public void UpdateAddNodeAfter (CommandArrayInfo cinfo)
  161. {
  162. foreach (ExtensionNodeType ntype in GetAllowedChildTypes ()) {
  163. cinfo.Add (ntype.NodeName, ntype);
  164. }
  165. }
  166. [CommandHandler (Commands.AddNodeAfter)]
  167. public void AddNodeAfter (object data)
  168. {
  169. DotNetProject p = (DotNetProject) CurrentNode.GetParentDataItem (typeof(Project), false);
  170. AddinData adata = p.GetAddinData ();
  171. ExtensionNodeInfo en = (ExtensionNodeInfo) CurrentNode.DataItem;
  172. ExtensionNodeType ntype = (ExtensionNodeType) data;
  173. ExtensionNodeDescription newNode = new ExtensionNodeDescription (ntype.NodeName);
  174. InsertNode (adata, en, DropPosition.After, newNode);
  175. adata.CachedAddinManifest.Save ();
  176. adata.NotifyChanged (false);
  177. }
  178. [CommandUpdateHandler (Commands.AddNodeBefore)]
  179. public void UpdateAddNodeBefore (CommandArrayInfo cinfo)
  180. {
  181. foreach (ExtensionNodeType ntype in GetAllowedChildTypes ()) {
  182. cinfo.Add (ntype.NodeName, ntype);
  183. }
  184. }
  185. [CommandHandler (Commands.AddNodeBefore)]
  186. public void AddNodeBefore (object data)
  187. {
  188. DotNetProject p = (DotNetProject) CurrentNode.GetParentDataItem (typeof(Project), false);
  189. AddinData adata = p.GetAddinData ();
  190. ExtensionNodeInfo en = (ExtensionNodeInfo) CurrentNode.DataItem;
  191. ExtensionNodeType ntype = (ExtensionNodeType) data;
  192. ExtensionNodeDescription newNode = new ExtensionNodeDescription (ntype.NodeName);
  193. InsertNode (adata, en, DropPosition.Before, newNode);
  194. adata.CachedAddinManifest.Save ();
  195. adata.NotifyChanged (false);
  196. }
  197. ExtensionNodeDescription InsertNode (AddinData adata, ExtensionNodeInfo refNode, DropPosition pos, ExtensionNodeDescription newNode)
  198. {
  199. ExtensionNodeDescriptionCollection nodes = null;
  200. newNode.InsertBefore = "";
  201. newNode.InsertAfter = "";
  202. if (refNode.CanModify) {
  203. if (pos == DropPosition.Into)
  204. nodes = refNode.Node.ChildNodes;
  205. else if (refNode.Node.Parent is ExtensionNodeDescription)
  206. nodes = ((ExtensionNodeDescription)refNode.Node.Parent).ChildNodes;
  207. } else {
  208. if (pos == DropPosition.After)
  209. newNode.InsertAfter = refNode.Node.Id;
  210. else if (pos == DropPosition.Before)
  211. newNode.InsertBefore = refNode.Node.Id;
  212. }
  213. if (nodes == null) {
  214. string path = refNode.Node.GetParentPath ();
  215. if (pos == DropPosition.Into)
  216. path += "/" + refNode.Node.Id;
  217. Extension ext = adata.CachedAddinManifest.MainModule.GetExtension (path);
  218. nodes = ext.ExtensionNodes;
  219. }
  220. for (int n = 0; n < nodes.Count; n++) {
  221. ExtensionNodeDescription node = nodes [n];
  222. if (node == refNode.Node) {
  223. if (pos == DropPosition.After) n++;
  224. nodes.Insert (n, newNode);
  225. return newNode;
  226. }
  227. }
  228. nodes.Add (newNode);
  229. return newNode;
  230. }
  231. ExtensionNodeTypeCollection GetAllowedChildTypes ()
  232. {
  233. ExtensionNodeInfo en = (ExtensionNodeInfo) CurrentNode.DataItem;
  234. object parent = en.Node.Parent;
  235. Extension ext = parent as Extension;
  236. if (ext != null)
  237. return ext.GetAllowedNodeTypes ();
  238. else {
  239. ExtensionNodeDescription node = (ExtensionNodeDescription) parent;
  240. if (node != null) {
  241. ExtensionNodeType tn = node.GetNodeType ();
  242. if (tn != null)
  243. return tn.GetAllowedNodeTypes ();
  244. }
  245. }
  246. return new ExtensionNodeTypeCollection ();
  247. }
  248. public override bool CanDeleteMultipleItems ()
  249. {
  250. foreach (var nav in CurrentNodes) {
  251. ExtensionNodeInfo en = (ExtensionNodeInfo) nav.DataItem;
  252. if (!en.CanModify)
  253. return false;
  254. }
  255. return true;
  256. }
  257. public override void DeleteMultipleItems ()
  258. {
  259. if (MessageService.Confirm (GettextCatalog.GetString ("Are you sure you want to delete the selected nodes?"), AlertButton.Delete)) {
  260. foreach (var nav in CurrentNodes) {
  261. ExtensionNodeInfo en = (ExtensionNodeInfo) nav.DataItem;
  262. if (en.Node.Parent is Extension)
  263. ((Extension)en.Node.Parent).ExtensionNodes.Remove (en.Node);
  264. else if (en.Node.Parent is ExtensionNodeDescription)
  265. ((ExtensionNodeDescription)en.Node.Parent).ChildNodes.Remove (en.Node);
  266. }
  267. }
  268. DotNetProject p = (DotNetProject) CurrentNode.GetParentDataItem (typeof(Project), false);
  269. AddinData adata = p.GetAddinData ();
  270. adata.SaveAddinManifest ();
  271. adata.NotifyChanged (false);
  272. }
  273. }
  274. }