Example #1
0
        private void ShowProjectContextMenu(MouseEventArgs e)
        {
            var p = new Point(e.X, e.Y);

            TreeNode selectedNode = ProjectTree.GetNodeAt(p);

            if (selectedNode == null)
            {
                return;
            }

            ProjectTree.SelectedNode = selectedNode;

            var          currentElement = (Element)selectedNode.Tag;
            IList <Type> childTypes     = Engine.GetChildTypes(currentElement.GetType());

            ProjectContextMenu.Items.Clear();
            var addNewLabel = new ToolStripLabel("Elements:")
            {
                Font = BoldFont.Font
            };

            ProjectContextMenu.Items.Add(addNewLabel);
            foreach (Type childType in childTypes)
            {
                bool allowNew            = true;
                var  allowMultipleAttrib = childType.GetAttribute <AllowMultipleAttribute>();
                if (allowMultipleAttrib != null && allowMultipleAttrib.Allow == false)
                {
                    foreach (Element childElement in currentElement.AllChildren)
                    {
                        if (childElement.GetType() == childType)
                        {
                            allowNew = false;
                            break;
                        }
                    }
                }
                string itemText = string.Format("Add {0}", childType.GetElementName());
                var    item     = new ToolStripMenuItem(itemText)
                {
                    Tag = childType
                };

                item.Click  += NewElement_Click;
                item.Enabled = allowNew;
                item.Image   = childType.GetElementIcon();
                if (allowNew)
                {
                }
                else
                {
                    item.ToolTipText = "Only one instance of this item is allowed";
                }
                ProjectContextMenu.Items.Add(item);
            }


            var separator1 = new ToolStripSeparator();

            ProjectContextMenu.Items.Add(separator1);


            var verbLabel = new ToolStripLabel("Verbs:")
            {
                Font = BoldFont.Font
            };

            ProjectContextMenu.Items.Add(verbLabel);

            List <MethodInfo> elementVerbs = currentElement.GetType().GetElementVerbs();

            foreach (MethodInfo method in elementVerbs)
            {
                string verbName = method.GetVerbName();
                var    item     = new ToolStripMenuItem(verbName)
                {
                    Tag = method
                };

                item.Click += ElementVerb_Click;
                ProjectContextMenu.Items.Add(item);
            }


            ProjectContextMenu.Show(ProjectTree, e.Location);
        }