コード例 #1
0
        public static bool ValidateCaseKey(object obj, ModelProperty casesProp, Type genericType, out string reason)
        {
            reason = string.Empty;
            string key = GenericFlowSwitchHelper.GetString(obj, genericType);

            if (GenericFlowSwitchHelper.CheckEquality(obj, genericType))
            {
                if (GenericFlowSwitchHelper.ContainsCaseKey(casesProp, obj))
                {
                    reason = string.Format(CultureInfo.CurrentCulture, SR.DuplicateCaseKey, key);
                    return(false);
                }
                return(true);
            }
            else
            {
                reason = string.Format(CultureInfo.CurrentUICulture, SR.EqualityError, genericType.Name);
                return(false);
            }
        }
コード例 #2
0
        void OnIsDefaultPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            bool isUndoRedoInProgress = this.IsUndoRedoInProgress();

            if (!this.internalDefaultCaseChange && !isUndoRedoInProgress)
            {
                bool value    = (bool)e.NewValue;
                bool oldValue = (bool)e.OldValue;

                if (value)
                {
                    if (object.Equals(this.flowSwitchModelItem.Properties["Default"].Value, null))
                    {
                        using (EditingScope es = (EditingScope)this.flowSwitchModelItem.BeginEdit(SR.FlowSwitchCaseRenameEditingScopeDesc))
                        {
                            ModelItem flowNodeMI = GenericFlowSwitchHelper.GetCaseModelItem(this.flowSwitchModelItem.Properties["Cases"], this.CaseObject);
                            GenericFlowSwitchHelper.RemoveCase(this.flowSwitchModelItem.Properties["Cases"], this.CaseObject);
                            this.flowSwitchModelItem.Properties["Default"].SetValue(flowNodeMI);
                            this.UpdateViewState(this.CaseName + CaseViewStateKeyAppendString, DefaultConnectorViewStateKey);
                            this.internalChange = true;
                            es.Complete();
                        }
                    }
                    else
                    {
                        this.internalDefaultCaseChange = true;
                        this.IsDefaultCase             = oldValue;
                        throw FxTrace.Exception.AsError(new InvalidOperationException(SR.DefaultCaseExists));
                    }
                }
                else
                {
                    if (oldValue)
                    {
                        using (EditingScope es = (EditingScope)this.flowSwitchModelItem.BeginEdit(SR.FlowSwitchCaseRenameEditingScopeDesc))
                        {
                            ModelItem defaultCase  = this.flowSwitchModelItem.Properties["Default"].Value;
                            object    uniqueCase   = null;
                            string    errorMessage = string.Empty;
                            Type      typeArgument = typeof(T);
                            if (GenericFlowSwitchHelper.CanBeGeneratedUniquely(typeArgument))
                            {
                                string caseName = GenericFlowSwitchHelper.GetCaseName(this.flowSwitchModelItem.Properties["Cases"], typeArgument, out errorMessage);
                                if (!string.IsNullOrEmpty(errorMessage))
                                {
                                    this.internalDefaultCaseChange = true;
                                    this.IsDefaultCase             = oldValue;
                                    throw FxTrace.Exception.AsError(new InvalidOperationException(errorMessage));
                                }
                                uniqueCase = GenericFlowSwitchHelper.GetObject(caseName, typeArgument);
                            }
                            else
                            {
                                FlowSwitchCaseEditorDialog editor = new FlowSwitchCaseEditorDialog(this.flowSwitchModelItem, ((WorkflowViewElement)this.flowSwitchModelItem.View).Context, this.flowSwitchModelItem.View, SR.ChangeCaseValue, this.flowSwitchModelItem.ItemType.GetGenericArguments()[0]);
                                editor.WindowSizeToContent = SizeToContent.WidthAndHeight;

                                if (!editor.ShowOkCancel())
                                {
                                    this.internalDefaultCaseChange = true;
                                    this.IsDefaultCase             = oldValue;
                                    return;
                                }
                                uniqueCase = editor.Case;
                                if (GenericFlowSwitchHelper.ContainsCaseKey(this.flowSwitchModelItem.Properties["Cases"], uniqueCase))
                                {
                                    this.internalDefaultCaseChange = true;
                                    this.IsDefaultCase             = oldValue;
                                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidFlowSwitchCaseMessage));
                                }
                            }

                            this.flowSwitchModelItem.Properties["Default"].SetValue(null);
                            this.flowSwitchModelItem.Properties[FlowSwitchLabelFeature.DefaultCaseDisplayNamePropertyName].SetValue(FlowSwitchLabelFeature.DefaultCaseDisplayNameDefaultValue);

                            this.internalChange = true;
                            if (typeof(string) != typeof(T))
                            {
                                this.ModelItem.Properties["Case"].SetValue(uniqueCase);
                                GenericFlowSwitchHelper.AddCase(this.flowSwitchModelItem.Properties["Cases"], uniqueCase, defaultCase.GetCurrentValue());
                            }
                            else
                            {
                                this.ModelItem.Properties["Case"].SetValue(uniqueCase);
                                GenericFlowSwitchHelper.AddCase(this.flowSwitchModelItem.Properties["Cases"], uniqueCase, defaultCase.GetCurrentValue());
                            }
                            this.UpdateViewState(DefaultConnectorViewStateKey, GenericFlowSwitchHelper.GetString(uniqueCase, typeof(T)) + CaseViewStateKeyAppendString);
                            es.Complete();
                            this.internalChange = false;
                        }
                        this.internalDefaultCaseChange = false;
                    }
                }
            }
            this.internalDefaultCaseChange = false;
        }
        void OnViewStateChanged(object sender, ViewStateChangedEventArgs e)
        {
            Fx.Assert(this.panel != null, "This code should not be hit if panel is null");
            Fx.Assert(e.ParentModelItem != null, "ViewState should be associated with some modelItem");
            Connector changedConnector = null;

            if (e.ParentModelItem == this.ModelItem)
            {
                if (string.Equals(e.Key, FlowchartSizeFeature.WidthPropertyName, StringComparison.Ordinal))
                {
                    this.FlowchartWidth = (double)TypeDescriptor.GetProperties(this.ModelItem)[FlowchartSizeFeature.WidthPropertyName].GetValue(this.ModelItem);
                }
                else if (string.Equals(e.Key, FlowchartSizeFeature.HeightPropertyName, StringComparison.Ordinal))
                {
                    this.FlowchartHeight = (double)TypeDescriptor.GetProperties(this.ModelItem)[FlowchartSizeFeature.HeightPropertyName].GetValue(this.ModelItem);
                }
            }
            if ((IsFlowNode(e.ParentModelItem) || this.ModelItem.Equals(e.ParentModelItem)) && !this.internalViewStateChange)
            {
                ModelItem itemOnCanvas = this.GetCorrespondingElementOnCanvas(e.ParentModelItem);
                if (this.modelElement.ContainsKey(itemOnCanvas))
                {
                    if (e.Key.Equals(shapeLocation))
                    {
                        if (e.NewValue != null)
                        {
                            FreeFormPanel.SetLocation(this.modelElement[itemOnCanvas], (Point)e.NewValue);
                            this.panel.InvalidateMeasure();
                            if (e.OldValue != null)
                            {
                                this.shapeLocations.Remove((Point)e.OldValue);
                            }
                            this.shapeLocations.Add((Point)e.NewValue);
                        }
                    }
                    else
                    {
                        if (this.ModelItem.Equals(e.ParentModelItem) &&
                            e.Key.Equals(ConnectorViewStateKey))
                        {
                            changedConnector = this.GetLinkOnCanvas(e.ParentModelItem, e.ParentModelItem.Properties["StartNode"].Value, "StartNode");
                        }
                        else if (typeof(FlowStep).IsAssignableFrom(e.ParentModelItem.ItemType) &&
                                 e.Key.Equals(ConnectorViewStateKey))
                        {
                            changedConnector = this.GetLinkOnCanvas(e.ParentModelItem, e.ParentModelItem.Properties["Next"].Value, "Next");
                        }
                        else if (typeof(FlowDecision).IsAssignableFrom(e.ParentModelItem.ItemType))
                        {
                            if (e.Key.Equals(TrueConnectorViewStateKey))
                            {
                                changedConnector = this.GetLinkOnCanvas(e.ParentModelItem, e.ParentModelItem.Properties["True"].Value, "True");
                            }
                            else if (e.Key.Equals(FalseConnectorViewStateKey))
                            {
                                changedConnector = this.GetLinkOnCanvas(e.ParentModelItem, e.ParentModelItem.Properties["False"].Value, "False");
                            }
                        }
                        else if (GenericFlowSwitchHelper.IsGenericFlowSwitch(e.ParentModelItem.ItemType))
                        {
                            if (e.Key.Equals(FlowchartDesigner.FlowSwitchDefaultViewStateKey, StringComparison.CurrentCulture))
                            {
                                changedConnector = this.GetLinkOnCanvas(e.ParentModelItem, e.ParentModelItem.Properties["Default"].Value, e.Key);
                            }
                            else if (e.Key.EndsWith(CaseViewStateKeyAppendString, StringComparison.CurrentCulture))
                            {
                                string switchCaseName = e.Key.Substring(0, e.Key.Length - CaseViewStateKeyAppendString.Length);
                                object switchCase     = switchCaseName;
                                Type   genericType    = e.ParentModelItem.ItemType.GetGenericArguments()[0];
                                switchCase = GenericFlowSwitchHelper.GetObject(switchCaseName, genericType);

                                if (GenericFlowSwitchHelper.ContainsCaseKey(e.ParentModelItem.Properties["Cases"], switchCase))
                                {
                                    //Prepending with GenericFlowSwitchHelper.FlowSwitchCasesKeyIdentifier to differentiate between the property Default and the key Default.
                                    changedConnector = this.GetLinkOnCanvas(e.ParentModelItem, GenericFlowSwitchHelper.GetCaseModelItem(e.ParentModelItem.Properties["Cases"], switchCase), GenericFlowSwitchHelper.FlowSwitchCasesKeyIdentifier + switchCase);
                                }
                            }
                        }
                    }
                }
            }
            if (changedConnector != null)
            {
                if (e.NewValue != null)
                {
                    Fx.Assert(e.NewValue is PointCollection, "e.NewValue is not PointCollection");
                    changedConnector.Points = e.NewValue as PointCollection;
                    this.panel.RemoveConnectorEditor();
                    this.panel.InvalidateMeasure();
                }
            }
        }
