Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorShapeInfo.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 7.3 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using Netron.Diagramming.Core;
28
29namespace HeuristicLab.Operators.Views.GraphVisualization {
30  [StorableClass]
31  internal class OperatorShapeInfo : ShapeInfo, IOperatorShapeInfo {
32    [Storable]
33    private List<string> labels;
34
35    public OperatorShapeInfo()
36      : base(typeof(OperatorShape)) {
37      this.connectorNames = new List<string>();
38      this.labels = new List<string>();
39    }
40
41    public OperatorShapeInfo(IEnumerable<string> connectorNames)
42      : this() {
43      foreach (string connectorName in connectorNames)
44        this.connectorNames.Add(connectorName);
45    }
46
47    public OperatorShapeInfo(IEnumerable<string> connectorNames, IEnumerable<string> labels)
48      : this(connectorNames) {
49      foreach (string label in labels)
50        this.labels.Add(label);
51    }
52
53    public void AddConnector(string connectorName) {
54      if (!this.connectorNames.Contains(connectorName)) {
55        this.connectorNames.Add(connectorName);
56        this.OnChanged();
57      }
58    }
59
60    public void RemoveConnector(string connectorName) {
61      if (this.connectorNames.Contains(connectorName)) {
62        this.connectorNames.Remove(connectorName);
63        this.OnChanged();
64      }
65    }
66
67    public void UpdateLabels(IEnumerable<string> labels) {
68      this.labels = new List<string>(labels);
69      this.OnChanged();
70    }
71
72    [Storable]
73    private List<string> connectorNames;
74    public override IEnumerable<string> Connectors {
75      get { return this.connectorNames; }
76    }
77
78    [Storable]
79    private bool collapsed;
80    public bool Collapsed {
81      get { return this.collapsed; }
82      set {
83        if (this.collapsed != value) {
84          this.collapsed = value;
85          this.OnChanged();
86        }
87      }
88    }
89
90    [Storable]
91    private string title;
92    public string Title {
93      get { return this.title; }
94      set {
95        if (this.title != value) {
96          this.title = value;
97          this.OnChanged();
98        }
99      }
100    }
101
102    [Storable]
103    private Color color;
104    public Color Color {
105      get { return this.color; }
106      set {
107        if (this.color != value) {
108          this.color = value;
109          this.OnChanged();
110        }
111      }
112    }
113
114    [Storable]
115    private Color lineColor;
116    public Color LineColor {
117      get { return this.lineColor; }
118      set {
119        if (this.lineColor != value) {
120          this.lineColor = value;
121          this.OnChanged();
122        }
123      }
124    }
125
126    [Storable]
127    private float lineWidth;
128    public float LineWidth {
129      get { return this.lineWidth; }
130      set {
131        if (this.lineWidth != value) {
132          this.lineWidth = value;
133          this.OnChanged();
134        }
135      }
136    }
137
138    private Bitmap icon;
139    public Bitmap Icon {
140      get { return this.icon; }
141      set {
142        if (this.icon != value) {
143          this.icon = value;
144          this.OnChanged();
145        }
146      }
147    }
148
149    public override IShape CreateShape() {
150      OperatorShape shape = (OperatorShape)base.CreateShape();
151      shape.Title = this.Title;
152      shape.Color = this.Color;
153      shape.LineColor = this.LineColor;
154      shape.LineWidth = this.LineWidth;
155      shape.Icon = this.Icon;
156      shape.Collapsed = this.Collapsed;
157      foreach (string connectorName in this.connectorNames)
158        if (connectorName != OperatorShapeInfoFactory.SuccessorConnector && connectorName != OperatorShapeInfoFactory.PredecessorConnector)
159          shape.AddConnector(connectorName);
160
161      shape.UpdateLabels(this.labels);
162      return shape;
163    }
164
165    public override void UpdateShape(IShape shape) {
166      base.UpdateShape(shape);
167      OperatorShape operatorShape = (OperatorShape)shape;
168      operatorShape.Title = this.Title;
169      operatorShape.Color = this.Color;
170      operatorShape.LineColor = this.LineColor;
171      operatorShape.LineWidth = this.LineWidth;
172      operatorShape.Icon = this.Icon;
173      operatorShape.Collapsed = this.Collapsed;
174
175      int i = 0;
176      int j = 0;
177      //remove old connectors and skip correct connectors
178      List<string> oldConnectorNames = operatorShape.AdditionalConnectorNames.ToList();
179      while (i < this.connectorNames.Count && j < oldConnectorNames.Count) {
180        if (connectorNames[i] == OperatorShapeInfoFactory.SuccessorConnector ||
181          connectorNames[i] == OperatorShapeInfoFactory.PredecessorConnector)
182          i++;
183        else if (oldConnectorNames[j] == OperatorShapeInfoFactory.SuccessorConnector ||
184          oldConnectorNames[j] == OperatorShapeInfoFactory.PredecessorConnector)
185          j++;
186        else if (this.connectorNames[i] != oldConnectorNames[j]) {
187          operatorShape.RemoveConnector(oldConnectorNames[j]);
188          j++;
189        } else {
190          i++;
191          j++;
192        }
193      }
194      //remove remaining old connectors
195      for (; j < oldConnectorNames.Count; j++)
196        operatorShape.RemoveConnector(oldConnectorNames[j]);
197
198      //add new connectors except successor and connector
199      for (; i < this.connectorNames.Count; i++)
200        if (this.connectorNames[i] != OperatorShapeInfoFactory.SuccessorConnector && this.connectorNames[i] != OperatorShapeInfoFactory.PredecessorConnector)
201          operatorShape.AddConnector(this.connectorNames[i]);
202
203      operatorShape.UpdateLabels(this.labels);
204    }
205
206    public override void UpdateShapeInfo(IShape shape) {
207      base.UpdateShapeInfo(shape);
208      OperatorShape operatorShape = (OperatorShape)shape;
209      this.Title = operatorShape.Title;
210      this.Color = operatorShape.Color;
211      this.LineColor = operatorShape.LineColor;
212      this.LineWidth = operatorShape.LineWidth;
213      this.Icon = operatorShape.Icon;
214      this.Collapsed = operatorShape.Collapsed;
215
216      //TODO update Connector and labels;
217    }
218
219    public override IDeepCloneable Clone(Cloner cloner) {
220      OperatorShapeInfo clone = (OperatorShapeInfo)base.Clone(cloner);
221      clone.collapsed = this.collapsed;
222      clone.color = this.color;
223      clone.lineColor = this.lineColor;
224      clone.lineWidth = this.lineWidth;
225      clone.title = this.title;
226      clone.icon = (Bitmap)this.icon.Clone();
227
228      clone.connectorNames = new List<string>(this.connectorNames);
229      clone.labels = new List<string>(this.labels);
230
231      return clone;
232    }
233  }
234}
Note: See TracBrowser for help on using the repository browser.