using System; using System.Windows.Forms; using Core.Common.Controls.TreeView; using Core.Common.Gui.Properties; namespace Core.Common.Gui.ContextMenu { /// /// This class represents a builder which can be used to create a context menu, which's items require information /// on the in order to render or perform actions. /// public class ContextMenuBuilder : IContextMenuBuilder { private readonly GuiContextMenuItemFactory guiItemsFactory; private readonly TreeViewContextMenuItemFactory treeViewItemsFactory; private readonly ContextMenuStrip contextMenu; /// /// Creates a new instance of , which uses the given to /// create a for the given . /// /// The from which to obtain information to render and bind actions /// to the items of the . If null, this builder will not render items which /// require this type of information. /// The /// from which to obtain information to render and bind actions to the items of the /// . If null, this builder will not render items /// which require this type of information. /// The from which /// to obtain information to render and bind actions to the items of the . /// If null, this builder will not render items which require this type of information. /// The for which to create a . /// The to use while creating the . /// Thrown when the required object instances could not be created based on /// the or . public ContextMenuBuilder(IApplicationFeatureCommands featureCommandHandler, IExportImportCommandHandler importExportHandler, IViewCommands viewsCommandsHandler, TreeNode treeNode, TreeNodeInfo treeNodeInfo) { try { guiItemsFactory = new GuiContextMenuItemFactory(featureCommandHandler, importExportHandler, viewsCommandsHandler, treeNode); treeViewItemsFactory = new TreeViewContextMenuItemFactory(treeNode, treeNodeInfo); } catch (ArgumentNullException e) { throw new ContextMenuBuilderException(Resources.ContextMenuBuilder_ContextMenuBuilder_Cannot_create_instances_of_factories, e); } contextMenu = new ContextMenuStrip(); } public IContextMenuBuilder AddRenameItem() { AddItem(treeViewItemsFactory.CreateRenameItem()); return this; } public IContextMenuBuilder AddDeleteItem() { AddItem(treeViewItemsFactory.CreateDeleteItem()); return this; } public IContextMenuBuilder AddExpandAllItem() { AddItem(treeViewItemsFactory.CreateExpandAllItem()); return this; } public IContextMenuBuilder AddCollapseAllItem() { AddItem(treeViewItemsFactory.CreateCollapseAllItem()); return this; } public IContextMenuBuilder AddOpenItem() { AddItem(guiItemsFactory.CreateOpenItem()); return this; } public IContextMenuBuilder AddExportItem() { AddItem(guiItemsFactory.CreateExportItem()); return this; } public IContextMenuBuilder AddImportItem() { AddItem(guiItemsFactory.CreateImportItem()); return this; } public IContextMenuBuilder AddPropertiesItem() { AddItem(guiItemsFactory.CreatePropertiesItem()); return this; } public IContextMenuBuilder AddSeparator() { if (MayAddSeparator()) { AddItem(new ToolStripSeparator()); } return this; } public IContextMenuBuilder AddCustomItem(StrictContextMenuItem item) { AddItem(item); return this; } public ContextMenuStrip Build() { if (contextMenu.Items.Count > 0) { var lastIndex = contextMenu.Items.Count - 1; if (contextMenu.Items[lastIndex] is ToolStripSeparator) { contextMenu.Items.RemoveAt(lastIndex); } } return contextMenu; } private bool MayAddSeparator() { if (contextMenu.Items.Count == 0) { return false; } return !(contextMenu.Items[contextMenu.Items.Count - 1] is ToolStripSeparator); } private void AddItem(ToolStripItem item) { if (item != null) { contextMenu.Items.Add(item); } } } }