コード例 #4
0
        public static string GenericGetCaseName <T>(ModelItemCollection collection, out string errorMessage)
        {
            int  maxName = 100000;
            Type type    = typeof(T);

            errorMessage = string.Empty;
            if (typeof(string).IsAssignableFrom(type))
            {
                string caseName = "caseN";
                for (int i = 1; i <= maxName; i++)
                {
                    caseName = string.Format(CultureInfo.InvariantCulture, SR.CaseFormat, i);
                    if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, caseName))
                    {
                        break;
                    }
                }
                return(caseName);
            }
            else if (GenericFlowSwitchHelper.IsIntegralType(type))
            {
                if (type == typeof(sbyte))
                {
                    sbyte maxCount = (sbyte.MaxValue < maxName) ? sbyte.MaxValue : (sbyte)maxName;
                    for (sbyte i = 0; i <= maxCount; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                else if (type == typeof(byte))
                {
                    byte maxCount = (byte.MaxValue < maxName) ? byte.MaxValue : (byte)maxName;
                    for (byte i = 0; i <= maxCount; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                else if (type == typeof(char))
                {
                    char maxCount = unchecked ((char)maxName);
                    for (char i = (char)48; i <= maxCount; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                else if (type == typeof(short))
                {
                    short maxCount = (short)maxName;
                    for (short i = 0; i <= maxCount; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                else if (type == typeof(ushort))
                {
                    ushort maxCount = (ushort)maxName;
                    for (ushort i = 0; i <= maxCount; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                else if (type == typeof(int))
                {
                    for (int i = 0; i <= maxName; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                else if (type == typeof(uint))
                {
                    for (uint i = 0; i <= (uint)maxName; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                else if (type == typeof(long))
                {
                    for (long i = 0; i <= (long)maxName; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                else if (type == typeof(ulong))
                {
                    for (ulong i = 0; i <= (ulong)maxName; i++)
                    {
                        if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, i))
                        {
                            return(GenericFlowSwitchHelper.GetString(i, type));
                        }
                    }
                }
                errorMessage = SR.InvalidFlowSwitchCaseMessage;
                return(string.Empty);
            }
            else if (type.IsEnum)
            {
                Array array = type.GetEnumValues();
                foreach (object value in array)
                {
                    if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, value))
                    {
                        return(GenericFlowSwitchHelper.GetString(value, type));
                    }
                }
                errorMessage = SR.InvalidFlowSwitchCaseMessage;
                return(string.Empty);
            }
            else if (type == typeof(bool))
            {
                if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, true))
                {
                    return(GenericFlowSwitchHelper.GetString(true, type));
                }
                else if (!GenericFlowSwitchHelper.ContainsCaseKey(collection, false))
                {
                    return(GenericFlowSwitchHelper.GetString(false, type));
                }
                errorMessage = SR.InvalidFlowSwitchCaseMessage;
                return(string.Empty);
            }
            return(string.Empty);
        }