|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows;
namespace NetworkModel
{
/// <summary>
/// Defines a connection between two connectors (aka connection points) of two nodes.
/// </summary>
public sealed class ConnectionViewModel : AbstractModelBase
{
#region Internal Data Members
/// <summary>
/// The source connector the connection is attached to.
/// </summary>
private ConnectorViewModel sourceConnector = null;
/// <summary>
/// The destination connector the connection is attached to.
/// </summary>
private ConnectorViewModel destConnector = null;
/// <summary>
/// The source and dest hotspots used for generating connection points.
/// </summary>
private Point sourceConnectorHotspot;
private Point destConnectorHotspot;
/// <summary>
/// Points that make up the connection.
/// </summary>
private PointCollection points = null;
#endregion Internal Data Members
/// <summary>
/// The source connector the connection is attached to.
/// </summary>
public ConnectorViewModel SourceConnector
{
get
{
return sourceConnector;
}
set
{
if (sourceConnector == value)
{
return;
}
if (sourceConnector != null)
{
sourceConnector.AttachedConnections.Remove(this);
sourceConnector.HotspotUpdated -= new EventHandler<EventArgs>(sourceConnector_HotspotUpdated);
}
sourceConnector = value;
if (sourceConnector != null)
{
sourceConnector.AttachedConnections.Add(this);
sourceConnector.HotspotUpdated += new EventHandler<EventArgs>(sourceConnector_HotspotUpdated);
this.SourceConnectorHotspot = sourceConnector.Hotspot;
}
OnPropertyChanged("SourceConnector");
OnConnectionChanged();
}
}
/// <summary>
/// The destination connector the connection is attached to.
/// </summary>
public ConnectorViewModel DestConnector
{
get
{
return destConnector;
}
set
{
if (destConnector == value)
{
return;
}
if (destConnector != null)
{
destConnector.AttachedConnections.Remove(this);
destConnector.HotspotUpdated -= new EventHandler<EventArgs>(destConnector_HotspotUpdated);
}
destConnector = value;
if (destConnector != null)
{
destConnector.AttachedConnections.Add(this);
destConnector.HotspotUpdated += new EventHandler<EventArgs>(destConnector_HotspotUpdated);
this.DestConnectorHotspot = destConnector.Hotspot;
}
OnPropertyChanged("DestConnector");
OnConnectionChanged();
}
}
/// <summary>
/// The source and dest hotspots used for generating connection points.
/// </summary>
public Point SourceConnectorHotspot
{
get
{
return sourceConnectorHotspot;
}
set
{
sourceConnectorHotspot = value;
ComputeConnectionPoints();
OnPropertyChanged("SourceConnectorHotspot");
}
}
public Point DestConnectorHotspot
{
get
{
return destConnectorHotspot;
}
set
{
destConnectorHotspot = value;
ComputeConnectionPoints();
OnPropertyChanged("DestConnectorHotspot");
}
}
/// <summary>
/// Points that make up the connection.
/// </summary>
public PointCollection Points
{
get
{
return points;
}
set
{
points = value;
OnPropertyChanged("Points");
}
}
/// <summary>
/// Event fired when the connection has changed.
/// </summary>
public event EventHandler<EventArgs> ConnectionChanged;
#region Private Methods
/// <summary>
/// Raises the 'ConnectionChanged' event.
/// </summary>
private void OnConnectionChanged()
{
if (ConnectionChanged != null)
{
ConnectionChanged(this, EventArgs.Empty);
}
}
/// <summary>
/// Event raised when the hotspot of the source connector has been updated.
/// </summary>
private void sourceConnector_HotspotUpdated(object sender, EventArgs e)
{
this.SourceConnectorHotspot = this.SourceConnector.Hotspot;
}
/// <summary>
/// Event raised when the hotspot of the dest connector has been updated.
/// </summary>
private void destConnector_HotspotUpdated(object sender, EventArgs e)
{
this.DestConnectorHotspot = this.DestConnector.Hotspot;
}
/// <summary>
/// Rebuild connection points.
/// </summary>
private void ComputeConnectionPoints()
{
PointCollection computedPoints = new PointCollection();
computedPoints.Add(this.SourceConnectorHotspot);
double deltaX = Math.Abs(this.DestConnectorHotspot.X - this.SourceConnectorHotspot.X);
double deltaY = Math.Abs(this.DestConnectorHotspot.Y - this.SourceConnectorHotspot.Y);
if (deltaX > deltaY)
{
double midPointX = this.SourceConnectorHotspot.X + ((this.DestConnectorHotspot.X - this.SourceConnectorHotspot.X) / 2);
computedPoints.Add(new Point(midPointX, this.SourceConnectorHotspot.Y));
computedPoints.Add(new Point(midPointX, this.DestConnectorHotspot.Y));
}
else
{
double midPointY = this.SourceConnectorHotspot.Y + ((this.DestConnectorHotspot.Y - this.SourceConnectorHotspot.Y) / 2);
computedPoints.Add(new Point(this.SourceConnectorHotspot.X, midPointY));
computedPoints.Add(new Point(this.DestConnectorHotspot.X, midPointY));
}
computedPoints.Add(this.DestConnectorHotspot);
computedPoints.Freeze();
this.Points = computedPoints;
}
#endregion Private Methods
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Software craftsman | Author | Writing rapidfullstackdevelopment.com - Posting about how to survive and flourish as a software developer
Follow on Twitter for news and updates: https://twitter.com/codecapers
I'm writing a new book: Rapid Fullstack Development. Learn from my years of experience and become a better developer.
My second book, Bootstrapping Microservices, is a practical and project-based guide to building distributed applications with microservices.
My first book Data Wrangling with JavaScript is a comprehensive overview of working with data in JavaScript.
Data-Forge Notebook is my notebook-style application for data transformation, analysis and transformation in JavaScript.
I have a long history in software development with many years in apps, web apps, backends, serious games, simulations and VR. Making technology work for business is what I do: building bespoke software solutions that span multiple platforms.
I have years of experience managing development teams, preparing technical strategies and creation of software products. I can explain complicated technology to senior management. I have delivered cutting-edge products in fast-paced and high-pressure environments. I know how to focus and prioritize to get the important things done.
Author
- Rapid Fullstack Development
- Bootstrapping Microservices
- Data Wrangling with JavaScript
Creator of Market Wizard
- https://www.market-wizard.com.au/
Creator of Data-Forge and Data-Forge Notebook
- http://www.data-forge-js.com
- http://www.data-forge-notebook.com
Web
- www.codecapers.com.au
Open source
- https://github.com/ashleydavis
- https://github.com/data-forge
- https://github.com/data-forge-notebook
Skills
- Quickly building MVPs for startups
- Understanding how to get the most out of technology for business
- Developing technical strategies
- Management and coaching of teams & projects
- Microservices, devops, mobile and fullstack software development