Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/Model/OperatorShapeInfo.cs @ 3376

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

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 6.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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using System.Drawing;
29using Netron.Diagramming.Core;
30using System.Windows.Forms;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Operators.Views.GraphVisualization {
34  [StorableClass]
35  internal class OperatorShapeInfo : ShapeInfo, IOperatorShapeInfo {
36    [Storable]
37    private List<string> connectorNames;
38    [Storable]
39    private List<string> labels;
40
41    public OperatorShapeInfo()
42      : base(typeof(OperatorShape)) {
43      this.connectorNames = new List<string>();
44      this.labels = new List<string>();
45    }
46
47    public OperatorShapeInfo(IEnumerable<string> connectorNames)
48      : this() {
49      foreach (string connectorName in connectorNames)
50        this.connectorNames.Add(connectorName);
51    }
52
53    public OperatorShapeInfo(IEnumerable<string> connectorNames, IEnumerable<string> labels)
54      : this(connectorNames) {
55      foreach (string label in labels)
56        this.labels.Add(label);
57    }
58
59    public void AddConnector(string connectorName) {
60      if (!this.connectorNames.Contains(connectorName) && connectorName != "Successor") {
61        this.connectorNames.Add(connectorName);
62        this.OnChanged();
63      }
64    }
65
66    public void RemoveConnector(string connectorName) {
67      if (this.connectorNames.Contains(connectorName)) {
68        this.connectorNames.Remove(connectorName);
69        this.OnChanged();
70      }
71    }
72
73    public void UpdateLabels(IEnumerable<string> labels) {
74      this.labels = new List<string>(labels);
75      this.OnChanged();
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        shape.AddConnector(connectorName);
159
160      shape.UpdateLabels(this.labels);
161      return shape;
162    }
163
164    public override void UpdateShape(IShape shape) {
165      base.UpdateShape(shape);
166      OperatorShape operatorShape = shape as OperatorShape;
167      if (operatorShape != null) {
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 (this.connectorNames[i] != oldConnectorNames[j]) {
181            operatorShape.RemoveConnector(oldConnectorNames[j]);
182          } else
183            i++;
184          j++;
185        }
186        //remove remaining old connectors
187        for (; j < oldConnectorNames.Count; j++)
188          operatorShape.RemoveConnector(oldConnectorNames[j]);
189
190        //add new connectors
191        for (; i < this.connectorNames.Count; i++)
192          operatorShape.AddConnector(this.connectorNames[i]);
193
194        operatorShape.UpdateLabels(this.labels);
195      }
196    }
197
198    public override IDeepCloneable Clone(Cloner cloner) {
199      OperatorShapeInfo clone = (OperatorShapeInfo) base.Clone(cloner);
200      clone.collapsed = this.collapsed;
201      clone.color = this.color;
202      clone.lineColor = this.lineColor;
203      clone.lineWidth = this.lineWidth;
204      clone.title = this.title;
205      clone.icon = (Bitmap) this.icon.Clone();
206
207      clone.connectorNames = new List<string>(this.connectorNames);
208      clone.labels = new List<string>(this.labels);
209
210      return clone;
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.