Ejemplo n.º 1
0
        private static void EnsureDefaultChildHierarchy(IDesignerHost designerHost)
        {
            //When we are adding the root activity we need to make sure that all the child activities which are required by the parent
            //activity are looked up in the toolboxitem and added appropriately
            //If the composite activity already has a some child activities but not all then it
            //means that user has changed the InitializeComponent and hence we do nothing
            //This is the simple check to get the designer working in case of selecting composite
            //root activities
            CompositeActivity rootActivity = designerHost.RootComponent as CompositeActivity;

            if (rootActivity != null && rootActivity.Activities.Count == 0)
            {
                object[]             attribs           = rootActivity.GetType().GetCustomAttributes(typeof(ToolboxItemAttribute), false);
                ToolboxItemAttribute toolboxItemAttrib = (attribs != null && attribs.GetLength(0) > 0) ? attribs[0] as ToolboxItemAttribute : null;
                if (toolboxItemAttrib != null && toolboxItemAttrib.ToolboxItemType != null)
                {
                    ToolboxItem  item       = Activator.CreateInstance(toolboxItemAttrib.ToolboxItemType, new object[] { rootActivity.GetType() }) as ToolboxItem;
                    IComponent[] components = item.CreateComponents();

                    //I am assuming here that there will be always one top level component created.
                    //If there are multiple then there is a bigger problem as we dont know how
                    //to use those
                    CompositeActivity compositeActivity = null;
                    foreach (IComponent component in components)
                    {
                        if (component.GetType() == rootActivity.GetType())
                        {
                            compositeActivity = component as CompositeActivity;
                            break;
                        }
                    }

                    //Add the children
                    if (compositeActivity != null && compositeActivity.Activities.Count > 0)
                    {
                        IIdentifierCreationService identifierCreationService = designerHost.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                        if (identifierCreationService != null)
                        {
                            //We do not go thru the composite designer here as composite activity
                            //might have simple designer
                            Activity[] activities = compositeActivity.Activities.ToArray();
                            compositeActivity.Activities.Clear();

                            identifierCreationService.EnsureUniqueIdentifiers(rootActivity, activities);
                            // Work around : Don't called AddRange because it doesn't send the ListChange notifications
                            // to the activity collection.  Use multiple Add calls instaead
                            foreach (Activity newActivity in activities)
                            {
                                rootActivity.Activities.Add(newActivity);
                            }

                            foreach (Activity childActivity in activities)
                            {
                                WorkflowDesignerLoader.AddActivityToDesigner(designerHost, childActivity);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void EnsureDefaultChildHierarchy(IDesignerHost designerHost)
        {
            CompositeActivity rootComponent = designerHost.RootComponent as CompositeActivity;

            if ((rootComponent != null) && (rootComponent.Activities.Count == 0))
            {
                object[]             customAttributes = rootComponent.GetType().GetCustomAttributes(typeof(ToolboxItemAttribute), false);
                ToolboxItemAttribute attribute        = ((customAttributes != null) && (customAttributes.GetLength(0) > 0)) ? (customAttributes[0] as ToolboxItemAttribute) : null;
                if ((attribute != null) && (attribute.ToolboxItemType != null))
                {
                    IComponent[]      componentArray = (Activator.CreateInstance(attribute.ToolboxItemType, new object[] { rootComponent.GetType() }) as ToolboxItem).CreateComponents();
                    CompositeActivity activity2      = null;
                    foreach (IComponent component in componentArray)
                    {
                        if (component.GetType() == rootComponent.GetType())
                        {
                            activity2 = component as CompositeActivity;
                            break;
                        }
                    }
                    if ((activity2 != null) && (activity2.Activities.Count > 0))
                    {
                        IIdentifierCreationService service = designerHost.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                        if (service != null)
                        {
                            Activity[] childActivities = activity2.Activities.ToArray();
                            activity2.Activities.Clear();
                            service.EnsureUniqueIdentifiers(rootComponent, childActivities);
                            foreach (Activity activity3 in childActivities)
                            {
                                rootComponent.Activities.Add(activity3);
                            }
                            foreach (Activity activity4 in childActivities)
                            {
                                WorkflowDesignerLoader.AddActivityToDesigner(designerHost, activity4);
                            }
                        }
                    }
                }
            }
        }