Click here to Skip to main content
15,896,259 members
Articles / Desktop Programming / Windows Forms

Plug-in Framework

Rate me:
Please Sign up or sign in to vote.
4.84/5 (25 votes)
17 May 2018CPOL4 min read 101.4K   1.9K   132  
Basic framework for building desktop plug-in applications
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using MBG.Extensions.Core;
using PluginFramework.Interface;
using PluginFramework.Utilities;

namespace PluginFramework.Controls
{
    public class PluginMenuStrip : MenuStrip
    {
        private List<string> pluginItems = new List<string>();

        public void AddPlugin(PluginInfo pluginInfo)
        {
            #region Main Plugin Node

            ToolStripMenuItem pluginItem = new ToolStripMenuItem(pluginInfo.Plugin.Title);
            pluginItem.Tag = pluginInfo;

            try
            {
                if (!string.IsNullOrEmpty(pluginInfo.Plugin.Icon))
                {
                    pluginItem.Image = Image.FromFile(pluginInfo.Plugin.Icon);
                }
            }
            catch (NotImplementedException) { } // No icon

            if (pluginInfo.Plugin is IFormPlugin)
            {
                pluginItem.Click += new EventHandler(pluginItem_Click);
            }

            #endregion

            ToolStripMenuItem subGroup = null;
            try
            {
                if (!string.IsNullOrEmpty(pluginInfo.Plugin.SubGroup))
                {
                    subGroup = new ToolStripMenuItem(pluginInfo.Plugin.SubGroup);
                    subGroup.DropDownItems.Add(pluginItem);
                }
            }
            catch (NotImplementedException)
            {
                // Do nothing (check for main group next)
            }

            ToolStripMenuItem group = null;
            try
            {
                if (!string.IsNullOrEmpty(pluginInfo.Plugin.Group))
                {
                    group = new ToolStripMenuItem(pluginInfo.Plugin.Group);

                    if (subGroup != null)
                    {
                        group.DropDownItems.Add(subGroup);
                    }
                    else { group.DropDownItems.Add(pluginItem); }
                    //this.Items.Add(group);
                    EnsureItem(group);
                    pluginItems.Add(pluginInfo.Plugin.Group);
                }
            }
            catch (NotImplementedException)
            {
                if (subGroup != null)
                {
                    this.Items.Add(subGroup);
                    pluginItems.Add(pluginInfo.Plugin.SubGroup);
                }
                else
                {
                    this.Items.Add(pluginItem);
                    pluginItems.Add(pluginInfo.Plugin.Title);
                }
                return;
            }

            if (group == null && subGroup == null)
            {
                this.Items.Add(pluginItem);
                pluginItems.Add(pluginInfo.Plugin.Title);
            }
        }
        private ToolStripMenuItem EnsureItem(ToolStripMenuItem menuItem)
        {
            ToolStripMenuItem item = (from x in this.Items.Cast<ToolStripMenuItem>()
                                      where x.Text == menuItem.Text
                                      select x).SingleOrDefault();

            if (item == null)
            {
                this.Items.Add(menuItem);
                return menuItem;
            }
            else
            {
                foreach (ToolStripMenuItem subItem in menuItem.DropDownItems)
                { item.DropDownItems.Add(subItem); }
                return item;
            }
        }
        public void RemovePlugins()
        {
            foreach (string item in pluginItems)
            {
                this.Items.RemoveByKey(item);
            }
        }

        void pluginItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            PluginInfo pluginInfo = menuItem.Tag as PluginInfo;
            IFormPlugin plugin = pluginInfo.Plugin as IFormPlugin;
            Form form = plugin.Content;

            if (form.IsDisposed)
            {
                form = PluginHelper.CreateNewInstance<Form>(pluginInfo.AssemblyPath);
            }

            if (plugin.ShowAs == ShowAs.Dialog)
            {
                form.ShowDialog();
            }
            else
            {
                form.Show();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freelancer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions