Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/GraphVisualizationInfoView.cs @ 2934

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

added parameters, corrected drag and drop position (ticket #867)

File size: 17.9 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using HeuristicLab.Core;
32using HeuristicLab.Core.Views;
33using Netron.Diagramming.Core;
34using HeuristicLab.Parameters;
35using HeuristicLab.MainForm.WindowsForms;
36using HeuristicLab.Collections;
37using System.Diagnostics;
38using System.Threading;
39
40namespace HeuristicLab.Operators.Views.GraphVisualization {
41  [View("GraphVisualizationInfo View")]
42  [Content(typeof(GraphVisualizationInfo), false)]
43  public partial class GraphVisualizationInfoView : ContentView {
44    private BidirectionalLookup<IOperatorShapeInfo, IShape> shapeInfoShapeMapping;
45    private BidirectionalLookup<IOperatorShapeInfo, INotifyObservableDictionaryItemsChanged<string, IOperatorShapeInfo>> shapeInfoConnectionsMapping;
46    private BidirectionalLookup<IConnection, KeyValuePair<IConnector, IConnector>> connectionConnectorsMapping;
47
48    private bool causedUpdateOfShapeInfo;
49    public GraphVisualizationInfoView() {
50      InitializeComponent();
51      this.causedUpdateOfShapeInfo = false;
52      this.shapeInfoShapeMapping = new BidirectionalLookup<IOperatorShapeInfo, IShape>();
53      this.shapeInfoConnectionsMapping = new BidirectionalLookup<IOperatorShapeInfo, INotifyObservableDictionaryItemsChanged<string, IOperatorShapeInfo>>();
54      this.connectionConnectorsMapping = new BidirectionalLookup<IConnection, KeyValuePair<IConnector, IConnector>>();
55    }
56
57    public GraphVisualizationInfoView(GraphVisualizationInfo content)
58      : this() {
59      this.Content = content;
60    }
61
62    public IController Controller {
63      get { return this.graphVisualization.Controller; }
64    }
65
66    public new GraphVisualizationInfo Content {
67      get { return (GraphVisualizationInfo)base.Content; }
68      set { base.Content = value; }
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73      this.UpdateContent();
74    }
75
76    private void UpdateContent() {
77      foreach (IOperatorShapeInfo shapeInfo in this.shapeInfoShapeMapping.FirstValues)
78        this.RemoveShapeInfo(shapeInfo);
79
80      this.shapeInfoShapeMapping.Clear();
81      this.shapeInfoConnectionsMapping.Clear();
82      this.connectionConnectorsMapping.Clear();
83
84      foreach (IOperatorShapeInfo shapeInfo in this.Content.OperatorShapeInfos)
85        if (!this.shapeInfoShapeMapping.ContainsFirst(shapeInfo))
86          this.AddShapeInfo(shapeInfo);
87
88      foreach (KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> connection in this.Content.Connections)
89        this.AddConnection(connection.Key.Key, connection.Key.Value, connection.Value);
90
91      this.UpdateLayoutRoot();
92    }
93
94    private void UpdateLayoutRoot() {
95      IOperatorShapeInfo shapeInfo = this.Content.InitialShape;
96      if (shapeInfo != null)
97        this.graphVisualization.Controller.Model.LayoutRoot = this.shapeInfoShapeMapping.GetByFirst(shapeInfo);
98      else
99        this.graphVisualization.Controller.Model.LayoutRoot = null;
100    }
101
102    private void VisualizationInfo_InitialShapeChanged(object sender, EventArgs e) {
103      this.UpdateLayoutRoot();
104    }
105
106    protected override void RegisterContentEvents() {
107      base.RegisterContentEvents();
108      this.Content.InitialShapeChanged += new EventHandler(VisualizationInfo_InitialShapeChanged);
109      this.Content.ObserveableShapeInfos.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperatorShapeInfo>(ShapeInfos_ItemsAdded);
110      this.Content.ObserveableShapeInfos.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperatorShapeInfo>(ShapeInfos_ItemsRemoved);
111      this.Content.ObserveableShapeInfos.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperatorShapeInfo>(ShapeInfos_CollectionReset);
112
113      this.Content.ObservableConnections.ItemsAdded += new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>>(Connections_ItemsAdded);
114      this.Content.ObservableConnections.ItemsRemoved += new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>>(Connections_ItemsRemoved);
115      this.Content.ObservableConnections.ItemsReplaced += new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>>(Connections_ItemsReplaced);
116      this.Content.ObservableConnections.CollectionReset += new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>>(Connections_CollectionReset);
117    }
118
119    protected override void DeregisterContentEvents() {
120      base.DeregisterContentEvents();
121      this.Content.InitialShapeChanged -= new EventHandler(VisualizationInfo_InitialShapeChanged);
122      this.Content.ObserveableShapeInfos.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperatorShapeInfo>(ShapeInfos_ItemsAdded);
123      this.Content.ObserveableShapeInfos.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperatorShapeInfo>(ShapeInfos_ItemsRemoved);
124      this.Content.ObserveableShapeInfos.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperatorShapeInfo>(ShapeInfos_CollectionReset);
125
126      this.Content.ObservableConnections.ItemsAdded -= new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>>(Connections_ItemsAdded);
127      this.Content.ObservableConnections.ItemsRemoved -= new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>>(Connections_ItemsRemoved);
128      this.Content.ObservableConnections.ItemsReplaced -= new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>>(Connections_ItemsReplaced);
129      this.Content.ObservableConnections.CollectionReset -= new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>>(Connections_CollectionReset);
130    }
131
132    private void ShapeInfos_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperatorShapeInfo> e) {
133      foreach (IOperatorShapeInfo shapeInfo in e.OldItems)
134        this.RemoveShapeInfo(shapeInfo);
135      foreach (IOperatorShapeInfo shapeInfo in e.Items)
136        this.AddShapeInfo(shapeInfo);
137    }
138
139    private void ShapeInfos_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperatorShapeInfo> e) {
140      foreach (IOperatorShapeInfo shapeInfo in e.Items)
141        this.AddShapeInfo(shapeInfo);
142    }
143
144    private void ShapeInfos_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperatorShapeInfo> e) {
145      foreach (IOperatorShapeInfo shapeInfo in e.Items)
146        this.RemoveShapeInfo(shapeInfo);
147    }
148
149    private void AddShapeInfo(IOperatorShapeInfo shapeInfo) {
150      this.RegisterShapeInfoEvents(shapeInfo);
151
152      IShape shape = shapeInfo.CreateShape();
153      shape.OnEntityChange += new EventHandler<EntityEventArgs>(shape_OnEntityChange);
154      this.shapeInfoShapeMapping.Add(shapeInfo, shape);
155
156      this.graphVisualization.Controller.Model.AddShape(shape);
157      this.graphVisualization.Invalidate();
158    }
159
160    private void RemoveShapeInfo(IOperatorShapeInfo shapeInfo) {
161      this.DeregisterShapeInfoEvents(shapeInfo);
162      IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo);
163      shape.OnEntityChange -= new EventHandler<EntityEventArgs>(shape_OnEntityChange);
164
165      IConnection connection;
166      foreach (IConnector connector in shape.Connectors) {
167        connection = this.GetConnection(shapeInfo, connector.Name);
168        this.RemoveConnection(connection);
169      }
170
171      this.shapeInfoShapeMapping.RemoveByFirst(shapeInfo);
172      this.shapeInfoConnectionsMapping.RemoveByFirst(shapeInfo);
173
174      if (this.graphVisualization.Controller.Model.Shapes.Contains(shape)) {
175        this.graphVisualization.Controller.Model.RemoveShape(shape);
176      }
177      this.graphVisualization.Invalidate();
178    }
179
180    private void RegisterShapeInfoEvents(IShapeInfo shapeInfo) {
181      shapeInfo.Changed += new EventHandler(shapeInfo_Changed);
182    }
183
184    private void DeregisterShapeInfoEvents(IShapeInfo shapeInfo) {
185      shapeInfo.Changed -= new EventHandler(shapeInfo_Changed);
186    }
187
188    private void Connections_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>> e) {
189      IConnection connection;
190      foreach (KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> pair in e.Items) {
191        connection = this.GetConnection(pair.Key.Key, pair.Key.Value);
192        this.RemoveConnection(connection);
193      }
194      foreach (KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> pair in e.Items)
195        this.AddConnection(pair.Key.Key, pair.Key.Value, pair.Value);
196    }
197
198    private void Connections_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>> e) {
199      IConnection connection;
200      foreach (KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> pair in e.Items) {
201        connection = this.GetConnection(pair.Key.Key, pair.Key.Value);
202        this.RemoveConnection(connection);
203      }
204      foreach (KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> pair in e.Items)
205        this.AddConnection(pair.Key.Key, pair.Key.Value, pair.Value);
206    }
207
208    private void Connections_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>> e) {
209      foreach (KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> pair in e.Items)
210        this.AddConnection(pair.Key.Key, pair.Key.Value, pair.Value);
211    }
212
213    private void Connections_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>> e) {
214      IConnection connection;
215      foreach (KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> pair in e.Items) {
216        connection = this.GetConnection(pair.Key.Key, pair.Key.Value);
217        this.RemoveConnection(connection);
218      }
219    }
220
221    private void AddConnection(IOperatorShapeInfo shapeInfoFrom, string connectorName, IOperatorShapeInfo shapeInfoTo) {
222      IShape shapeFrom = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom);
223      IShape shapeTo = this.shapeInfoShapeMapping.GetByFirst(shapeInfoTo);
224
225      IConnector connectorFrom = shapeFrom.Connectors.Where(c => c.Name == connectorName).First();
226      IConnector connectorTo = shapeTo.Connectors.Where(c => c.Name == "Predecessor").FirstOrDefault();
227      KeyValuePair<IConnector, IConnector> connectorPair = new KeyValuePair<IConnector, IConnector>(connectorFrom, connectorTo);
228      if (!this.connectionConnectorsMapping.ContainsSecond(connectorPair)) {
229        IConnection connection = Factory.CreateConnection(connectorFrom, connectorTo);
230        this.connectionConnectorsMapping.Add(connection, connectorPair);
231        this.graphVisualization.Controller.Model.AddConnection(connection);
232        this.graphVisualization.Invalidate();
233      }
234    }
235
236    private IConnection GetConnection(IOperatorShapeInfo shapeInfoFrom, string connectorName) {
237      IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom);
238      IConnector connector = shape.Connectors.Where(c => c.Name == connectorName).First();
239
240      if (!this.connectionConnectorsMapping.SecondValues.Any(p => p.Key == connector))
241        return null;
242
243      KeyValuePair<IConnector, IConnector> connectorPair = this.connectionConnectorsMapping.SecondValues.Where(p => p.Key == connector).FirstOrDefault();
244      return this.connectionConnectorsMapping.GetBySecond(connectorPair);
245    }
246
247    private void ChangeConnection(IOperatorShapeInfo shapeInfoFrom, string connectorName, IOperatorShapeInfo shapeInfoTo) {
248      IConnection connection = this.GetConnection(shapeInfoFrom, connectorName);
249      IShape shapeTo = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom);
250      IConnector connectorTo = shapeTo.Connectors.Where(c => c.Name == "Predecessor").First();
251
252      connection.To.DetachFromParent();
253      connection.To.AttachTo(connectorTo);
254      this.graphVisualization.Invalidate();
255    }
256
257    private void RemoveConnection(IConnection connection) {
258      if (connection == null)
259        return;
260
261      if (connection.From.AttachedTo != null)
262        connection.From.DetachFromParent();
263      if (connection.To.AttachedTo != null)
264        connection.To.DetachFromParent();
265
266      if (this.connectionConnectorsMapping.ContainsFirst(connection))
267        this.connectionConnectorsMapping.RemoveByFirst(connection);
268      if (this.graphVisualization.Controller.Model.Connections.Contains(connection))
269        this.graphVisualization.Controller.Model.Remove(connection);
270      this.graphVisualization.Invalidate();
271    }
272
273
274    private void shapeInfo_Changed(object sender, EventArgs e) {
275      if (this.causedUpdateOfShapeInfo)
276        return;
277
278      this.causedUpdateOfShapeInfo = true;
279      IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)sender;
280      IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo);
281      shapeInfo.UpdateShape(shape);
282      shape.Invalidate();
283      this.causedUpdateOfShapeInfo = false;
284    }
285
286
287    private void shape_OnEntityChange(object sender, EntityEventArgs e) {
288      if (this.causedUpdateOfShapeInfo)
289        return;
290
291      this.causedUpdateOfShapeInfo = true;
292      IShape shape = e.Entity as IShape;
293      IOperatorShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape);
294      shapeInfo.Location = shape.Location;
295
296      OperatorShape operatorShape = shape as OperatorShape;
297      if (operatorShape != null)
298        shapeInfo.Collapsed = operatorShape.Collapsed;
299     
300      this.graphVisualization.Invalidate();
301      this.causedUpdateOfShapeInfo = false;
302    }
303
304    private void graphVisualization_OnEntityAdded(object sender, EntityEventArgs e) {
305      IConnection connection = e.Entity as IConnection;
306      if (connection != null && !this.connectionConnectorsMapping.ContainsFirst(connection)) {
307        IConnector connectorFrom = connection.From.AttachedTo;
308        IConnector connectorTo = connection.To.AttachedTo;
309        this.RemoveConnection(connection); //is added again by the model events
310
311        if (connectorFrom != null && connectorTo != null && connectorFrom.Name != "Predecessor") {
312          IShape shapeFrom = (IShape)connectorFrom.Parent;
313          IShape shapeTo = (IShape)connectorTo.Parent;
314          IOperatorShapeInfo shapeInfoFrom = this.shapeInfoShapeMapping.GetBySecond(shapeFrom);
315          IOperatorShapeInfo shapeInfoTo = this.shapeInfoShapeMapping.GetBySecond(shapeTo);
316          string connectorName = connectorFrom.Name;
317
318          this.Content.AddConnection(shapeInfoFrom, connectorName, shapeInfoTo);
319        }
320      }
321    }
322
323    private void graphVisualization_OnEntityRemoved(object sender, EntityEventArgs e) {
324      IShape shape = e.Entity as IShape;
325      if (shape != null && this.shapeInfoShapeMapping.ContainsSecond(shape)) {
326        IOperatorShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape);
327        this.Content.RemoveShapeInfo(shapeInfo);
328      }
329
330      IConnection connection = e.Entity as IConnection;
331      if (connection != null && this.connectionConnectorsMapping.ContainsFirst(connection)) {
332        IShape parentShape = (IShape)connection.From.AttachedTo.Parent;
333        IOperatorShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(parentShape);
334        string connectorName = connection.From.AttachedTo.Name;
335
336        this.Content.RemoveConnection(shapeInfo, connectorName);
337      }
338    }
339
340    internal void RelayoutGraph() {
341      if (this.shapeInfoShapeMapping.Count > 0
342        && this.connectionConnectorsMapping.Count > 0
343        && this.Content.InitialShape != null) { //otherwise the layout does not work
344        string layoutName = "Standard TreeLayout";
345        this.graphVisualization.Controller.RunActivity(layoutName);
346        this.graphVisualization.Invalidate();
347
348        //fix to avoid negative shape positions after layouting
349        Thread.Sleep(100);
350        int minX = this.graphVisualization.Controller.Model.Shapes.Min(s => s.Location.X);
351        int shiftX = minX < 0 ? Math.Abs(minX) + 50 : 0;
352        int minY = this.graphVisualization.Controller.Model.Shapes.Min(s => s.Location.Y);
353        int shiftY = minY < 0 ? Math.Abs(minY) + 50 : 0;
354        if (minX < 0 || minY < 0) {
355          foreach (IShape s in this.Controller.Model.Shapes)
356            s.MoveBy(new Point(shiftX, shiftY));
357        }
358      }
359    }
360  }
361}
Note: See TracBrowser for help on using the repository browser.