Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3580 was 3566, checked in by mkommend, 15 years ago

removed ctors with contents in all views (ticket #972)

File size: 16.7 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;
39using System.Drawing.Drawing2D;
40
41namespace HeuristicLab.Operators.Views.GraphVisualization {
42  [View("GraphVisualizationInfo View")]
43  [Content(typeof(IGraphVisualizationInfo), true)]
44  public partial class GraphVisualizationInfoView : AsynchronousContentView {
45    private BidirectionalLookup<IShapeInfo, IShape> shapeInfoShapeMapping;
46    private BidirectionalLookup<IConnectionInfo, IConnection> connectionInfoConnectionMapping;
47    private LinePenStyle connectionPenStyle;
48
49    public GraphVisualizationInfoView() {
50      InitializeComponent();
51      this.shapeInfoShapeMapping = new BidirectionalLookup<IShapeInfo, IShape>();
52      this.connectionInfoConnectionMapping = new BidirectionalLookup<IConnectionInfo, IConnection>();
53      this.connectionPenStyle = new LinePenStyle();
54      this.connectionPenStyle.EndCap = LineCap.ArrowAnchor;
55    }
56
57    public IController Controller {
58      get { return this.graphVisualization.Controller; }
59    }
60
61    public new IGraphVisualizationInfo Content {
62      get { return (IGraphVisualizationInfo)base.Content; }
63      set { base.Content = value; }
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      this.UpdateContent();
69    }
70
71    protected override void OnReadOnlyChanged() {
72      base.OnReadOnlyChanged();
73      this.SetEnabledStateOfControls();
74    }
75    protected override void OnLockedChanged() {
76      base.OnLockedChanged();
77      this.SetEnabledStateOfControls();
78    }
79    private void SetEnabledStateOfControls() {
80      DeleteTool deleteTool = (DeleteTool)this.Controller.Tools.Where(t => t.Name == ControllerBase.DeleteToolName).FirstOrDefault();
81      HeuristicLab.Netron.Controller controller = this.Controller as HeuristicLab.Netron.Controller;
82      if (Content == null && deleteTool != null && controller != null)
83        controller.RemoveTool(deleteTool);
84      else {
85        if ((ReadOnly || Locked) && deleteTool != null && controller != null)
86          controller.RemoveTool(deleteTool);
87        else if ((!ReadOnly && !Locked) && deleteTool == null)
88          this.Controller.AddTool(new DeleteTool(ControllerBase.DeleteToolName));
89      }
90    }
91
92    private void UpdateContent() {
93      foreach (IConnectionInfo connectionInfo in this.connectionInfoConnectionMapping.FirstValues)
94        this.RemoveConnectionInfo(connectionInfo);
95      this.connectionInfoConnectionMapping.Clear();
96      foreach (IShapeInfo shapeInfo in this.shapeInfoShapeMapping.FirstValues)
97        this.RemoveShapeInfo(shapeInfo);
98      this.shapeInfoShapeMapping.Clear();
99
100      if (Content != null) {
101        foreach (IShapeInfo shapeInfo in this.Content.ShapeInfos)
102          this.AddShapeInfo(shapeInfo);
103        foreach (IConnectionInfo connectionInfo in this.Content.ConnectionInfos)
104          this.AddConnectionInfo(connectionInfo);
105        this.UpdateLayoutRoot();
106      }
107      this.SetEnabledStateOfControls();
108    }
109    private void UpdateLayoutRoot() {
110      IShapeInfo shapeInfo = this.Content.InitialShape;
111      if (shapeInfo != null)
112        this.graphVisualization.Controller.Model.LayoutRoot = this.shapeInfoShapeMapping.GetByFirst(shapeInfo);
113      else
114        this.graphVisualization.Controller.Model.LayoutRoot = null;
115    }
116    private void VisualizationInfo_InitialShapeChanged(object sender, EventArgs e) {
117      this.UpdateLayoutRoot();
118    }
119
120    protected override void RegisterContentEvents() {
121      base.RegisterContentEvents();
122      this.Content.InitialShapeChanged += new EventHandler(VisualizationInfo_InitialShapeChanged);
123
124      this.Content.ObserveableShapeInfos.ItemsAdded += new CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsAdded);
125      this.Content.ObserveableShapeInfos.ItemsRemoved += new CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsRemoved);
126      this.Content.ObserveableShapeInfos.CollectionReset += new CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_CollectionReset);
127
128      this.Content.ObservableConnectionInfos.ItemsAdded += new CollectionItemsChangedEventHandler<IConnectionInfo>(ConnectionInfos_ItemsAdded);
129      this.Content.ObservableConnectionInfos.ItemsRemoved += new CollectionItemsChangedEventHandler<IConnectionInfo>(ConnectionInfos_ItemsRemoved);
130      this.Content.ObservableConnectionInfos.CollectionReset += new CollectionItemsChangedEventHandler<IConnectionInfo>(ConnectionInfos_CollectionReset);
131    }
132
133    protected override void DeregisterContentEvents() {
134      base.DeregisterContentEvents();
135      this.Content.InitialShapeChanged -= new EventHandler(VisualizationInfo_InitialShapeChanged);
136
137      this.Content.ObserveableShapeInfos.ItemsAdded -= new CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsAdded);
138      this.Content.ObserveableShapeInfos.ItemsRemoved -= new CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsRemoved);
139      this.Content.ObserveableShapeInfos.CollectionReset -= new CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_CollectionReset);
140
141      this.Content.ObservableConnectionInfos.ItemsAdded -= new CollectionItemsChangedEventHandler<IConnectionInfo>(ConnectionInfos_ItemsAdded);
142      this.Content.ObservableConnectionInfos.ItemsRemoved -= new CollectionItemsChangedEventHandler<IConnectionInfo>(ConnectionInfos_ItemsRemoved);
143      this.Content.ObservableConnectionInfos.CollectionReset -= new CollectionItemsChangedEventHandler<IConnectionInfo>(ConnectionInfos_CollectionReset);
144    }
145
146    #region ShapeInfos
147    private void ShapeInfos_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) {
148      foreach (IShapeInfo shapeInfo in e.OldItems)
149        this.RemoveShapeInfo(shapeInfo);
150      foreach (IShapeInfo shapeInfo in e.Items)
151        this.AddShapeInfo(shapeInfo);
152    }
153    private void ShapeInfos_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) {
154      foreach (IShapeInfo shapeInfo in e.Items)
155        this.AddShapeInfo(shapeInfo);
156    }
157    private void ShapeInfos_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) {
158      foreach (IShapeInfo shapeInfo in e.Items)
159        this.RemoveShapeInfo(shapeInfo);
160    }
161
162    private void AddShapeInfo(IShapeInfo shapeInfo) {
163      this.RegisterShapeInfoEvents(shapeInfo);
164      IShape shape = shapeInfo.CreateShape();
165      this.RegisterShapeEvents(shape);
166      this.shapeInfoShapeMapping.Add(shapeInfo, shape);
167
168      this.graphVisualization.Controller.Model.AddShape(shape);
169      this.graphVisualization.Invalidate();
170    }
171    private void RemoveShapeInfo(IShapeInfo shapeInfo) {
172      this.DeregisterShapeInfoEvents(shapeInfo);
173      IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo);
174      this.DeregisterShapeEvents(shape);
175      this.shapeInfoShapeMapping.RemoveByFirst(shapeInfo);
176
177      if (this.graphVisualization.Controller.Model.Shapes.Contains(shape)) {
178        this.graphVisualization.Controller.Model.RemoveShape(shape);
179        this.graphVisualization.Controller.Model.Selection.Clear();
180        this.graphVisualization.Invalidate();
181      }
182    }
183
184    private void RegisterShapeInfoEvents(IShapeInfo shapeInfo) {
185      shapeInfo.Changed += new EventHandler(shapeInfo_Changed);
186    }
187    private void DeregisterShapeInfoEvents(IShapeInfo shapeInfo) {
188      shapeInfo.Changed -= new EventHandler(shapeInfo_Changed);
189    }
190
191    private void shapeInfo_Changed(object sender, EventArgs e) {
192      IShapeInfo shapeInfo = (IShapeInfo)sender;
193      IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo);
194      this.DeregisterShapeEvents(shape);
195      shapeInfo.UpdateShape(shape);
196      shape.Invalidate();
197      this.RegisterShapeEvents(shape);
198    }
199    #endregion
200
201    #region ConnectionInfos
202    private void ConnectionInfos_CollectionReset(object sender, CollectionItemsChangedEventArgs<IConnectionInfo> e) {
203      foreach (IConnectionInfo connectionInfo in e.Items)
204        this.RemoveConnectionInfo(connectionInfo);
205      foreach (IConnectionInfo connectionInfo in e.Items)
206        this.AddConnectionInfo(connectionInfo);
207    }
208    private void ConnectionInfos_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IConnectionInfo> e) {
209      foreach (IConnectionInfo connectionInfo in e.Items)
210        this.AddConnectionInfo(connectionInfo);
211    }
212    private void ConnectionInfos_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IConnectionInfo> e) {
213      foreach (IConnectionInfo connectionInfo in e.Items)
214        this.RemoveConnectionInfo(connectionInfo);
215    }
216
217    private void AddConnectionInfo(IConnectionInfo connectionInfo) {
218      this.RegisterConnectionInfoEvents(connectionInfo);
219      IShape shapeFrom = this.shapeInfoShapeMapping.GetByFirst(connectionInfo.From);
220      IShape shapeTo = this.shapeInfoShapeMapping.GetByFirst(connectionInfo.To);
221
222      IConnector connectorFrom = shapeFrom.Connectors.Where(c => c.Name == connectionInfo.ConnectorFrom).FirstOrDefault();
223      IConnector connectorTo = shapeTo.Connectors.Where(c => c.Name == connectionInfo.ConnectorTo).FirstOrDefault();
224      if (connectorFrom != null && connectorTo != null) {
225        Connection connection = new Connection(connectorFrom.Point, connectorTo.Point);
226        connection.From.AllowMove = false;
227        connection.To.AllowMove = false;
228        connectorFrom.AttachConnector(connection.From);
229        connectorTo.AttachConnector(connection.To);
230        connection.PenStyle = this.connectionPenStyle;
231        this.connectionInfoConnectionMapping.Add(connectionInfo, connection);
232        this.graphVisualization.Controller.Model.AddConnection(connection);
233        this.graphVisualization.Invalidate();
234      }
235    }
236
237    private void RemoveConnectionInfo(IConnectionInfo connectionInfo) {
238      DeregisterConnectionInfoEvents(connectionInfo);
239      IConnection connection = this.connectionInfoConnectionMapping.GetByFirst(connectionInfo);
240      this.connectionInfoConnectionMapping.RemoveByFirst(connectionInfo);
241      this.RemoveConnection(connection);
242
243    }
244    private void RemoveConnection(IConnection connection) {
245      if (connection.From.AttachedTo != null)
246        connection.From.DetachFromParent();
247      if (connection.To.AttachedTo != null)
248        connection.To.DetachFromParent();
249      if (this.Controller.Model.Connections.Contains(connection)) {
250        this.graphVisualization.Controller.Model.Remove(connection);
251        this.graphVisualization.Invalidate();
252      }
253    }
254
255    private void RegisterConnectionInfoEvents(IConnectionInfo connectionInfo) {
256      connectionInfo.Changed += new EventHandler(connectionInfo_Changed);
257    }
258    private void DeregisterConnectionInfoEvents(IConnectionInfo connectionInfo) {
259      connectionInfo.Changed -= new EventHandler(connectionInfo_Changed);
260    }
261    private void connectionInfo_Changed(object sender, EventArgs e) {
262      IConnectionInfo connectionInfo = (IConnectionInfo)sender;
263      IConnection connection = this.connectionInfoConnectionMapping.GetByFirst(connectionInfo);
264      this.RemoveConnectionInfo(connectionInfo);
265      this.AddConnectionInfo(connectionInfo);
266    }
267    #endregion
268
269    #region netron events - shapes, graphvisualization
270    private void RegisterShapeEvents(IShape shape) {
271      shape.OnEntityChange += new EventHandler<EntityEventArgs>(shape_OnEntityChange);
272      shape.OnMouseEnter += new EventHandler<EntityMouseEventArgs>(shape_OnMouseEnter);
273      shape.OnMouseLeave += new EventHandler<EntityMouseEventArgs>(shape_OnMouseLeave);
274    }
275
276    private void DeregisterShapeEvents(IShape shape) {
277      shape.OnEntityChange -= new EventHandler<EntityEventArgs>(shape_OnEntityChange);
278      shape.OnMouseEnter -= new EventHandler<EntityMouseEventArgs>(shape_OnMouseEnter);
279      shape.OnMouseLeave -= new EventHandler<EntityMouseEventArgs>(shape_OnMouseLeave);
280    }
281
282    private Cursor oldCursor;
283    private void shape_OnMouseEnter(object sender, EntityMouseEventArgs e) {
284      this.oldCursor = this.Cursor;
285      this.Controller.View.CurrentCursor = CursorPalette.Move;
286    }
287
288    private void shape_OnMouseLeave(object sender, EntityMouseEventArgs e) {
289      this.Controller.View.CurrentCursor = this.oldCursor;
290      this.oldCursor = null;
291    }
292
293    private void shape_OnEntityChange(object sender, EntityEventArgs e) {
294      IShape shape = e.Entity as IShape;
295      IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape);
296      this.DeregisterShapeInfoEvents(shapeInfo);
297      shapeInfo.UpdateShapeInfo(shape);
298      this.RegisterShapeInfoEvents(shapeInfo);
299      this.graphVisualization.Invalidate();
300    }
301
302
303    private void graphVisualization_OnEntityAdded(object sender, EntityEventArgs e) {
304      IConnection connection = e.Entity as IConnection;
305      if (connection != null && !this.connectionInfoConnectionMapping.ContainsSecond(connection)) {
306        IConnector connectorFrom = connection.From.AttachedTo;
307        IConnector connectorTo = connection.To.AttachedTo;
308        this.RemoveConnection(connection); //is added again by the model events
309
310        if (connectorFrom != null && connectorTo != null) {
311          IShape shapeFrom = (IShape)connectorFrom.Parent;
312          IShape shapeTo = (IShape)connectorTo.Parent;
313          IShapeInfo shapeInfoFrom = this.shapeInfoShapeMapping.GetBySecond(shapeFrom);
314          IShapeInfo shapeInfoTo = this.shapeInfoShapeMapping.GetBySecond(shapeTo);
315          string connectorFromName = connectorFrom.Name;
316          string connectorToName = connectorTo.Name;
317
318          if (shapeInfoFrom != shapeInfoTo) //avoid self references
319            this.Content.AddConnectionInfo(new ConnectionInfo(shapeInfoFrom, connectorFromName, shapeInfoTo, connectorToName));
320        }
321      }
322    }
323
324    private void graphVisualization_OnEntityRemoved(object sender, EntityEventArgs e) {
325      IShape shape = e.Entity as IShape;
326      if (shape != null && this.shapeInfoShapeMapping.ContainsSecond(shape)) {
327        IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape);
328        this.Content.RemoveShapeInfo(shapeInfo);
329      }
330
331      IConnection connection = e.Entity as IConnection;
332      if (connection != null && this.connectionInfoConnectionMapping.ContainsSecond(connection)) {
333        IConnectionInfo connectionInfo = connectionInfoConnectionMapping.GetBySecond(connection);
334        this.Content.RemoveConnectionInfo(connectionInfo);
335      }
336    }
337    #endregion
338
339    public void RelayoutGraph() {
340      if (this.shapeInfoShapeMapping.Count > 0
341        && this.connectionInfoConnectionMapping.Count > 0
342        && this.Content.InitialShape != null) { //otherwise the layout does not work
343        string layoutName = "Standard TreeLayout";
344        this.graphVisualization.Controller.RunActivity(layoutName);
345        this.graphVisualization.Invalidate();
346
347        //fix to avoid negative shape positions after layouting
348        Thread.Sleep(300);
349        int minX = this.graphVisualization.Controller.Model.Shapes.Min(s => s.Location.X);
350        int shiftX = minX < 0 ? Math.Abs(minX) + 50 : 0;
351        int minY = this.graphVisualization.Controller.Model.Shapes.Min(s => s.Location.Y);
352        int shiftY = minY < 0 ? Math.Abs(minY) + 50 : 0;
353        if (minX < 0 || minY < 0) {
354          foreach (IShape s in this.Controller.Model.Shapes)
355            s.MoveBy(new Point(shiftX, shiftY));
356        }
357      }
358    }
359  }
360}
Note: See TracBrowser for help on using the repository browser.