Click here to Skip to main content
11,408,708 members (64,235 online)
Click here to Skip to main content
Add your own
alternative version

Plug-in Framework

, 14 Sep 2010 CPOL
Basic framework for building desktop plug-in applications.
PluginFramework.zip
PluginFramework
Demo
bin
Debug
Plugins
Release
Plugins
Forms
Properties
Settings.settings
DemoFormPlugin
Properties
Settings.settings
DemoUserControlPlugin
Properties
Settings.settings
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.Drawing;
using System.Linq;
using System.Windows.Forms;
using PluginFramework.Controls.Properties;
using PluginFramework.Interface;
using PluginFramework.Utilities;

namespace PluginFramework.Controls
{
    public class PluginTreeView : TreeView
    {
        //TODO: Check same image not being added multiple times
        ImageList imageList = new ImageList();

        public Size ImageListImageSize
        {
            get { return imageList.ImageSize; }
            set { imageList.ImageSize = value; }
        }

        public PluginTreeView()
        {
            this.ImageList = imageList;
            imageList.Images.Add(Resources.TreeImage);
        }

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

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

            try
            {
                if (!string.IsNullOrEmpty(pluginInfo.Plugin.Icon))
                {
                    imageList.Images.Add(Image.FromFile(pluginInfo.Plugin.Icon));
                    pluginItem.ImageIndex = imageList.Images.Count - 1;
                    pluginItem.SelectedImageIndex = imageList.Images.Count - 1;
                }
            }
            catch (NotImplementedException) { } // No icon

            #endregion

            #region SubGroup

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

            #endregion

            #region Group

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

                    if (subGroup != null)
                    {
                        group.Nodes.Add(subGroup);
                    }
                    else { group.Nodes.Add(pluginItem); }
                    //this.Nodes.Add(group);
                    EnsureNode(group);
                }
            }
            catch (NotImplementedException)
            {
                if (subGroup != null)
                {
                    this.Nodes.Add(subGroup);
                }
                else { this.Nodes.Add(pluginItem); }
                return;
            }

            #endregion

            if (group == null && subGroup == null)
            {
                this.Nodes.Add(pluginItem);
            }
        }
        public void AddSettingsPlugin(PluginInfo pluginInfo, ISettingsPlugin settingsPlugin)
        {
            #region Main Plugin Node

            TreeNode pluginItem = new TreeNode(pluginInfo.Plugin.Title);
            pluginItem.Tag = settingsPlugin;

            try
            {
                if (!string.IsNullOrEmpty(pluginInfo.Plugin.Icon))
                {
                    imageList.Images.Add(Image.FromFile(pluginInfo.Plugin.Icon));
                    pluginItem.ImageIndex = imageList.Images.Count - 1;
                    pluginItem.SelectedImageIndex = imageList.Images.Count - 1;
                }
            }
            catch (NotImplementedException) { } // No icon

            #endregion

            #region SubGroup

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

            #endregion

            #region Group

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

                    if (subGroup != null)
                    {
                        group.Nodes.Add(subGroup);
                    }
                    else { group.Nodes.Add(pluginItem); }
                    //this.Nodes.Add(group);
                    EnsureNode(group);
                }
            }
            catch (NotImplementedException)
            {
                if (subGroup != null)
                {
                    this.Nodes.Add(subGroup);
                }
                else { this.Nodes.Add(pluginItem); }
                return;
            }

            #endregion

            if (group == null && subGroup == null)
            {
                this.Nodes.Add(pluginItem);
            }
        }
        private TreeNode EnsureNode(TreeNode treeNode)
        {
            TreeNode node = (from x in this.Nodes.Cast<TreeNode>()
                             where x.Text == treeNode.Text
                             select x).SingleOrDefault();

            if (node == null)
            {
                this.Nodes.Add(treeNode);
                return treeNode;
            }
            else
            {
                foreach (TreeNode subNode in treeNode.Nodes)
                { node.Nodes.Add(subNode); }
                return node;
            }
        }
    }
}

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

gordon_matt
Software Developer VortexSoft
Vietnam Vietnam
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 14 Sep 2010
Article Copyright 2010 by gordon_matt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid