private static void UpdateTypeArgument(ModelItem modelItem, Type value)
        {
            if (value != null)
            {
                Type oldModelItemType = modelItem.ItemType;
                Fx.Assert(oldModelItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?");
                Type newModelItemType = oldModelItemType.GetGenericTypeDefinition().MakeGenericType(value);
                Fx.Assert(newModelItemType != null, "New model item type needs to be non null or we cannot proceed further");
                ModelItem newModelItem = ModelFactory.CreateItem(modelItem.GetEditingContext(), Activator.CreateInstance(newModelItemType));
                MorphHelper.MorphObject(modelItem, newModelItem);
                MorphHelper.MorphProperties(modelItem, newModelItem);

                if (oldModelItemType.IsSubclassOf(typeof(Activity)) && newModelItemType.IsSubclassOf(typeof(Activity)))
                {
                    if (string.Equals((string)modelItem.Properties["DisplayName"].ComputedValue, GetActivityDefaultName(oldModelItemType), StringComparison.Ordinal))
                    {
                        newModelItem.Properties["DisplayName"].SetValue(GetActivityDefaultName(newModelItemType));
                    }
                }

                DesignerView designerView = modelItem.GetEditingContext().Services.GetService<DesignerView>();
                if (designerView != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, (Action)(() =>
                    {
                        if (designerView.RootDesigner != null && ((WorkflowViewElement)designerView.RootDesigner).ModelItem == modelItem)
                        {
                            designerView.MakeRootDesigner(newModelItem, true);
                        }
                        Selection.SelectOnly(modelItem.GetEditingContext(), newModelItem);
                    }));
                }
            }
        }
Beispiel #2
0
        private static void UpdateTypeArgument(ModelItem modelItem, Type value)
        {
            if (value != null)
            {
                Type oldModelItemType = modelItem.ItemType;
                Fx.Assert(oldModelItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?");
                Type newModelItemType = oldModelItemType.GetGenericTypeDefinition().MakeGenericType(value);
                Fx.Assert(newModelItemType != null, "New model item type needs to be non null or we cannot proceed further");
                ModelItem newModelItem = ModelFactory.CreateItem(modelItem.GetEditingContext(), Activator.CreateInstance(newModelItemType));
                MorphHelper.MorphObject(modelItem, newModelItem);
                MorphHelper.MorphProperties(modelItem, newModelItem);

                if (oldModelItemType.IsSubclassOf(typeof(Activity)) && newModelItemType.IsSubclassOf(typeof(Activity)))
                {
                    if (string.Equals((string)modelItem.Properties["DisplayName"].ComputedValue, GetActivityDefaultName(oldModelItemType), StringComparison.Ordinal))
                    {
                        newModelItem.Properties["DisplayName"].SetValue(GetActivityDefaultName(newModelItemType));
                    }
                }

                DesignerView designerView = modelItem.GetEditingContext().Services.GetService <DesignerView>();
                if (designerView != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, (Action)(() =>
                    {
                        if (designerView.RootDesigner != null && ((WorkflowViewElement)designerView.RootDesigner).ModelItem == modelItem)
                        {
                            designerView.MakeRootDesigner(newModelItem, true);
                        }
                        Selection.SelectOnly(modelItem.GetEditingContext(), newModelItem);
                    }));
                }
            }
        }
        public static object ActivityActionMorphHelper(ModelItem originalValue, ModelProperty newModelProperty)
        {
            Fx.Assert(newModelProperty.PropertyType.GetGenericArguments().Count() == 1, "This should only be applied for ActivityAction<T>");
            Type activityActionTypeArgument = newModelProperty.PropertyType.GetGenericArguments()[0];
            Type activityActionType = typeof(ActivityAction<>).MakeGenericType(activityActionTypeArgument);
            object activityAction = Activator.CreateInstance(activityActionType);
            ModelItem morphed = ModelFactory.CreateItem(originalValue.GetEditingContext(), activityAction);

            ModelItem originalActivityActionArgument = originalValue.Properties[PropertyNames.ActionArgument].Value;
            if (originalActivityActionArgument != null)
            {
                Type variableType = typeof(DelegateInArgument<>).MakeGenericType(activityActionTypeArgument);
                DelegateInArgument iterationDelegateArgument = (DelegateInArgument)Activator.CreateInstance(variableType);
                iterationDelegateArgument.Name = (string)originalActivityActionArgument.Properties[PropertyNames.NameProperty].Value.GetCurrentValue();
                morphed.Properties[PropertyNames.ActionArgument].SetValue(iterationDelegateArgument);
            }

            ModelItem originalActivityActionHandler = originalValue.Properties[PropertyNames.ActionHandler].Value;
            if (originalActivityActionHandler != null)
            {
                morphed.Properties[PropertyNames.ActionHandler].SetValue(originalActivityActionHandler);
                originalValue.Properties[PropertyNames.ActionHandler].SetValue(null);
            }

            return morphed;
        }
        internal static Argument MorphArgument(ModelItem originalValue, Type targetType)
        {
            Argument morphed = null;
            Argument original = (Argument)originalValue.GetCurrentValue();
            ActivityWithResult originalExpression = original.Expression;
            
            if (originalExpression != null)
            {
                Type expressionType = originalExpression.GetType();
                Type expressionGenericType = expressionType.IsGenericType ? expressionType.GetGenericTypeDefinition() : null;

                if (expressionGenericType != null)
                {       
                    bool isLocation = ExpressionHelper.IsGenericLocationExpressionType(originalExpression);
                    ActivityWithResult morphedExpression;
                    EditingContext context = originalValue.GetEditingContext();
                    morphed = Argument.Create(targetType, original.Direction);
                    if (ExpressionHelper.TryMorphExpression(originalExpression, isLocation, targetType, 
                        context, out morphedExpression))
                    {                        
                        morphed.Expression = morphedExpression;
                    }
                    //[....] 

                }
            }
            return morphed;
        }
Beispiel #5
0
        /// <summary>
        /// Clean key in modelitem's view state
        /// </summary>
        public static void ClearViewState(ModelItem model, string key)
        {
            Contract.Requires(model != null);
            Contract.Requires(key != null);

            ViewStateService vss = model.GetEditingContext().Services.GetService<ViewStateService>();
            vss.RemoveViewState(model, key);
        }
 public bool Initialize(ModelItem modelItem)
 {
     this.baseModelItem = modelItem;
     if (null != modelItem)
     {
         this.context = modelItem.GetEditingContext();
     }
     return (null != this.baseModelItem);
 }
        PropertyDescriptorCollection GetPropertyDescriptors()
        {
            PropertyDescriptorCollection propertyDescriptors = PropertyDescriptorCollection.Empty;

            try
            {
                object instance = parent.GetCurrentValue();
                if (instance != null)
                {
                    if (!(instance is ICustomTypeDescriptor))
                    {
                        Type instanceType = instance.GetType();
                        if (instanceType.IsValueType)
                        {
                            propertyDescriptors = TypeDescriptor.GetProvider(instanceType).GetTypeDescriptor(instanceType).GetProperties();
                        }
                        else
                        {
                            propertyDescriptors = TypeDescriptor.GetProvider(instance).GetTypeDescriptor(instance).GetProperties();
                        }
                    }
                    else
                    {
                        propertyDescriptors = TypeDescriptor.GetProperties(instance);
                    }
                }

                // Add browsable attached properties
                AttachedPropertiesService AttachedPropertiesService = this.parent.GetEditingContext().Services.GetService <AttachedPropertiesService>();
                if (AttachedPropertiesService != null)
                {
                    var browsableAttachedProperties = from attachedProperty in AttachedPropertiesService.GetAttachedProperties(this.parent.ItemType)
                                                      where (attachedProperty.IsBrowsable || attachedProperty.IsVisibleToModelItem)
                                                      select new AttachedPropertyDescriptor(attachedProperty, this.parent);

                    List <PropertyDescriptor> mergedProperties = new List <PropertyDescriptor>();
                    foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors)
                    {
                        mergedProperties.Add(propertyDescriptor);
                    }
                    propertyDescriptors = new PropertyDescriptorCollection(mergedProperties.Concat(browsableAttachedProperties).ToArray(), true);
                }
            }
            catch (FileNotFoundException e)
            {
                EditingContext context = parent.GetEditingContext();
                if (context.Items.GetValue <ErrorItem>() == null)
                {
                    context.Items.SetValue(new ErrorItem {
                        Message = e.Message, Details = e.ToString()
                    });
                }
            }

            return(propertyDescriptors);
        }
        public static void OnAddAnnotationCommandExecuted(ExecutedRoutedEventArgs e, ModelItem modelItem)
        {
            ModelProperty property = modelItem.Properties.Find(Annotation.AnnotationTextPropertyName);
            if (property != null)
            {
                using (ModelEditingScope editingScope = modelItem.BeginEdit(SR.AddAnnotationDescription))
                {
                    property.SetValue(string.Empty);
                    ViewStateService viewStateService = modelItem.GetEditingContext().Services.GetService<ViewStateService>();
                    viewStateService.StoreViewStateWithUndo(modelItem, Annotation.IsAnnotationDockedViewStateName, false);
                    editingScope.Complete();
                }

                if (modelItem.View != null)
                {
                    WorkflowViewElement element = modelItem.View as WorkflowViewElement;
                    if (element != null)
                    {
                        element.OnEditAnnotation();
                    }
                }
            }

            e.Handled = true;
        }
        public static void OnDeleteAnnotationCommandExecuted(ExecutedRoutedEventArgs e, ModelItem modelItem)
        {
            using (ModelEditingScope editingScope = modelItem.BeginEdit(SR.DeleteAnnotationDescription))
            {
                modelItem.Properties[Annotation.AnnotationTextPropertyName].SetValue(null);
                ViewStateService viewStateService = modelItem.GetEditingContext().Services.GetService<ViewStateService>();
                viewStateService.StoreViewStateWithUndo(modelItem, Annotation.IsAnnotationDockedViewStateName, null);
                editingScope.Complete();
            }

            e.Handled = true;
        }
        internal static List<ModelItem> GetVariablesInScope(ModelItem ownerActivity)
        {
            List<ModelItem> declaredVariables = new List<ModelItem>();
            if (ownerActivity != null)
            {
                bool includeArguments = !(ownerActivity.GetCurrentValue() is ActivityBuilder);
                FrameworkName targetFramework = WorkflowDesigner.GetTargetFramework(ownerActivity.GetEditingContext());
                if ((targetFramework != null) && (targetFramework.IsLessThan45()))
                {
                    declaredVariables.AddRange(VariableHelper.FindVariablesInScope(ownerActivity));
                    declaredVariables.AddRange(VariableHelper.FindActivityDelegateArgumentsInScope(ownerActivity));
                    if (includeArguments)
                    {
                        declaredVariables.AddRange(VisualBasicEditor.GetVariablesForArguments(ownerActivity.Root));
                    }
                }
                else
                {
                    declaredVariables.AddRange(VisualBasicEditor.GetVariablesInScopeWithShadowing(ownerActivity, includeArguments));
                }
            }

            return declaredVariables;
        }
 void GetPropertyData(PropertyEntry property, out ModelItem activityItem, out ModelItem propertyParentItem, out EditingContext context)
 {
     activityItem = (ModelItem)this.propertyEntryConverter.Convert(property, typeof(ModelItem), false, null);
     propertyParentItem = (ModelItem)this.propertyEntryConverter.Convert(property, typeof(ModelItem), true, null);
     context = activityItem.GetEditingContext();
 }
        public static object ActivityFuncMorphHelper(ModelItem originalValue, ModelProperty newModelProperty)
        {
            Fx.Assert(newModelProperty.PropertyType.GetGenericArguments().Count() == 2, "This should only be applied for ActivityFunc<TArgument, TResult>");
            Type activityFuncArgumentType = newModelProperty.PropertyType.GetGenericArguments()[0];
            Type activityFuncResultType = newModelProperty.PropertyType.GetGenericArguments()[1];
            Type activityFuncType = typeof(ActivityFunc<,>).MakeGenericType(activityFuncArgumentType, activityFuncResultType);
            object activityFunc = Activator.CreateInstance(activityFuncType);
            ModelItem morphed = ModelFactory.CreateItem(originalValue.GetEditingContext(), activityFunc);

            ModelItem originalActivityFuncArgument = originalValue.Properties[PropertyNames.ActionArgument].Value;
            if (originalActivityFuncArgument != null)
            {
                Type argumentType = typeof(DelegateInArgument<>).MakeGenericType(activityFuncArgumentType);
                DelegateInArgument newActivityActionArgument = (DelegateInArgument)Activator.CreateInstance(argumentType);
                newActivityActionArgument.Name = (string)originalActivityFuncArgument.Properties[PropertyNames.NameProperty].Value.GetCurrentValue();
                morphed.Properties[PropertyNames.ActionArgument].SetValue(newActivityActionArgument);
            }

            ModelItem originalActivityFuncResult = originalValue.Properties[PropertyNames.ResultProperty].Value;
            if (originalActivityFuncResult != null)
            {
                Type resultType = typeof(DelegateOutArgument<>).MakeGenericType(activityFuncResultType);
                DelegateOutArgument newActivityActionResult = (DelegateOutArgument)Activator.CreateInstance(resultType);
                newActivityActionResult.Name = (string)originalActivityFuncResult.Properties[PropertyNames.NameProperty].Value.GetCurrentValue();
                morphed.Properties[PropertyNames.ResultProperty].SetValue(newActivityActionResult);
            }

            ModelItem originalActivityActionHandler = originalValue.Properties[PropertyNames.ActionHandler].Value;
            if (originalActivityActionHandler != null)
            {
                morphed.Properties[PropertyNames.ActionHandler].SetValue(originalActivityActionHandler);
                originalValue.Properties[PropertyNames.ActionHandler].SetValue(null);
            }

            return morphed;
        }
