Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Operators.Views.GraphVisualization.Views/3.3/ShapeInfoExtensions.cs @ 16123

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

#1486: Decoupled operator graph visualization content classes from view classes.

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Color = shapeInfo.Color;
53      shape.LineColor = shapeInfo.LineColor;
54      shape.LineWidth = shapeInfo.LineWidth;
55      shape.Icon = shapeInfo.Icon;
56      shape.Collapsed = shapeInfo.Collapsed;
57      foreach (string connectorName in shapeInfo.Connectors)
58        if (connectorName != OperatorShapeInfoFactory.SuccessorConnector && connectorName != OperatorShapeInfoFactory.PredecessorConnector)
59          shape.AddConnector(connectorName);
60
61      shape.UpdateLabels(shapeInfo.Labels);
62      return shape;
63    }
64
65    public static void UpdateShape(IOperatorShapeInfo operatorShapeInfo, OperatorShape operatorShape) {
66      operatorShape.Title = operatorShapeInfo.Title;
67      operatorShape.Color = operatorShapeInfo.Color;
68      operatorShape.LineColor = operatorShapeInfo.LineColor;
69      operatorShape.LineWidth = operatorShapeInfo.LineWidth;
70      operatorShape.Icon = operatorShapeInfo.Icon;
71      operatorShape.Collapsed = operatorShapeInfo.Collapsed;
72
73      int i = 0;
74      int j = 0;
75      //remove old connectors and skip correct connectors
76      List<string> oldConnectorNames = operatorShape.AdditionalConnectorNames.ToList();
77      while (i < operatorShapeInfo.Connectors.Count() && j < oldConnectorNames.Count) {
78        if (operatorShapeInfo.Connectors.ElementAt(i) == OperatorShapeInfoFactory.SuccessorConnector ||
79          operatorShapeInfo.Connectors.ElementAt(i) == OperatorShapeInfoFactory.PredecessorConnector)
80          i++;
81        else if (oldConnectorNames[j] == OperatorShapeInfoFactory.SuccessorConnector ||
82          oldConnectorNames[j] == OperatorShapeInfoFactory.PredecessorConnector)
83          j++;
84        else if (operatorShapeInfo.Connectors.ElementAt(i) != oldConnectorNames[j]) {
85          operatorShape.RemoveConnector(oldConnectorNames[j]);
86          j++;
87        } else {
88          i++;
89          j++;
90        }
91      }
92      //remove remaining old connectors
93      for (; j < oldConnectorNames.Count; j++)
94        operatorShape.RemoveConnector(oldConnectorNames[j]);
95
96      //add new connectors except successor and connector
97      for (; i < operatorShapeInfo.Connectors.Count(); i++)
98        if (operatorShapeInfo.Connectors.ElementAt(i) != OperatorShapeInfoFactory.SuccessorConnector &&
99          operatorShapeInfo.Connectors.ElementAt(i) != OperatorShapeInfoFactory.PredecessorConnector)
100          operatorShape.AddConnector(operatorShapeInfo.Connectors.ElementAt(i));
101
102      operatorShape.UpdateLabels(operatorShapeInfo.Labels);
103    }
104
105    public static void UpdateShapeInfo(IOperatorShapeInfo operatorShapeInfo, OperatorShape operatorShape) {
106      operatorShapeInfo.Title = operatorShape.Title;
107      operatorShapeInfo.Color = operatorShape.Color;
108      operatorShapeInfo.LineColor = operatorShape.LineColor;
109      operatorShapeInfo.LineWidth = operatorShape.LineWidth;
110      operatorShapeInfo.Icon = operatorShape.Icon;
111      operatorShapeInfo.Collapsed = operatorShape.Collapsed;
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.