void AddFlowElementsToDesigner(IList <ModelItem> flowElementMICollection, bool addConnectorAfterLoaded = false) { Queue <ModelItem> flowElementsToProcess = new Queue <ModelItem>(); List <UIElement> viewsAdded = new List <UIElement>(); foreach (ModelItem model in flowElementMICollection) { ModelItem itemOnCanvas = GetCorrespondingElementOnCanvas(model); if (!this.modelElement.ContainsKey(itemOnCanvas)) { flowElementsToProcess.Enqueue(model); viewsAdded.Add(ProcessAndGetModelView(itemOnCanvas)); } else if (!this.panel.Children.Contains(this.modelElement[itemOnCanvas])) { flowElementsToProcess.Enqueue(model); viewsAdded.Add(this.modelElement[itemOnCanvas]); } } ModelItem startNodeModelItem = null; List <Tuple <UIElement, UIElement, ModelItem> > elem2elemConnections = new List <Tuple <UIElement, UIElement, ModelItem> >(); List <Tuple <ConnectionPoint, UIElement, ModelItem> > point2elemConnections = new List <Tuple <ConnectionPoint, UIElement, ModelItem> >(); while (flowElementsToProcess.Count > 0) { ModelItem currentMI = flowElementsToProcess.Dequeue(); //Create links for the current FlowNode. //First of all check if this is connected to the start node. if (this.ModelItem.Properties["StartNode"].Value != null && this.ModelItem.Properties["StartNode"].Value.Equals(currentMI)) { startNodeModelItem = currentMI; } if (typeof(FlowStep).IsAssignableFrom(currentMI.ItemType)) { ModelItem linkDest = currentMI.Properties["Next"].Value; if (linkDest != null) { ModelItem src = GetCorrespondingElementOnCanvas(currentMI); ModelItem dest = GetCorrespondingElementOnCanvas(linkDest); if (!modelElement.ContainsKey(dest)) { viewsAdded.Add(ProcessAndGetModelView(dest)); flowElementsToProcess.Enqueue(linkDest); } elem2elemConnections.Add(Tuple.Create(modelElement[src], modelElement[dest], currentMI)); } } else if (typeof(FlowDecision).IsAssignableFrom(currentMI.ItemType)) { ModelItem trueDest = currentMI.Properties["True"].Value; ModelItem falseDest = currentMI.Properties["False"].Value; if (trueDest != null) { ConnectionPoint srcConnectionPoint = FlowchartDesigner.GetTrueConnectionPoint(modelElement[currentMI]); ModelItem trueDestOnCanvas = GetCorrespondingElementOnCanvas(trueDest); if (!modelElement.ContainsKey(trueDestOnCanvas)) { viewsAdded.Add(ProcessAndGetModelView(trueDestOnCanvas)); flowElementsToProcess.Enqueue(trueDest); } point2elemConnections.Add(Tuple.Create(srcConnectionPoint, modelElement[trueDestOnCanvas], currentMI)); } if (falseDest != null) { ConnectionPoint srcConnectionPoint = FlowchartDesigner.GetFalseConnectionPoint(modelElement[currentMI]); ModelItem falseDestOnCanvas = GetCorrespondingElementOnCanvas(falseDest); if (!modelElement.ContainsKey(falseDestOnCanvas)) { viewsAdded.Add(ProcessAndGetModelView(falseDestOnCanvas)); flowElementsToProcess.Enqueue(falseDest); } point2elemConnections.Add(Tuple.Create(srcConnectionPoint, modelElement[falseDestOnCanvas], currentMI)); } } else if (GenericFlowSwitchHelper.IsGenericFlowSwitch(currentMI.ItemType)) { IModelTreeItem modelTreeItem = this.ModelItem as IModelTreeItem; ModelItem defaultCase = currentMI.Properties["Default"].Value; if (defaultCase != null) { ModelItem defaultCaseOnCanvas = GetCorrespondingElementOnCanvas(defaultCase); if (!modelElement.ContainsKey(defaultCaseOnCanvas)) { viewsAdded.Add(ProcessAndGetModelView(defaultCaseOnCanvas)); flowElementsToProcess.Enqueue(defaultCase); } IFlowSwitchLink link = GenericFlowSwitchHelper.CreateFlowSwitchLink(currentMI.ItemType, currentMI, null, true); ModelItem linkModelItem = new FakeModelItemImpl(modelTreeItem.ModelTreeManager, link.GetType(), link, null); link.ModelItem = linkModelItem; elem2elemConnections.Add(Tuple.Create(modelElement[currentMI], modelElement[defaultCaseOnCanvas], linkModelItem)); } Type genericType = currentMI.ItemType.GetGenericArguments()[0]; foreach (ModelItem key in GenericFlowSwitchHelper.GetCaseKeys(currentMI.Properties["Cases"])) { ModelItem destFlowElementMI = GenericFlowSwitchHelper.GetCaseModelItem(currentMI.Properties["Cases"], (key == null) ? null : key.GetCurrentValue()); IFlowSwitchLink link = GenericFlowSwitchHelper.CreateFlowSwitchLink(currentMI.ItemType, currentMI, (key == null) ? null : key.GetCurrentValue(), false); ModelItem linkModelItem = new FakeModelItemImpl(modelTreeItem.ModelTreeManager, link.GetType(), link, null); link.ModelItem = linkModelItem; ModelItem destModelItem = GetCorrespondingElementOnCanvas(destFlowElementMI); if (!modelElement.ContainsKey(destModelItem)) { viewsAdded.Add(ProcessAndGetModelView(destModelItem)); flowElementsToProcess.Enqueue(destFlowElementMI); } elem2elemConnections.Add(Tuple.Create(modelElement[currentMI], modelElement[destModelItem], linkModelItem)); } } else { Fx.Assert(false, "Unknown type of FlowNode"); } } if (!this.startNodeAdded) { panel.Children.Add(this.StartSymbol); this.startNodeAdded = true; } foreach (UIElement view in viewsAdded) { panel.Children.Add(view); } // connection between flownode should be create only after all flownodes have been loaded on the canvas if (addConnectorAfterLoaded) { this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => { if (this.isLoaded) { AddConnectorsToPanel(startNodeModelItem, elem2elemConnections, point2elemConnections); } })); } else { AddConnectorsToPanel(startNodeModelItem, elem2elemConnections, point2elemConnections); } }
bool CreateFlowSwitchLink(ConnectionPoint sourceConnPoint, ModelItem srcFlowElementMI, ModelItem destFlowElementMI, IFlowSwitchLink caseKey, PointCollection connectorViewState, ref string errorMessage) { IModelTreeItem modelTreeItem = this.ModelItem as IModelTreeItem; if ((caseKey != null && caseKey.IsDefaultCase) || (caseKey == null && srcFlowElementMI.Properties["Default"].Value == null)) { IFlowSwitchLink link = GenericFlowSwitchHelper.CreateFlowSwitchLink(srcFlowElementMI.ItemType, srcFlowElementMI, null, true); ModelItem linkModelItem = new FakeModelItemImpl(modelTreeItem.ModelTreeManager, link.GetType(), link, null); link.ModelItem = linkModelItem; if (connectorViewState != null) { this.StoreConnectorViewState(linkModelItem, connectorViewState, sourceConnPoint); } srcFlowElementMI.Properties["Default"].SetValue(destFlowElementMI); } else { ModelProperty casesProp = srcFlowElementMI.Properties["Cases"]; string uniqueCaseName = null; if (caseKey == null) { Type typeArgument = srcFlowElementMI.ItemType.GetGenericArguments()[0]; if (GenericFlowSwitchHelper.CanBeGeneratedUniquely(typeArgument)) { uniqueCaseName = GenericFlowSwitchHelper.GetCaseName(casesProp, typeArgument, out errorMessage); } else { FlowSwitchCaseEditorDialog editor = new FlowSwitchCaseEditorDialog(srcFlowElementMI, this.Context, this, SR.AddNewCase, typeArgument); editor.WindowSizeToContent = SizeToContent.WidthAndHeight; if (!editor.ShowOkCancel()) { return(false); } uniqueCaseName = editor.CaseName; } } else { uniqueCaseName = caseKey.CaseName; } if (string.IsNullOrEmpty(errorMessage)) { IFlowSwitchLink link = GenericFlowSwitchHelper.CreateFlowSwitchLink(srcFlowElementMI.ItemType, srcFlowElementMI, uniqueCaseName, false); ModelItem linkModelItem = new FakeModelItemImpl(modelTreeItem.ModelTreeManager, link.GetType(), link, null); link.ModelItem = linkModelItem; if (connectorViewState != null) { this.StoreConnectorViewState(linkModelItem, connectorViewState, sourceConnPoint); } GenericFlowSwitchHelper.AddCase(srcFlowElementMI.Properties["Cases"], link.CaseObject, destFlowElementMI.GetCurrentValue()); } } return(true); }
void RefreshFlowSwitchLinkModelItem(ModelItem flowSwitchModelItem, Connector connector, bool isDefault) { ModelItem oldLinkModelItem = FlowchartDesigner.GetLinkModelItem(connector); IModelTreeItem modelTreeItem = flowSwitchModelItem as IModelTreeItem; IFlowSwitchLink link = GenericFlowSwitchHelper.CreateFlowSwitchLink(flowSwitchModelItem.ItemType, flowSwitchModelItem, ((IFlowSwitchLink)oldLinkModelItem.GetCurrentValue()).CaseObject, isDefault); ModelItem linkModelItem = new FakeModelItemImpl(modelTreeItem.ModelTreeManager, link.GetType(), link, null); link.ModelItem = linkModelItem; FlowchartDesigner.SetLinkModelItem(connector, linkModelItem); connector.SetBinding(Connector.LabelTextProperty, link.CreateConnectorLabelTextBinding()); Selection currentSelection = this.Context.Items.GetValue <Selection>(); if (currentSelection.SelectedObjects.Contains(oldLinkModelItem)) { Selection.Toggle(this.Context, oldLinkModelItem); Selection.Select(this.Context, linkModelItem); } }