Beispiel #13
0
 private static object GetViewState(ModelItem model, string key)
 {
     ViewStateService vss = model.GetEditingContext().Services.GetService<ViewStateService>();
     return vss.RetrieveViewState(model, key);
 }
Beispiel #14
0
        /// <summary>
        /// Replace modelitem by activity
        /// </summary>
        /// <param name="oldModelItem"></param>
        public static ModelItem UpdateModelItem(ModelItem oldModelItem, object newItem)
        {
            Contract.Requires(oldModelItem != null);
            Contract.Requires(newItem != null);

            ModelItem newModelItem = WrapAsModelItem(oldModelItem.GetEditingContext(), newItem, oldModelItem.Parent);

            try
            {
                foreach (ModelProperty modelProperty in oldModelItem.Sources.ToList())
                {
                    modelProperty.SetValue(newModelItem);
                }

                var collectionParents = from parent in oldModelItem.Parents
                                        where parent is ModelItemCollection
                                        select (ModelItemCollection)parent;
                foreach (ModelItemCollection collectionParent in collectionParents.ToList())
                {
                    int index = collectionParent.IndexOf(oldModelItem);
                    collectionParent.Insert(index, newModelItem);
                    collectionParent.Remove(oldModelItem);
                }
            }
            catch
            {
                throw new UserFacingException("Cannot make selected activity as a task.");
            }

            return newModelItem;
        }
Beispiel #15
0
        /// <summary>
        /// Save key object in modelitem's view state
        /// </summary>
        public static void StoreViewState(ModelItem model, string key, object value)
        {
            Contract.Requires(model != null);
            Contract.Requires(key != null);
            Contract.Requires(value != null);

            ViewStateService vss = model.GetEditingContext().Services.GetService<ViewStateService>();
            vss.StoreViewState(model, key, value);
        }