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);
                            }
                        }
                    }
                }
            }
        }
        public override void SetValue(object component, object value)
        {
            Activity activity = component as Activity;

            if (activity != null)
            {
                ISite site = PropertyDescriptorUtils.GetSite(ServiceProvider, component);
                if (site == null)
                {
                    throw new Exception(SR.GetString(SR.General_MissingService, typeof(ISite).FullName));
                }

                IIdentifierCreationService identifierCreationService = site.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                if (identifierCreationService == null)
                {
                    throw new Exception(SR.GetString(SR.General_MissingService, typeof(IIdentifierCreationService).FullName));
                }

                string newID = value as string;
                identifierCreationService.ValidateIdentifier(activity, newID);

                DesignerHelpers.UpdateSiteName(activity, newID);
                base.SetValue(component, value);
            }
        }
        private string GeneratePropertyAssociatedFieldName(string className, string propertyName, Type propertyType, out bool exisitingField)
        {
            exisitingField = false;

            IIdentifierCreationService identCreationService = this.ServiceProvider.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;

            if (identCreationService == null)
            {
                throw new InvalidOperationException();
            }

            string baseFieldName = "_";

            if (propertyName.StartsWith("@"))
            {
                baseFieldName += propertyName.Substring(1);
            }
            else
            {
                baseFieldName += propertyName;
            }

            bool   validIdent = false;
            int    identCount = 1;
            string fieldName  = String.Empty;

            while (!validIdent && identCount < Int32.MaxValue)
            {
                fieldName = baseFieldName + System.Convert.ToString(identCount);

                try
                {
                    identCreationService.ValidateIdentifier(RootActivity, fieldName);
                    validIdent = true;

                    //See if the field already exists, and if it's the same type
                    if (DoesFieldExist(className, fieldName, propertyType))
                    {
                        exisitingField = true;
                    }
                    else if (DoesFieldExist(className, fieldName))
                    {
                        validIdent = false;
                    }
                }
                catch
                {
                    validIdent = false;
                }

                identCount += 1;
            }

            return(fieldName);
        }
        public override void SetValue(object component, object value)
        {
            Activity activity = component as Activity;

            if (activity != null)
            {
                IIdentifierCreationService service = activity.Site.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                if (service == null)
                {
                    throw new Exception(SR.GetString("General_MissingService", new object[] { typeof(IIdentifierCreationService).FullName }));
                }
                string identifier = value as string;
                service.ValidateIdentifier(activity, identifier);
                bool ignoreCase      = CompilerHelpers.GetSupportedLanguage(activity.Site) == SupportedLanguages.VB;
                Type dataSourceClass = Helpers.GetDataSourceClass(Helpers.GetRootActivity(activity), activity.Site);
                if ((dataSourceClass != null) && (ActivityBindPropertyDescriptor.FindMatchingMember(identifier, dataSourceClass, ignoreCase) != null))
                {
                    throw new ArgumentException(SR.GetString("Error_ActivityNameExist", new object[] { identifier }));
                }
                IMemberCreationService service2 = activity.Site.GetService(typeof(IMemberCreationService)) as IMemberCreationService;
                if (service2 == null)
                {
                    throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IMemberCreationService).FullName }));
                }
                IDesignerHost host = activity.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
                if (host == null)
                {
                    throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(IDesignerHost).FullName }));
                }
                string newClassName = identifier;
                int    num          = host.RootComponentClassName.LastIndexOf('.');
                if (num > 0)
                {
                    newClassName = host.RootComponentClassName.Substring(0, num + 1) + identifier;
                }
                service2.UpdateTypeName(((Activity)host.RootComponent).GetValue(WorkflowMarkupSerializer.XClassProperty) as string, newClassName);
                ((Activity)host.RootComponent).SetValue(WorkflowMarkupSerializer.XClassProperty, newClassName);
                base.SetValue(component, value);
                DesignerHelpers.UpdateSiteName((Activity)host.RootComponent, identifier);
            }
        }
