Friday, November 19, 2010

SharpDevelop:- ToolBoxProvider Service

In this blog , we will look at ToolBoxProvider service of SharpDevelop. SharpDevelop ToolBox window  uses this service for creating the categories and  loading the controls inside  the ToolBox . This service is provided by FormsDesigner AddIn.  Default controls loaded  in the SharpDevelop’s ToolBox are from various assemblies (System.Windows.Forms, System.Data, System.Drawing etc ) . Information regarding these controls assemblies and components inside them is stored by SharpDevelop in a  xml file called  “SharpDevelopControlLibrary.sdcl” , this file is located at  “..\data\options”. If we want our custom controls  to be  added to the ToolBox , we have to use the services of ToolBoxProvider.  Lets us see how to add the components from controls library . 

public static void LoadToolBoxItems()
   {
       List<ToolComponent> lstToolsComponent = null;

       string ControlsFilePath = System.Configuration.ConfigurationManager.AppSettings["Controls_File_Path"].ToString();

       Category myCatagory = new Category("MyCatagory");
       myCatagory.IsEnabled = true;

       ToolboxProvider.ComponentLibraryLoader.Categories.Add(myCatagory);

       //Add the controls file controls
       Assembly asm = Assembly.LoadFrom(ControlsFilePath);
       lstToolsComponent = AddComponentsToList(asm, Path.GetDirectoryName(ControlsFilePath));
       foreach (ToolComponent comp in lstToolsComponent)
       {
           myCatagory.ToolComponents.Add(comp);
       }

       ToolboxProvider.SaveToolbox();
       ToolboxProvider.ReloadSideTabs(true);
   }

Above routine gets the control library’s path from config and loads the assembly at runtime to fetch the components from the assembly . Another routine AddComponetsToList()  takes the assembly and assembly directory path as parameter and returns the list of  ToolComponent instances. At last we call SaveToolBox on ToolBoxProvider and reload the toolbox. SaveToolBox routine populates the “SharpDevelopControlLibrary.sdcl” file with our assembly and components information and saves it. ReloadSideTabs routine reads the .sdcl file and fills the components in ToolBox.

AddComponentsToList routine :--

static List<ToolComponent> AddComponentsToList(Assembly assembly, string loadPath)
    {
        List<ToolComponent> lstToolsComponent = new List<ToolComponent>();
        if (assembly != null)
        {
            try
            {

                foreach (Type t in assembly.GetExportedTypes())
                {
                    if (!t.IsAbstract)
                    {
                        if (t.IsDefined(typeof(ToolboxItemFilterAttribute), true) || t.IsDefined(typeof(ToolboxItemAttribute), true) || typeof(System.ComponentModel.IComponent).IsAssignableFrom(t))
                        {
                            object[] attributes = t.GetCustomAttributes(false);
                            object[] filterAttrs = t.GetCustomAttributes(typeof(DesignTimeVisibleAttribute), true);
                            foreach (DesignTimeVisibleAttribute visibleAttr in filterAttrs)
                            {
                                if (!visibleAttr.Visible)
                                {
                                    goto skip;
                                }
                            }
                            ToolComponent toolComponent = new ToolComponent(t.FullName, new ComponentAssembly(assembly.FullName, loadPath), true);
                            lstToolsComponent.Add(toolComponent);

                        skip: ;
                        }
                    }
                }
                return lstToolsComponent;
            }
            catch (Exception e)
            {
                return lstToolsComponent;
            }
        }
        return lstToolsComponent;
    }

No comments:

Post a Comment