#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Drawing; using System.Linq; using HeuristicLab.Core.Networks; using HeuristicLab.Visualization; namespace HeuristicLab.Networks.Views.NetworkVisualization { public class PortRectangle : FixedSizeRectangle, INetworkItemPrimitive { private const int size = 10; public override Size Size { get { return new Size((int)Math.Round(size * Chart.WorldToPixelRatio.Width), (int)Math.Round(size * Chart.WorldToPixelRatio.Height)); } } protected IPort networkItem; public IPort NetworkItem { get { return networkItem; } set { if (networkItem == value) return; if (networkItem != null) DeregisterNetworkItemEvents(); networkItem = value; if (networkItem != null) RegisterNetworkItemEvents(); OnNetworkItemChanged(); } } public PortRectangle(NodeRectangle nodeRectangle, IPort port) : base(nodeRectangle.Chart, PointD.Empty, Size.Empty, Pens.Black, Brushes.Red) { NetworkItem = port; } protected virtual void RegisterNetworkItemEvents() { networkItem.NameChanged += NetworkItemNameChanged; var connectablePort = networkItem as IConnectablePort; if (connectablePort != null) { connectablePort.ConnectedPortChanged += Port_ConnectedPortChanged; } } protected virtual void DeregisterNetworkItemEvents() { networkItem.NameChanged -= NetworkItemNameChanged; var connectablePort = networkItem as IConnectablePort; if (connectablePort != null) { connectablePort.ConnectedPortChanged -= Port_ConnectedPortChanged; } } #region Events public event EventHandler NetworkItemChanged; protected virtual void OnNetworkItemChanged() { UpdateToolTip(); var handler = NetworkItemChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler NodeRectangleChanged; protected virtual void OnNodeRectangleChanged() { var handler = NodeRectangleChanged; if (handler != null) handler(this, EventArgs.Empty); } #endregion #region Event Handlers private void NetworkItemNameChanged(object sender, EventArgs e) { UpdateToolTip(); } private void Port_ConnectedPortChanged(object sender, EventArgs e) { var connectablePort = (IConnectablePort)networkItem; var connectionLines = Chart.Group.GetAllPrimitives(Point).OfType(); var outgoingLines = connectionLines.Where(x => x.StartPortRectangle == this); foreach (var l in outgoingLines) Chart.Group.Remove(l); var connectedPort = connectablePort.ConnectedPort as IConnectablePort; if (connectedPort == null) return; var connectedNode = connectedPort.Parent; if (connectedNode == null) return; var nodeRectangles = Chart.Group.Primitives.OfType(); PortRectangle endPortRectangle = null; foreach (var nodeRectangle in nodeRectangles) { endPortRectangle = nodeRectangle.GetPortRectangle(connectedPort); if (endPortRectangle != null) break; } if (endPortRectangle == null) return; var connectionShape = new ConnectionLine(Chart, this, endPortRectangle); Chart.Group.Add(connectionShape); } #endregion #region Helpers private void UpdateToolTip() { ToolTipText = networkItem.Name; } #endregion } }