Ejemplo n.º 5
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);
                            }
                        }
                    }
                }
            }
        }
            public override void SetValue(object component, object value)
            {
                if (IsReadOnly)
                {
                    throw new ArgumentException(this.eventDescriptor.Name);
                }

                if (value != null && !(value is string))
                {
                    throw new ArgumentException(this.eventDescriptor.Name);
                }

                if (component is DependencyObject)
                {
                    string           name             = value as string;
                    DependencyObject dependencyObject = component as DependencyObject;
                    string           oldName          = null;
                    if (dependencyObject.GetValue(WorkflowMarkupSerializer.EventsProperty) == null)
                    {
                        dependencyObject.SetValue(WorkflowMarkupSerializer.EventsProperty, new Hashtable());
                    }

                    Hashtable dynamicEvents = dependencyObject.GetValue(WorkflowMarkupSerializer.EventsProperty) as Hashtable;
                    if (dynamicEvents.ContainsKey(this.eventDescriptor.Name))
                    {
                        oldName = dynamicEvents[this.eventDescriptor.Name] as string;
                    }

                    if (oldName != null && name != null && oldName.Equals(name, StringComparison.Ordinal))
                    {
                        foreach (string methodName in this.eventSvc.GetCompatibleMethods(this.eventDescriptor))
                        {
                            if (methodName.Equals(name, StringComparison.CurrentCulture))
                            {
                                return;
                            }
                        }
                    }
                    else if (oldName == name)
                    {
                        return;
                    }

                    IDesignerHost host = this.serviceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost;
                    if (host == null)
                    {
                        throw new InvalidOperationException(typeof(IDesignerHost).FullName);
                    }

                    if (!String.IsNullOrEmpty(name))
                    {
                        if (name.StartsWith("@"))
                        {
                            throw new InvalidOperationException(name);
                        }

                        Activity rootActivity = host.RootComponent as Activity;
                        if (rootActivity != null)
                        {
                            // make sure the name doesn't conflict with an existing member in the code-beside.
                            MemberInfo matchingMember = null;
                            Type       designedType   = Helpers.GetDataSourceClass(rootActivity, this.serviceProvider);
                            if (designedType != null)
                            {
                                WorkflowLoader loader = this.serviceProvider.GetService(typeof(WorkflowLoader)) as WorkflowLoader;
                                foreach (MemberInfo memberInfo in designedType.GetMembers(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
                                {
                                    if (memberInfo.Name.Equals(name, StringComparison.Ordinal))
                                    {
                                        matchingMember = memberInfo;
                                        break;
                                    }
                                }
                            }

                            if (matchingMember != null)
                            {
                                if (!(matchingMember is MethodInfo))
                                {
                                    throw new InvalidOperationException(designedType.FullName);
                                }
                            }
                            else
                            {
                                // make sure the name doesn't conflict with an existing activity.
                                IIdentifierCreationService idService = this.serviceProvider.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                                if (idService != null)
                                {
                                    idService.ValidateIdentifier(rootActivity, name);
                                }
                            }
                        }
                    }

                    // If there is a designer host, create a transaction.
                    DesignerTransaction trans = null;
                    if (host != null)
                    {
                        trans = host.CreateTransaction(name);
                    }

                    try
                    {
                        IComponentChangeService change = this.serviceProvider.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
                        if (change != null)
                        {
                            try
                            {
                                change.OnComponentChanging(component, this);
                                change.OnComponentChanging(component, this.eventDescriptor);
                            }
                            catch (CheckoutException coEx)
                            {
                                if (coEx == CheckoutException.Canceled)
                                {
                                    return;
                                }
                                throw;
                            }
                        }

                        if (name != null)
                        {
                            if (host.RootComponent != null)
                            {
                                if (!this.useMethodCalled && !string.IsNullOrEmpty(oldName))
                                {
                                    eventSvc.UseMethod((IComponent)component, eventDescriptor, oldName);
                                }

                                eventSvc.UseMethod((IComponent)component, eventDescriptor, name);
                                this.useMethodCalled = true;
                            }
                        }

                        if (oldName != null && host.RootComponent != null)
                        {
                            eventSvc.FreeMethod((IComponent)component, eventDescriptor, oldName);
                        }

                        dynamicEvents[this.eventDescriptor.Name] = name;

                        if (change != null)
                        {
                            change.OnComponentChanged(component, this.eventDescriptor, null, null);
                            change.OnComponentChanged(component, this, oldName, name);
                        }

                        OnValueChanged(component, EventArgs.Empty);

                        if (trans != null)
                        {
                            trans.Commit();
                        }
                    }
                    finally
                    {
                        if (trans != null)
                        {
                            ((IDisposable)trans).Dispose();
                        }
                    }
                }
            }
        public override void SetValue(object component, object value)
        {
            Activity activity = component as Activity;

            if (activity != null)
            {
                // validate the identifier
                IIdentifierCreationService identifierCreationService = activity.Site.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                if (identifierCreationService == null)
                {
                    throw new Exception(SR.GetString(SR.General_MissingService, typeof(IIdentifierCreationService).FullName));
                }

                string name = value as string;
                identifierCreationService.ValidateIdentifier(activity, name);

                bool isVB         = (CompilerHelpers.GetSupportedLanguage(activity.Site) == SupportedLanguages.VB);
                Type designedType = Helpers.GetDataSourceClass(Helpers.GetRootActivity(activity), activity.Site);
                if (designedType != null)
                {
                    MemberInfo matchingMember = ActivityBindPropertyDescriptor.FindMatchingMember(name, designedType, isVB);
                    if (matchingMember != null)
                    {
                        throw new ArgumentException(SR.GetString(SR.Error_ActivityNameExist, name));
                    }
                }
                IMemberCreationService memberCreationService = activity.Site.GetService(typeof(IMemberCreationService)) as IMemberCreationService;
                if (memberCreationService == null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IMemberCreationService).FullName));
                }

                IDesignerHost host = activity.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
                if (host == null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IDesignerHost).FullName));
                }

                // We need to update the activityType's name before trying to update the type because
                // updating the type causes a flush, which access the custom activity's properties, and
                // doing so requires the new type name
                string newClassName = name;
                int    indexOfDot   = host.RootComponentClassName.LastIndexOf('.');
                if (indexOfDot > 0)
                {
                    newClassName = host.RootComponentClassName.Substring(0, indexOfDot + 1) + name;
                }

                // IMPORTANT: You must update the class name in code before renaming the site, since
                // VS's OnComponentRename updates the RootComponentClassName, so the flush code called
                // in our OnComponentRename tries to access the new class for information.
                memberCreationService.UpdateTypeName(((Activity)host.RootComponent).GetValue(WorkflowMarkupSerializer.XClassProperty) as string, newClassName);

                //((Activity)host.RootComponent).Name = name;
                ((Activity)host.RootComponent).SetValue(WorkflowMarkupSerializer.XClassProperty, newClassName);
                base.SetValue(component, value);

                // Update the site name so the component name shows up correctly in the designer
                DesignerHelpers.UpdateSiteName((Activity)host.RootComponent, name);
            }
        }