Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3001 was 2995, checked in by abeham, 15 years ago

Added StorableClass attribute to several more classes #548

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