Click here to Skip to main content
12,353,329 members (70,350 online)
Click here to Skip to main content

Stats

44.2K views
1.4K downloads
108 bookmarked
Posted

Plug-in Framework

, 14 Sep 2010 CPOL
Basic framework for building desktop plug-in applications.
PluginFramework
Demo
bin
Debug
Plugins
Release
Plugins
Forms
Properties
DemoFormPlugin
Properties
DemoUserControlPlugin
Properties
MBG.Extensions
Collections
Controls
Core
Data
Math
Properties
Xml
MBG.IO
Properties
PluginFramework.Attributes
Properties
PluginFramework.Controls
Properties
Resources
Icons
SettingsIcon.ico
TreeIcon.ico
Images
SettingsImage.png
TreeImage.png
PluginFramework.Interface
Properties
PluginFramework.Utilities
Properties
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)

Share

About the Author

vnmatt
Software Developer (Senior) Freelancer
Vietnam Vietnam
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 14 Sep 2010
Article Copyright 2010 by vnmatt
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid