public Connection(Connector source, Connector sink)
 {
     DataContext    = new DiagramRelationshipViewModel();
     this.Source    = source;
     this.Sink      = sink;
     base.Unloaded += new RoutedEventHandler(Connection_Unloaded);
 }
        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed)
            {
                dragStartPoint = null;
                isDragging     = false;
            }
            else if (dragStartPoint.HasValue && isDragging == false)
            {
                Point?currentPosition = new Point?(e.GetPosition(this));
                if (currentPosition.HasValue && (Math.Abs(currentPosition.Value.X - dragStartPoint.Value.X) > 10 || Math.Abs(currentPosition.Value.Y - dragStartPoint.Value.Y) > 10))
                {
                    isDragging = true;
                    DiagramRelationshipViewModel diagramView = new DiagramRelationshipViewModel();
                    diagramView.SourceItem = DataContext as PropertiesViewModel;
                    diagramView.Solution   = diagramView.SourceItem.Solution;
                    diagramView.SourceDiagramEntityViewModel = ParentEntityDiagramControl.DataContext as DiagramEntityViewModel;

                    // custom drag/drop is disabled
                    //ParentDiagramControl.IsDragging = true;
                    //ParentDiagramControl.DragItem = diagramView;
                    //ParentEntityDiagramControl.HandleAdorner();

                    // DoDragDrop would be disabled if custom drag/drop approach with arc drawing adorner is utilized
                    DragDrop.DoDragDrop(this, diagramView, DragDropEffects.Link);
                    e.Handled = true;
                }
            }
        }
        void Connection_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            DiagramRelationshipViewModel view = DataContext as DiagramRelationshipViewModel;

            SourceArrowSymbol = ArrowSymbol.Arrow;
            SinkArrowSymbol   = ArrowSymbol.Arrow;
            string sourceMin = String.Empty, sourceMax = String.Empty, sinkMin = String.Empty, sinkMax = String.Empty;

            if (view != null)
            {
                // set labels
                if (view.RelationshipViewModel.EditRelationship.EntityID != view.RelationshipViewModel.EditRelationship.ReferencedEntityID)
                {
                    sourceMin   = view.RelationshipViewModel.EditRelationship.ItemsMin == -1 ? "*" : view.RelationshipViewModel.EditRelationship.ItemsMin.ToString();
                    sourceMax   = view.RelationshipViewModel.EditRelationship.ItemsMax == -1 ? "*" : view.RelationshipViewModel.EditRelationship.ItemsMax.ToString();
                    sinkMin     = view.RelationshipViewModel.EditRelationship.ReferencedItemsMin == -1 ? "*" : view.RelationshipViewModel.EditRelationship.ReferencedItemsMin.ToString();
                    sinkMax     = view.RelationshipViewModel.EditRelationship.ReferencedItemsMax == -1 ? "*" : view.RelationshipViewModel.EditRelationship.ReferencedItemsMax.ToString();
                    SourceLabel = sourceMin == sourceMax ? sourceMin : sourceMin + ".." + sourceMax;
                    SinkLabel   = sinkMin == sinkMax ? sinkMin : sinkMin + ".." + sinkMax;
                }

                // set many relationship
                if (view.RelationshipViewModel.EditRelationship.ItemsMax == -1 || view.RelationshipViewModel.EditRelationship.ItemsMax > 0)
                {
                    SourceArrowSymbol = ArrowSymbol.DoubleArrow;
                }

                // if primary key relationship, make diamond headed arrows
                bool isPrimaryKeyRelationship = true;
                if (view.RelationshipViewModel.Relationship.PropertyCount == 0 || view.RelationshipViewModel.Relationship.PropertyCount != view.RelationshipViewModel.Relationship.Entity.PrimaryKeyCount)
                {
                    isPrimaryKeyRelationship = false;
                }
                else
                {
                    foreach (RelationshipProperty property in view.RelationshipViewModel.Relationship.RelationshipPropertyList)
                    {
                        if (property.Property.IsPrimaryKeyMember == false || property.ReferencedProperty.IsPrimaryKeyMember == false)
                        {
                            isPrimaryKeyRelationship = false;
                            break;
                        }
                    }
                }
                if (isPrimaryKeyRelationship == true)
                {
                    SourceArrowSymbol = ArrowSymbol.None;
                    SinkArrowSymbol   = ArrowSymbol.Diamond;
                    SourceLabel       = String.Empty;
                    SinkLabel         = String.Empty;
                }
                UpdateSourceAndSink();
            }
        }
        public void UpdateSourceAndSink()
        {
            DiagramRelationshipViewModel view = DataContext as DiagramRelationshipViewModel;

            if (view != null)
            {
                DesignerCanvas       designer      = VisualItemHelper.VisualUpwardSearch <DesignerCanvas>(this) as DesignerCanvas;
                EntityDiagramControl sourceControl = VisualItemHelper.FindChild <EntityDiagramControl>(designer, view.SourceDiagramEntityViewModel) as EntityDiagramControl;
                EntityDiagramControl sinkControl   = VisualItemHelper.FindChild <EntityDiagramControl>(designer, view.SinkDiagramEntityViewModel) as EntityDiagramControl;
                if (sourceControl != null && sinkControl != null)
                {
                    Source = sourceControl.GetBestConnector(sinkControl, false);
                    Sink   = sinkControl.GetBestConnector(sourceControl, true);
                }
            }
        }
        public void HandleCustomDrop()
        {
            // handle "drop" of relationship in custom drag/drop approach with arc drawing adorner
            if (DragItem is DiagramRelationshipViewModel)
            {
                DiagramViewModel             diagramView       = DataContext as DiagramViewModel;
                DiagramEntityViewModel       diagramEntityView = GetMouseOverEntity();
                DiagramRelationshipViewModel dragRelationship  = DragItem as DiagramRelationshipViewModel;
                Point currentPosition = MouseUtilities.GetMousePosition(this);
                if (dragRelationship != null && diagramEntityView != null)
                {
                    dragRelationship.SinkDiagramEntityViewModel = diagramEntityView;
                    dragRelationship.Diagram = diagramView;
                    dialog         = new Window();
                    dialog.Height  = 450 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Width   = 650 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Left    = Math.Max(currentPosition.X, 20);
                    dialog.Top     = Math.Max(currentPosition.Y, 20);
                    dialog.Content = new RelationshipDialogControl();
                    Relationship newRelationship = new Relationship();
                    newRelationship.RelationshipID   = Guid.NewGuid();
                    newRelationship.Solution         = dragRelationship.Solution;
                    newRelationship.ReferencedEntity = dragRelationship.SinkDiagramEntityViewModel.DiagramEntity.Entity;
                    newRelationship.Entity           = dragRelationship.SourceDiagramEntityViewModel.DiagramEntity.Entity;
                    RelationshipViewModel newRelationshipView = new RelationshipViewModel(newRelationship, dragRelationship.Solution);
                    dialog.DataContext = newRelationshipView;
                    dialog.Title       = newRelationshipView.Title;
                    newRelationshipView.RequestClose += new EventHandler(Item_RequestClose);

                    dialog.ShowDialog();
                    if (newRelationshipView.IsOK == true)
                    {
                        dragRelationship.RelationshipViewModel = newRelationshipView;
                        diagramView.CreateRelationship(dragRelationship);
                    }
                    dialog.Close();
                    dialog = null;
                }
            }
            IsDragging = false;
            DragItem   = null;
        }
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            if (HitConnector != null)
            {
                DiagramEntityViewModel sourceView = sourceConnector.DataContext as DiagramEntityViewModel;
                DiagramEntityViewModel sinkView   = HitConnector.DataContext as DiagramEntityViewModel;

                if (DiagramViewModel != null && sourceView != null && sinkView != null)
                {
                    Relationship relationship = new Relationship();
                    relationship.RelationshipID     = Guid.NewGuid();
                    relationship.EntityID           = sourceView.EntityID;
                    relationship.ReferencedEntityID = sinkView.EntityID;
                    // TODO: bring up dialog to set up new entity relationship properties

                    RelationshipViewModel        relationshipViewModel   = new RelationshipViewModel(relationship, sourceView.Solution);
                    DiagramRelationshipViewModel dropDiagramRelationship = new DiagramRelationshipViewModel(sourceView, sinkView, sourceView.Diagram, relationshipViewModel);
                    dropDiagramRelationship.ZIndex = 0;
                    DiagramViewModel.DiagramRelationships.Add(dropDiagramRelationship);
                    DiagramViewModel.ClearSelectedItems();
                    dropDiagramRelationship.IsSelected = true;
                    DiagramViewModel.Refresh(true);
                }
            }
            if (HitDesignerItem != null)
            {
                this.HitDesignerItem.IsDragConnectionOver = false;
            }

            if (this.IsMouseCaptured)
            {
                this.ReleaseMouseCapture();
            }

            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this.designerCanvas);

            if (adornerLayer != null)
            {
                adornerLayer.Remove(this);
            }
        }
