Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2972_PDPRowSelect/HeuristicLab.Operators.Views.GraphVisualization.Views/3.3/ShapeInfoExtensions.cs @ 16674

Last change on this file since 16674 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 Netron.Diagramming.Core;
26
27namespace HeuristicLab.Operators.Views.GraphVisualization.Views {
28  internal static class ShapeInfoExtensions {
29    public static IShape CreateShape(this IShapeInfo shapeInfo) {
30      if (shapeInfo is IOperatorShapeInfo) return CreateShape((IOperatorShapeInfo)shapeInfo);
31      throw new ArgumentException("Could not determine which shape object should be created.");
32    }
33
34    public static void UpdateShape(this IShapeInfo shapeInfo, IShape shape) {
35      shape.Location = shapeInfo.Location;
36      if (shapeInfo is IOperatorShapeInfo && shape is OperatorShape)
37        UpdateShape((IOperatorShapeInfo)shapeInfo, (OperatorShape)shape);
38    }
39
40    public static void UpdateShapeInfo(this IShapeInfo shapeInfo, IShape shape) {
41      shapeInfo.Location = shape.Location;
42      if (shapeInfo is IOperatorShapeInfo && shape is OperatorShape)
43        UpdateShapeInfo((IOperatorShapeInfo)shapeInfo, (OperatorShape)shape);
44    }
45
46    #region OperatorShapeInfo specific methos
47    public static OperatorShape CreateShape(IOperatorShapeInfo shapeInfo) {
48      OperatorShape shape = new OperatorShape();
49      shape.Tag = shapeInfo;
50      shape.Location = shapeInfo.Location;
51      shape.Title = shapeInfo.Title;
52      shape.Subtitle = shapeInfo.TypeName;
53      shape.Color = shapeInfo.Color;
54      shape.LineColor = shapeInfo.LineColor;
55      shape.LineWidth = shapeInfo.LineWidth;
56      shape.Icon = shapeInfo.Icon;
57      shape.Collapsed = shapeInfo.Collapsed;
58      foreach (string connectorName in shapeInfo.Connectors)
59        if (connectorName != OperatorShapeInfoFactory.SuccessorConnector && connectorName != OperatorShapeInfoFactory.PredecessorConnector)
60          shape.AddConnector(connectorName);
61
62      shape.UpdateLabels(shapeInfo.Labels);
63      return shape;
64    }
65
66    public static void UpdateShape(IOperatorShapeInfo operatorShapeInfo, OperatorShape operatorShape) {
67      operatorShape.Title = operatorShapeInfo.Title;
68      operatorShape.Subtitle = operatorShapeInfo.TypeName;
69      operatorShape.Color = operatorShapeInfo.Color;
70      operatorShape.LineColor = operatorShapeInfo.LineColor;
71      operatorShape.LineWidth = operatorShapeInfo.LineWidth;
72      operatorShape.Icon = operatorShapeInfo.Icon;
73      operatorShape.Collapsed = operatorShapeInfo.Collapsed;
74
75      int i = 0;
76      int j = 0;
77      //remove old connectors and skip correct connectors
78      List<string> oldConnectorNames = operatorShape.AdditionalConnectorNames.ToList();
79      while (i < operatorShapeInfo.Connectors.Count() && j < oldConnectorNames.Count) {
80        if (operatorShapeInfo.Connectors.ElementAt(i) == OperatorShapeInfoFactory.SuccessorConnector ||
81          operatorShapeInfo.Connectors.ElementAt(i) == OperatorShapeInfoFactory.PredecessorConnector)
82          i++;
83        else if (oldConnectorNames[j] == OperatorShapeInfoFactory.SuccessorConnector ||
84          oldConnectorNames[j] == OperatorShapeInfoFactory.PredecessorConnector)
85          j++;
86        else if (operatorShapeInfo.Connectors.ElementAt(i) != oldConnectorNames[j]) {
87          operatorShape.RemoveConnector(oldConnectorNames[j]);
88          j++;
89        } else {
90          i++;
91          j++;
92        }
93      }
94      //remove remaining old connectors
95      for (; j < oldConnectorNames.Count; j++)
96        operatorShape.RemoveConnector(oldConnectorNames[j]);
97
98      //add new connectors except successor and connector
99      for (; i < operatorShapeInfo.Connectors.Count(); i++)
100        if (operatorShapeInfo.Connectors.ElementAt(i) != OperatorShapeInfoFactory.SuccessorConnector &&
101          operatorShapeInfo.Connectors.ElementAt(i) != OperatorShapeInfoFactory.PredecessorConnector)
102          operatorShape.AddConnector(operatorShapeInfo.Connectors.ElementAt(i));
103
104      operatorShape.UpdateLabels(operatorShapeInfo.Labels);
105    }
106
107    public static void UpdateShapeInfo(IOperatorShapeInfo operatorShapeInfo, OperatorShape operatorShape) {
108      operatorShapeInfo.Title = operatorShape.Title;
109      operatorShapeInfo.TypeName = operatorShape.Subtitle;
110      operatorShapeInfo.Color = operatorShape.Color;
111      operatorShapeInfo.LineColor = operatorShape.LineColor;
112      operatorShapeInfo.LineWidth = operatorShape.LineWidth;
113      operatorShapeInfo.Icon = operatorShape.Icon;
114      operatorShapeInfo.Collapsed = operatorShape.Collapsed;
115    }
116    #endregion
117  }
118}
Note: See TracBrowser for help on using the repository browser.