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
RevLine 
[2893]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;
[2909]37using System.Diagnostics;
38using System.Threading;
[3386]39using System.Drawing.Drawing2D;
[2893]40
41namespace HeuristicLab.Operators.Views.GraphVisualization {
[2917]42  [View("GraphVisualizationInfo View")]
[3386]43  [Content(typeof(IGraphVisualizationInfo), true)]
[3514]44  public partial class GraphVisualizationInfoView : AsynchronousContentView {
[3386]45    private BidirectionalLookup<IShapeInfo, IShape> shapeInfoShapeMapping;
46    private BidirectionalLookup<IConnectionInfo, IConnection> connectionInfoConnectionMapping;
47    private LinePenStyle connectionPenStyle;
[2893]48
49    public GraphVisualizationInfoView() {
50      InitializeComponent();
[3386]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;
[2893]55    }
56
57    public IController Controller {
58      get { return this.graphVisualization.Controller; }
59    }
60
[3386]61    public new IGraphVisualizationInfo Content {
62      get { return (IGraphVisualizationInfo)base.Content; }
[2893]63      set { base.Content = value; }
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      this.UpdateContent();
69    }
70
[3355]71    protected override void OnReadOnlyChanged() {
72      base.OnReadOnlyChanged();
[3362]73      this.SetEnabledStateOfControls();
[3355]74    }
[3514]75    protected override void OnLockedChanged() {
76      base.OnLockedChanged();
77      this.SetEnabledStateOfControls();
78    }
[3362]79    private void SetEnabledStateOfControls() {
[3386]80      DeleteTool deleteTool = (DeleteTool)this.Controller.Tools.Where(t => t.Name == ControllerBase.DeleteToolName).FirstOrDefault();
[3355]81      HeuristicLab.Netron.Controller controller = this.Controller as HeuristicLab.Netron.Controller;
82      if (Content == null && deleteTool != null && controller != null)
[3386]83        controller.RemoveTool(deleteTool);
[3355]84      else {
[3514]85        if ((ReadOnly || Locked) && deleteTool != null && controller != null)
[3355]86          controller.RemoveTool(deleteTool);
[3514]87        else if ((!ReadOnly && !Locked) && deleteTool == null)
[3355]88          this.Controller.AddTool(new DeleteTool(ControllerBase.DeleteToolName));
89      }
90    }
91
[2893]92    private void UpdateContent() {
[3386]93      foreach (IConnectionInfo connectionInfo in this.connectionInfoConnectionMapping.FirstValues)
94        this.RemoveConnectionInfo(connectionInfo);
95      this.connectionInfoConnectionMapping.Clear();
96      foreach (IShapeInfo shapeInfo in this.shapeInfoShapeMapping.FirstValues)
[2893]97        this.RemoveShapeInfo(shapeInfo);
98      this.shapeInfoShapeMapping.Clear();
99
[3355]100      if (Content != null) {
[3386]101        foreach (IShapeInfo shapeInfo in this.Content.ShapeInfos)
102          this.AddShapeInfo(shapeInfo);
103        foreach (IConnectionInfo connectionInfo in this.Content.ConnectionInfos)
104          this.AddConnectionInfo(connectionInfo);
[3355]105        this.UpdateLayoutRoot();
106      }
[3362]107      this.SetEnabledStateOfControls();
[2893]108    }
109    private void UpdateLayoutRoot() {
[3386]110      IShapeInfo shapeInfo = this.Content.InitialShape;
[2893]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
[3386]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);
[2893]131    }
132
133    protected override void DeregisterContentEvents() {
134      base.DeregisterContentEvents();
135      this.Content.InitialShapeChanged -= new EventHandler(VisualizationInfo_InitialShapeChanged);
136
[3386]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);
[2893]144    }
145
[3386]146    #region ShapeInfos
147    private void ShapeInfos_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) {
148      foreach (IShapeInfo shapeInfo in e.OldItems)
[2893]149        this.RemoveShapeInfo(shapeInfo);
[3386]150      foreach (IShapeInfo shapeInfo in e.Items)
[2893]151        this.AddShapeInfo(shapeInfo);
152    }
[3386]153    private void ShapeInfos_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) {
154      foreach (IShapeInfo shapeInfo in e.Items)
[2893]155        this.AddShapeInfo(shapeInfo);
156    }
[3386]157    private void ShapeInfos_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) {
158      foreach (IShapeInfo shapeInfo in e.Items)
[2893]159        this.RemoveShapeInfo(shapeInfo);
160    }
161
[3386]162    private void AddShapeInfo(IShapeInfo shapeInfo) {
[2893]163      this.RegisterShapeInfoEvents(shapeInfo);
164      IShape shape = shapeInfo.CreateShape();
[3386]165      this.RegisterShapeEvents(shape);
[2893]166      this.shapeInfoShapeMapping.Add(shapeInfo, shape);
167
168      this.graphVisualization.Controller.Model.AddShape(shape);
169      this.graphVisualization.Invalidate();
170    }
[3386]171    private void RemoveShapeInfo(IShapeInfo shapeInfo) {
[3514]172      this.DeregisterShapeInfoEvents(shapeInfo);
173      IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo);
174      this.DeregisterShapeEvents(shape);
175      this.shapeInfoShapeMapping.RemoveByFirst(shapeInfo);
[2893]176
[3514]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      }
[2893]182    }
183
184    private void RegisterShapeInfoEvents(IShapeInfo shapeInfo) {
[2934]185      shapeInfo.Changed += new EventHandler(shapeInfo_Changed);
[2893]186    }
187    private void DeregisterShapeInfoEvents(IShapeInfo shapeInfo) {
[2934]188      shapeInfo.Changed -= new EventHandler(shapeInfo_Changed);
[2893]189    }
190
[3386]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);
[2893]198    }
[3386]199    #endregion
[2893]200
[3386]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);
[2893]207    }
[3386]208    private void ConnectionInfos_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IConnectionInfo> e) {
209      foreach (IConnectionInfo connectionInfo in e.Items)
210        this.AddConnectionInfo(connectionInfo);
[2893]211    }
[3386]212    private void ConnectionInfos_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IConnectionInfo> e) {
213      foreach (IConnectionInfo connectionInfo in e.Items)
214        this.RemoveConnectionInfo(connectionInfo);
[2893]215    }
216
[3386]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);
[2893]221
[3386]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);
[2893]232        this.graphVisualization.Controller.Model.AddConnection(connection);
233        this.graphVisualization.Invalidate();
234      }
235    }
236
[3386]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);
[3514]242
[2893]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();
[3386]249      if (this.Controller.Model.Connections.Contains(connection)) {
[2893]250        this.graphVisualization.Controller.Model.Remove(connection);
[3386]251        this.graphVisualization.Invalidate();
252      }
[2893]253    }
254
[3386]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
[2893]268
[3386]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    }
[2893]275
[3386]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);
[2893]280    }
281
[3171]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    }
[2893]287
[3171]288    private void shape_OnMouseLeave(object sender, EntityMouseEventArgs e) {
289      this.Controller.View.CurrentCursor = this.oldCursor;
290      this.oldCursor = null;
291    }
292
[2893]293    private void shape_OnEntityChange(object sender, EntityEventArgs e) {
294      IShape shape = e.Entity as IShape;
[3386]295      IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape);
296      this.DeregisterShapeInfoEvents(shapeInfo);
297      shapeInfo.UpdateShapeInfo(shape);
298      this.RegisterShapeInfoEvents(shapeInfo);
[2893]299      this.graphVisualization.Invalidate();
300    }
301
[3386]302
[2893]303    private void graphVisualization_OnEntityAdded(object sender, EntityEventArgs e) {
304      IConnection connection = e.Entity as IConnection;
[3386]305      if (connection != null && !this.connectionInfoConnectionMapping.ContainsSecond(connection)) {
[2893]306        IConnector connectorFrom = connection.From.AttachedTo;
307        IConnector connectorTo = connection.To.AttachedTo;
308        this.RemoveConnection(connection); //is added again by the model events
309
[3386]310        if (connectorFrom != null && connectorTo != null) {
[2893]311          IShape shapeFrom = (IShape)connectorFrom.Parent;
312          IShape shapeTo = (IShape)connectorTo.Parent;
[3386]313          IShapeInfo shapeInfoFrom = this.shapeInfoShapeMapping.GetBySecond(shapeFrom);
314          IShapeInfo shapeInfoTo = this.shapeInfoShapeMapping.GetBySecond(shapeTo);
315          string connectorFromName = connectorFrom.Name;
316          string connectorToName = connectorTo.Name;
[2893]317
[3386]318          if (shapeInfoFrom != shapeInfoTo) //avoid self references
319            this.Content.AddConnectionInfo(new ConnectionInfo(shapeInfoFrom, connectorFromName, shapeInfoTo, connectorToName));
[2893]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)) {
[3386]327        IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape);
[2893]328        this.Content.RemoveShapeInfo(shapeInfo);
329      }
330
331      IConnection connection = e.Entity as IConnection;
[3386]332      if (connection != null && this.connectionInfoConnectionMapping.ContainsSecond(connection)) {
333        IConnectionInfo connectionInfo = connectionInfoConnectionMapping.GetBySecond(connection);
334        this.Content.RemoveConnectionInfo(connectionInfo);
[2893]335      }
336    }
[3386]337    #endregion
[2893]338
[3386]339    public void RelayoutGraph() {
[2893]340      if (this.shapeInfoShapeMapping.Count > 0
[3386]341        && this.connectionInfoConnectionMapping.Count > 0
[2893]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();
[2909]346
347        //fix to avoid negative shape positions after layouting
[3186]348        Thread.Sleep(300);
[2909]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        }
[2893]357      }
358    }
359  }
360}
Note: See TracBrowser for help on using the repository browser.