/// <summary> /// Disconnects from. /// </summary> /// <param name="item">The item.</param> /// <param name="sholdRemove">if set to <c>true</c> [shold remove].</param> public void DisconnectFrom(IDiagramItemViewModel item, out bool sholdRemove) { IsRemoved = true; IsConnected = false; sholdRemove = false; if (_item != null) { _item.Connector.Connection = null; _item.Connector.ConnectedTo = null; } }
/// <summary> /// Start the drag operation. /// </summary> private void Start() { diagramDesigner = DiagramHelper.GetDiagramDesigner(this); diagramDesignerVM = diagramDesigner.DataContext as IDiagramViewModel; selectedItems = new List <DiagramDesignerItem>(); selectedItemsVM = new List <IDiagramItemViewModel>(); List <ISelectable> selection = diagramDesigner.SelectionService.CurrentSelection; foreach (ISelectable item in selection) { if (item is DiagramDesignerItem) { if (!(item as DiagramDesignerItem).IsDiagramLink) { selectedItems.Add(item as DiagramDesignerItem); } } } // create control to display on the drag adorner Canvas canvas = new Canvas(); foreach (DiagramDesignerItem item in selectedItems) { IDiagramItemViewModel designerItem = item.DataContext as IDiagramItemViewModel; if (designerItem == null) { continue; } selectedItemsVM.Add(designerItem); Rectangle r = new Rectangle(); r.Stroke = new SolidColorBrush(Colors.Black); r.StrokeDashArray = new DoubleCollection(); r.StrokeDashArray.Add(3); r.StrokeDashArray.Add(6); r.StrokeThickness = 1.0; r.Width = item.ActualWidth; r.Height = item.ActualHeight; r.Margin = new System.Windows.Thickness(designerItem.AbsoluteLocation.X, designerItem.AbsoluteLocation.Y, 0, 0); canvas.Children.Add(r); } // create drag adorner if (diagramDesigner != null) { AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(diagramDesigner); if (adornerLayer != null) { this.dragAdorner = new DragAdorner(diagramDesigner, canvas, false, 1.0); if (this.dragAdorner != null) { adornerLayer.Add(this.dragAdorner); //e.Handled = true; } } } //e.Handled = false; }
/// <summary> /// Determines whether [is connected to] [the specified item]. /// </summary> /// <param name="item">The item.</param> /// <returns><c>true</c> if [is connected to] [the specified item]; otherwise, <c>false</c>.</returns> public bool IsConnectedTo(IDiagramItemViewModel item) { return _diagramViewModel.Items.Any(x => x.IsConnectedTo(this) && x.IsConnectedTo(item)); }
/// <summary> /// Connects to. /// </summary> /// <param name="item">The item.</param> public void ConnectTo(IDiagramItemViewModel item) { IsConnected = true; // remove all destination incomming connections var connections = (from c in _diagramViewModel.Items where c.IsConnectedTo(item) && !c.IsConnectedTo(this) select c).ToArray(); foreach (var c in connections) { _diagramViewModel.DeleteItem(c); bool shouldRemove; c.DisconnectFrom(item, out shouldRemove); } var destConnector = item as ExpressionConnectorViewModel; if (destConnector != null) { destConnector.IsConnected = true; } }
/// <summary> /// Creates the connection to. /// </summary> /// <param name="destinationItem">The destination item.</param> /// <returns>IDiagramItemViewModel.</returns> /// <exception cref="System.ArgumentNullException">destinationItem</exception> public IDiagramItemViewModel CreateConnectionTo(IDiagramItemViewModel destinationItem) { if (destinationItem == null) throw new ArgumentNullException("destinationItem"); return new ConnectionViewModel<ExpressionConnection>( (ExpressionConnection)destinationItem.Item.CreateConnection(Item, destinationItem.Item), this, (IConnectableItemViewModel)destinationItem, _diagramViewModel ); }
/// <summary> /// Determines whether this instance [can connect to] the specified destination item. /// </summary> /// <param name="destinationItem">The destination item.</param> /// <param name="items">The items.</param> /// <returns>ConnectivityState</returns> public ConnectivityState CanConnectTo(IDiagramItemViewModel destinationItem, ICollection<IDiagramItemViewModel> items) { var connector = destinationItem as IConnectorViewModel; var canConnect = connector != null && destinationItem != this && connector.ConnectorType != ConnectorType && connector.OwnerNode != OwnerNode && !IsConnectedTo(destinationItem); if (canConnect) { var inboundConnector = (ExpressionConnector)(ConnectorType == ConnectorType.In ? this : connector).Item; var outboundConnector = (ExpressionConnector)(ConnectorType == ConnectorType.In ? connector : this).Item; if (AreTypesCompatible(outboundConnector.DataType, inboundConnector.DataType)) { if (inboundConnector.Connector.Validator != null) { var connectivity = inboundConnector.Connector.Validator.CanConnect( (IConnectorOut) outboundConnector.Connector, (IConnectorIn) inboundConnector.Connector); if (!connectivity.CanConnect) return connectivity; } if (outboundConnector.Connector.Validator != null) { var connectivity = outboundConnector.Connector.Validator.CanConnect( (IConnectorOut) outboundConnector.Connector, (IConnectorIn) inboundConnector.Connector); if (!connectivity.CanConnect) return connectivity; } if (outboundConnector.DataType == NodeDataType.Null && !inboundConnector.Connector.IsNullable) return ConnectivityState.Refuse(); return ConnectivityState.Allow(); } else { return ConnectivityState.Refuse( string.Format(CultureInfo.InvariantCulture, LanguageService.Translate("Warn_ExpressionBuilder_TypesAreIncompatible"), outboundConnector.DataType, inboundConnector.DataType)); } } return ConnectivityState.Refuse(); }
/// <summary> /// Unwires the specified item to remove. /// </summary> /// <param name="itemToRemove">The item to remove.</param> /// <param name="removeCandidates">The remove candidates.</param> private void Unwire(IDiagramItemViewModel itemToRemove, IList<IDiagramItemViewModel> removeCandidates) { if (!Enabled) return; foreach (var item in Items) { if (removeCandidates.Contains(item)) { continue; } if (item.IsConnectedTo(itemToRemove)) { bool sholdRemove; item.DisconnectFrom(itemToRemove, out sholdRemove); if (sholdRemove) { item.IsRemoved = true; removeCandidates.Add(item); } Unwire(item, removeCandidates); } } }
/// <summary> /// Deletes the item. /// </summary> /// <param name="item">The item.</param> /// <exception cref="System.ArgumentNullException">item</exception> public void DeleteItem(IDiagramItemViewModel item) { if (item == null) throw new ArgumentNullException("item"); if (!Enabled || AddRemoveDisabled) return; item.IsRemoved = true; var removeCandidates = new List<IDiagramItemViewModel>(); removeCandidates.Add(item); Unwire(item, removeCandidates); foreach (var itemToRemove in removeCandidates) { DeselectItems(); Items.Remove(itemToRemove); } }
/// <summary> /// Adds the expression item. /// </summary> /// <param name="item">The diagram item.</param> private void AddExpressionItem(IDiagramItemViewModel item) { var node = item as INodeViewModel; if (node != null) { var constant = node.Item as ConstantExpressionNode; if (constant != null) { var dlg = ConstantDialog.CreateExport(); dlg.Value.StateList.Clear(); dlg.Value.StateList.AddRange(Diagram.StateList); ThePopupFactory.Popup() .SetPosition(PopupPosition.Center) .SetIsModal(true) .SetCustomViewModel(dlg.Value) .SetCaption("Constant") .Show(() => { if (!dlg.Value.Validate()) { constant.Value = dlg.Value.Value; ((ExpressionConnector)constant.ConnectorOut.Item).DataType = dlg.Value.DataType; Diagram.RaiseForceRelayout(); Diagram.RaisePropertyChanged(); } else { Diagram.Items.Remove(node); ThePopupFactory.Popup() .SetIsModal(true) .SetPosition(PopupPosition.Center) .SetCaption("Validation Error") .Message("The value supplied is invalid, please try again.") .Show(); } dlg.Dispose(); }); } var converter = node.Item as ConverterExpressionNode; if (converter != null) { var dlg = ConverterDialog.CreateExport(); ThePopupFactory.Popup() .SetPosition(PopupPosition.Center) .SetIsModal(true) .SetCustomViewModel(dlg.Value) .SetCaption("Converter") .Show(() => { if (!dlg.Value.Validate()) { ((ExpressionConnector)converter.ConnectorIn.Item).DataType = dlg.Value.DataTypeIn; ((ExpressionConnector)converter.ConnectorOut.Item).DataType = dlg.Value.DataTypeOut; Diagram.RaiseForceRelayout(); converter.Refresh(); } else { Diagram.Items.Remove(node); ThePopupFactory.Popup() .SetIsModal(true) .SetPosition(PopupPosition.Center) .SetCaption("Validation Error") .Message("The value supplied is invalid, please try again.") .Show(); } dlg.Dispose(); }); return; } var userDefinedTypeConnectors = node.GetConnectors().Where(c => ((ExpressionConnector)c.Item).DataType == NodeDataType.UserDefined).ToArray(); if (userDefinedTypeConnectors.Length > 0) { var dlg = ConditionDialog.CreateExport(); ThePopupFactory.Popup() .SetPosition(PopupPosition.Center) .SetIsModal(true) .SetCustomViewModel(dlg.Value) .SetCaption("Value type") .Show(() => { foreach (var udtConnector in userDefinedTypeConnectors) { ((ExpressionConnector)udtConnector.Item).DataType = dlg.Value.DataType; } }); } } }