Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization.Views/3.3/ChartModes/AddPortsChartMode.cs @ 15287

Last change on this file since 15287 was 13135, checked in by jkarder, 9 years ago

#2205: worked on optimization networks

  • refactored network visualization
File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core.Networks;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Networks.Programmable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Visualization;
32
33namespace HeuristicLab.Networks.Views.NetworkVisualization.Views {
34  public class AddPortsChartMode : ChartMode {
35    protected TypeSelectorDialog typeSelectorDialog;
36
37    public override Image Image { get { return VSImageLibrary.Interface; } }
38    public override string Text { get { return "Add Por&ts"; } }
39    public override string ToolTipText { get { return "Add Ports"; } }
40
41    public AddPortsChartMode(ChartControl control) : base(control) { }
42
43    public override void HandleOnMouseMove(object sender, MouseEventArgs e) {
44      try {
45        if (!(chartControl.Tag is IUserDefinedNetwork)) return;
46
47        chartControl.RefreshPicture();
48
49        var nodeRectangles = chartControl.Chart.GetAllPrimitives(e.Location).OfType<NodeRectangle>();
50        var nodeRectangle = nodeRectangles.FirstOrDefault();
51        if (nodeRectangle == null) return;
52
53        var size = chartControl.Chart.TransformWorldToPixel(nodeRectangle.Size);
54        var ll = chartControl.Chart.TransformWorldToPixel(nodeRectangle.LowerLeft);
55        var ur = chartControl.Chart.TransformWorldToPixel(nodeRectangle.UpperRight);
56
57        var node = nodeRectangle.NetworkItem;
58        bool nodeValid = node is IAlgorithmNode || node is IUserDefinedNode;
59
60        using (var graphics = chartControl.CreatePictureGraphics())
61        using (var pen = new Pen(nodeValid ? Color.Green : Color.Red) { Width = 2f }) {
62          graphics.DrawRectangle(pen,
63            ll.X,
64            ur.Y,
65            size.Width,
66            size.Height);
67        }
68      } finally {
69        chartControl.UpdatePicture();
70        base.HandleOnMouseMove(sender, e);
71      }
72    }
73
74    public override void HandleOnMouseDown(object sender, MouseEventArgs e) {
75      try {
76        switch (e.Button) {
77          case MouseButtons.Left:
78            if (!(chartControl.Tag is IUserDefinedNetwork)) return;
79
80            var nodeRectangles = chartControl.Chart.GetAllPrimitives(e.Location).OfType<NodeRectangle>();
81            var nodeRectangle = nodeRectangles.FirstOrDefault();
82            if (nodeRectangle == null) return;
83
84            var node = nodeRectangle.NetworkItem;
85            bool nodeValid = node is IAlgorithmNode || node is IUserDefinedNode;
86            if (!nodeValid) return;
87
88            var port = CreatePort(node);
89            if (port == null) return;
90
91            var algNode = node as IAlgorithmNode;
92            if (algNode != null) {
93              algNode.Ports.Add(port);
94              return;
95            }
96
97            var userDefNode = (IUserDefinedNode)node;
98            userDefNode.Ports.Add(port);
99            break;
100        }
101      } finally {
102        base.HandleOnMouseDown(sender, e);
103      }
104    }
105
106    #region Helpers
107    protected virtual IPort CreatePort(INode node) {
108      if (typeSelectorDialog == null) {
109        typeSelectorDialog = new TypeSelectorDialog();
110        typeSelectorDialog.Caption = "Select Port";
111        typeSelectorDialog.TypeSelector.Caption = "Available Ports";
112        typeSelectorDialog.TypeSelector.Configure(typeof(IPort), false, true);
113      }
114
115      if (typeSelectorDialog.ShowDialog(chartControl) == DialogResult.OK) {
116        try {
117          var n = (IPort)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
118          n.Name = GetUniqueName(n.Name, node);
119          return n;
120        } catch (Exception ex) {
121          ErrorHandling.ShowErrorDialog(chartControl, ex);
122        }
123      }
124      return null;
125    }
126
127    protected virtual string GetUniqueName(string originalName, INode node) {
128      if (!node.Ports.ContainsKey(originalName))
129        return originalName;
130
131      string name;
132      int index = 0;
133      do {
134        index++;
135        name = originalName + index;
136      } while (node.Ports.ContainsKey(name));
137      return name;
138    }
139    #endregion
140  }
141}
Note: See TracBrowser for help on using the repository browser.