Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: worked on optimization networks

  • added first version of network visualization
  • updated frame files
File size: 4.6 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 ToolTip { get { return "Add Ports"; } }
39
40    public AddPortsChartMode(ChartControl control) : base(control) { }
41
42    public override void HandleOnMouseMove(object sender, MouseEventArgs e) {
43      base.HandleOnMouseMove(sender, e);
44
45      if (!(chartControl.Tag is IUserDefinedNetwork)) return;
46
47      try {
48        chartControl.PictureBox.Refresh();
49
50        var nodeRectangles = chartControl.Chart.GetAllPrimitives(e.Location).OfType<NodeRectangle>();
51        var nodeRectangle = nodeRectangles.FirstOrDefault();
52        if (nodeRectangle == null) return;
53
54        var size = chartControl.Chart.TransformWorldToPixel(nodeRectangle.Size);
55        var ll = chartControl.Chart.TransformWorldToPixel(nodeRectangle.LowerLeft);
56        var ur = chartControl.Chart.TransformWorldToPixel(nodeRectangle.UpperRight);
57
58        using (var graphics = chartControl.PictureBox.CreateGraphics())
59        using (var pen = new Pen(Color.Green) { Width = 2f }) {
60          graphics.DrawRectangle(pen,
61            ll.X,
62            ur.Y,
63            size.Width,
64            size.Height);
65        }
66      } finally {
67        chartControl.PictureBox.Update();
68      }
69    }
70
71    public override void HandleOnMouseUp(object sender, MouseEventArgs e) {
72      base.HandleOnMouseUp(sender, e);
73
74      switch (e.Button) {
75        case MouseButtons.Left:
76          var network = chartControl.Tag as IUserDefinedNetwork;
77          if (network == null) return;
78
79          var nodeRectangles = chartControl.Chart.GetAllPrimitives(e.Location).OfType<NodeRectangle>();
80          var nodeRectangle = nodeRectangles.FirstOrDefault();
81          if (nodeRectangle == null) return;
82
83          var node = nodeRectangle.NetworkItem;
84          var port = CreatePort(node);
85
86          var algNode = node as IAlgorithmNode;
87          if (algNode != null) {
88            algNode.Ports.Add(port);
89            return;
90          }
91
92          var userDefNode = node as IUserDefinedNode;
93          if (userDefNode != null) {
94            userDefNode.Ports.Add(port);
95            return;
96          }
97          break;
98      }
99    }
100
101    #region Helpers
102    protected virtual IPort CreatePort(INode node) {
103      if (typeSelectorDialog == null) {
104        typeSelectorDialog = new TypeSelectorDialog();
105        typeSelectorDialog.Caption = "Select Port";
106        typeSelectorDialog.TypeSelector.Caption = "Available Ports";
107        typeSelectorDialog.TypeSelector.Configure(typeof(IPort), false, true);
108      }
109
110      if (typeSelectorDialog.ShowDialog(chartControl) == DialogResult.OK) {
111        try {
112          var n = (IPort)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
113          n.Name = GetUniqueName(n.Name, node);
114          return n;
115        } catch (Exception ex) {
116          ErrorHandling.ShowErrorDialog(chartControl, ex);
117        }
118      }
119      return null;
120    }
121
122    protected virtual string GetUniqueName(string originalName, INode node) {
123      if (!node.Ports.ContainsKey(originalName))
124        return originalName;
125
126      string name;
127      int index = 0;
128      do {
129        index++;
130        name = originalName + index;
131      } while (node.Ports.ContainsKey(name));
132      return name;
133    }
134    #endregion
135  }
136}
Note: See TracBrowser for help on using the repository browser.