Example #7
0
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);

            Point                  currentPosition   = MouseUtilities.GetMousePosition(this);
            DiagramViewModel       diagramView       = ParentCanvas.DataContext as DiagramViewModel;
            DiagramEntityViewModel diagramEntityView = DataContext as DiagramEntityViewModel;

            if (diagramView != null && diagramEntityView != null)
            {
                // try drop as diagram relationship
                DiagramRelationshipViewModel dragRelationship = e.Data.GetData(typeof(DiagramRelationshipViewModel)) as DiagramRelationshipViewModel;
                if (dragRelationship != null)
                {
                    dragRelationship.SinkDiagramEntityViewModel = diagramEntityView;
                    dragRelationship.Diagram = diagramView;
                    dialog         = new Window();
                    dialog.Height  = 450 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Width   = 650 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Left    = Math.Max(currentPosition.X, 20);
                    dialog.Top     = Math.Max(currentPosition.Y, 20);
                    dialog.Content = new RelationshipDialogControl();
                    Relationship newRelationship = new Relationship();
                    newRelationship.RelationshipID   = Guid.NewGuid();
                    newRelationship.Solution         = dragRelationship.Solution;
                    newRelationship.ReferencedEntity = dragRelationship.SinkDiagramEntityViewModel.DiagramEntity.Entity;
                    newRelationship.Entity           = dragRelationship.SourceDiagramEntityViewModel.DiagramEntity.Entity;
                    RelationshipViewModel newRelationshipView = new RelationshipViewModel(newRelationship, dragRelationship.Solution);
                    dialog.DataContext = newRelationshipView;
                    dialog.Title       = newRelationshipView.Title;
                    newRelationshipView.RequestClose += new EventHandler(Item_RequestClose);

                    dialog.ShowDialog();
                    if (newRelationshipView.IsOK == true)
                    {
                        dragRelationship.RelationshipViewModel = newRelationshipView;
                        diagramView.CreateRelationship(dragRelationship);
                    }
                    dialog.Close();
                    dialog    = null;
                    e.Handled = true;
                    return;
                }

                // try drop as collection
                CollectionsViewModel dragCollections = e.Data.GetData(typeof(CollectionsViewModel)) as CollectionsViewModel;
                if (dragCollections != null)
                {
                    dialog         = new Window();
                    dialog.Height  = 450 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Width   = 400 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Left    = Math.Max(currentPosition.X, 20);
                    dialog.Top     = Math.Max(currentPosition.Y, 20);
                    dialog.Content = new CollectionDialogControl();
                    Collection newProperty = new Collection();
                    newProperty.PropertyID       = Guid.NewGuid();
                    newProperty.Solution         = dragCollections.Solution;
                    newProperty.Entity           = dragCollections.Entity;
                    newProperty.ReferencedEntity = diagramEntityView.DiagramEntity.Entity;
                    CollectionViewModel newPropertyView = new CollectionViewModel(newProperty, dragCollections.Solution);
                    dialog.DataContext            = newPropertyView;
                    dialog.Title                  = newPropertyView.Title;
                    newPropertyView.RequestClose += new EventHandler(Item_RequestClose);

                    dialog.ShowDialog();
                    if (newPropertyView.IsOK == true)
                    {
                        dragCollections.AddCollection(newPropertyView);
                    }
                    dialog.Close();
                    dialog    = null;
                    e.Handled = true;
                    return;
                }

                // try drop as property reference
                PropertyReferencesViewModel dragPropertyReferences = e.Data.GetData(typeof(PropertyReferencesViewModel)) as PropertyReferencesViewModel;
                if (dragPropertyReferences != null)
                {
                    dialog         = new Window();
                    dialog.Height  = 450 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Width   = 400 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Left    = Math.Max(currentPosition.X, 20);
                    dialog.Top     = Math.Max(currentPosition.Y, 20);
                    dialog.Content = new PropertyReferenceDialogControl();
                    PropertyReference newProperty = new PropertyReference();
                    newProperty.PropertyID = Guid.NewGuid();
                    newProperty.Solution   = dragPropertyReferences.Solution;
                    newProperty.Entity     = dragPropertyReferences.Entity;
                    PropertyReferenceViewModel newPropertyView = new PropertyReferenceViewModel(newProperty, dragPropertyReferences.Solution);
                    newPropertyView.ReferencedEntityID = diagramEntityView.EntityViewModel.EntityID;
                    newPropertyView.RefreshProperties();
                    dialog.DataContext            = newPropertyView;
                    dialog.Title                  = newPropertyView.Title;
                    newPropertyView.RequestClose += new EventHandler(Item_RequestClose);

                    dialog.ShowDialog();
                    if (newPropertyView.IsOK == true)
                    {
                        dragPropertyReferences.AddPropertyReference(newPropertyView);
                    }
                    dialog.Close();
                    dialog    = null;
                    e.Handled = true;
                    return;
                }

                // try drop as entity reference
                EntityReferencesViewModel dragEntityReferences = e.Data.GetData(typeof(EntityReferencesViewModel)) as EntityReferencesViewModel;
                if (dragEntityReferences != null)
                {
                    dialog         = new Window();
                    dialog.Height  = 450 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Width   = 400 * UserSettingsHelper.Instance.ControlSize;
                    dialog.Left    = Math.Max(currentPosition.X, 20);
                    dialog.Top     = Math.Max(currentPosition.Y, 20);
                    dialog.Content = new EntityReferenceDialogControl();
                    EntityReference newProperty = new EntityReference();
                    newProperty.PropertyID       = Guid.NewGuid();
                    newProperty.Solution         = dragEntityReferences.Solution;
                    newProperty.Entity           = dragEntityReferences.Entity;
                    newProperty.ReferencedEntity = diagramEntityView.DiagramEntity.Entity;
                    EntityReferenceViewModel newPropertyView = new EntityReferenceViewModel(newProperty, dragEntityReferences.Solution);
                    dialog.DataContext            = newPropertyView;
                    dialog.Title                  = newPropertyView.Title;
                    newPropertyView.RequestClose += new EventHandler(Item_RequestClose);

                    dialog.ShowDialog();
                    if (newPropertyView.IsOK == true)
                    {
                        dragEntityReferences.AddEntityReference(newPropertyView);
                    }
                    dialog.Close();
                    dialog    = null;
                    e.Handled = true;
                    return;
                }
            }
        }