Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/Model/OperatorShape.cs @ 2872

Last change on this file since 2872 was 2872, checked in by mkommend, 14 years ago

corrected handling of connections (ticket #867)

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using Netron.Diagramming.Core;
27using System.Drawing;
28
29namespace HeuristicLab.Operators.Views.GraphVisualization {
30  public class OperatorShape : ClassShape {
31
32    public OperatorShape()
33      : base() {
34      this.additionalConnectors = new List<IConnector>();
35    }
36
37    private List<IConnector> additionalConnectors;
38    public IEnumerable<string> AdditionalConnectorNames {
39      get { return this.additionalConnectors.Select(c => c.Name); }
40    }
41
42    private IConnector predecessor;
43    public IConnector Predecessor {
44      get { return this.predecessor; }
45    }
46
47    private IConnector successor;
48    public IConnector Successor {
49      get { return this.successor; }
50    }
51
52    private IConnector CreateConnector(string connectorName, Point location) {
53      Connector connector = new Connector(location, this.Model);
54      connector.ConnectorStyle = ConnectorStyle.Square;
55      connector.Parent = this;
56      connector.Name = connectorName;
57      return connector;
58    }
59
60
61
62    public void AddConnector(string connectorName) {
63      IConnector connector = this.CreateConnector(connectorName, this.BottomRightCorner);
64
65      this.additionalConnectors.Add(connector);
66      this.Connectors.Add(connector);
67      this.UpdateConnectorLocation();
68    }
69
70    public void RemoveConnector(string connectorName) {
71      IConnector connector = this.additionalConnectors.Where(c => c.Name == connectorName).FirstOrDefault();
72      if (connector != null) {
73        this.additionalConnectors.Remove(connector);
74        this.Connectors.Remove(connector);
75        this.UpdateConnectorLocation();
76      }
77    }
78
79    private void UpdateConnectorLocation() {
80      if (this.additionalConnectors.Count == 0)
81        return;
82
83      int spacing = this.Rectangle.Width / this.additionalConnectors.Count;
84      int margin = spacing / 2;
85      int posX = margin + this.Rectangle.X;
86      for (int i = 0; i < this.additionalConnectors.Count; i++) {
87        this.additionalConnectors[i].MoveBy(new Point(posX - this.additionalConnectors[i].Point.X, 0));
88        posX += spacing;
89      }
90    }
91
92
93    protected override void Initialize() {
94      base.Initialize();
95
96      #region Connectors
97      this.Connectors.Clear();
98
99      predecessor = this.CreateConnector("Predecessor", new Point(Rectangle.Left, Center.Y));
100      Connectors.Add(predecessor);
101
102      successor = this.CreateConnector("Successor", (new Point(Rectangle.Right, Center.Y)));
103      Connectors.Add(successor);
104      #endregion
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.