Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/MessagePortView.cs @ 11538

Last change on this file since 11538 was 11538, checked in by swagner, 9 years ago

#2205: Implemented review comments:

  • added IConnectablePort
  • replaced MessagePort.Messages by MessagePort.LastMessage
  • removed Path and PathChanged in NetworkItem

Additional changes:

  • refactored CanConnectToPort
  • refactored cloning of port parameters in MessagePortView
File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.MainForm;
23using HeuristicLab.PluginInfrastructure;
24using System;
25using System.Windows.Forms;
26
27namespace HeuristicLab.Core.Networks.Views {
28  [View("MessagePort View")]
29  [Content(typeof(MessagePort), true)]
30  [Content(typeof(IMessagePort), false)]
31  public partial class MessagePortView : PortView {
32    protected NetworkItemSelectorDialog networkItemSelectorDialog;
33
34    public new IMessagePort Content {
35      get { return (IMessagePort)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public MessagePortView() {
40      InitializeComponent();
41      errorProvider.SetIconAlignment(connectedPortView, ErrorIconAlignment.MiddleRight);
42      errorProvider.SetIconPadding(connectedPortView, 2);
43    }
44
45    protected override void Dispose(bool disposing) {
46      if (disposing) {
47        if (networkItemSelectorDialog != null) networkItemSelectorDialog.Dispose();
48        if (components != null) components.Dispose();
49      }
50      base.Dispose(disposing);
51    }
52
53    protected override void DeregisterContentEvents() {
54      Content.ConnectedPortChanged -= Content_ConnectedPortChanged;
55      Content.PortConnectionValidChanged -= Content_PortConnectionValidChanged;
56      Content.LastMessageChanged -= Content_LastMessageChanged;
57      base.DeregisterContentEvents();
58    }
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.ConnectedPortChanged += Content_ConnectedPortChanged;
62      Content.PortConnectionValidChanged += Content_PortConnectionValidChanged;
63      Content.LastMessageChanged += Content_LastMessageChanged;
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      portParameterCollectionView.Content = Content == null ? null : Content.Parameters;
69      connectedPortView.Content = Content == null ? null : Content.ConnectedPort;
70      portParameterCollectionView.Content = Content == null ? null : Content.Parameters;
71      lastMessageView.Content = Content == null ? null : Content.LastMessage;
72      errorProvider.SetError(connectedPortView, ((Content == null) || Content.PortConnectionValid) ? string.Empty : "Port connection is not valid");
73    }
74
75    protected override void SetEnabledStateOfControls() {
76      base.SetEnabledStateOfControls();
77      sendMessageButton.Enabled = Content != null;
78      connectedPortGroupBox.Enabled = Content != null && !ReadOnly;
79      setConnectedPortButton.Enabled = Content != null && !ReadOnly;
80      clearConnectedPortButton.Enabled = Content != null && Content.ConnectedPort != null && !ReadOnly;
81      portParameterCollectionView.Enabled = Content != null && !ReadOnly;
82      cloneParametersFromPortButton.Enabled = Content != null && Content.ConnectedPort != null && !ReadOnly;
83      lastMessageView.Enabled = Content != null && !ReadOnly;
84    }
85
86    protected virtual void Content_ConnectedPortChanged(object sender, EventArgs e) {
87      if (InvokeRequired)
88        Invoke(new EventHandler(Content_ConnectedPortChanged), sender, e);
89      else {
90        clearConnectedPortButton.Enabled = Content.ConnectedPort != null && !ReadOnly;
91        cloneParametersFromPortButton.Enabled = Content.ConnectedPort != null && !ReadOnly;
92        connectedPortView.Content = Content.ConnectedPort;
93      }
94    }
95    protected virtual void Content_PortConnectionValidChanged(object sender, EventArgs e) {
96      if (InvokeRequired)
97        Invoke(new EventHandler(Content_PortConnectionValidChanged), sender, e);
98      else {
99        errorProvider.SetError(connectedPortView, Content.PortConnectionValid ? string.Empty : "Port connection is not valid");
100      }
101    }
102    protected virtual void Content_LastMessageChanged(object sender, EventArgs e) {
103      if (InvokeRequired)
104        Invoke(new EventHandler(Content_LastMessageChanged), sender, e);
105      else {
106        lastMessageView.Content = Content.LastMessage;
107      }
108    }
109
110    protected virtual void sendMessageButton_Click(object sender, EventArgs e) {
111      Content.SendMessageAsync(Content.PrepareMessage());
112    }
113
114    protected virtual void setConnectedPortButton_Click(object sender, EventArgs e) {
115      if (networkItemSelectorDialog == null) networkItemSelectorDialog = new NetworkItemSelectorDialog();
116      networkItemSelectorDialog.Caption = "Select Connected Port";
117      networkItemSelectorDialog.NetworkItemSelector.Caption = "Available Ports";
118
119      try {
120        INetworkItem root = Content;
121        while (root.Parent != null)
122          root = root.Parent;
123        networkItemSelectorDialog.NetworkItemSelector.Configure(
124          root,
125          Content.ConnectedPort,
126          typeof(IMessagePort)
127        );
128
129        if (networkItemSelectorDialog.ShowDialog(this) == DialogResult.OK) {
130          Content.ConnectedPort = networkItemSelectorDialog.NetworkItemSelector.SelectedNetworkItem as IMessagePort;
131        }
132      }
133      catch (Exception ex) {
134        ErrorHandling.ShowErrorDialog(this, ex);
135      }
136    }
137    protected virtual void clearConnectedPortButton_Click(object sender, EventArgs e) {
138      Content.ConnectedPort = null;
139    }
140
141    protected virtual void connectedPortView_DragEnterOver(object sender, DragEventArgs e) {
142      e.Effect = DragDropEffects.None;
143      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IPort;
144
145      if (!ReadOnly && (data != null) && (Content.CanConnectToPort(data))) {
146        e.Effect = DragDropEffects.Link;
147      }
148    }
149    protected virtual void connectedPortView_DragDrop(object sender, DragEventArgs e) {
150      if (e.Effect != DragDropEffects.None) {
151        var port = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IMessagePort;
152        Content.ConnectedPort = port;
153      }
154    }
155    protected virtual void cloneParametersFromPortButton_Click(object sender, EventArgs e) {
156      if (networkItemSelectorDialog == null) networkItemSelectorDialog = new NetworkItemSelectorDialog();
157      networkItemSelectorDialog.Caption = "Select Port to Clone Parameters from";
158      networkItemSelectorDialog.NetworkItemSelector.Caption = "Available Ports";
159
160      INetworkItem root = Content;
161      while (root.Parent != null)
162        root = root.Parent;
163      networkItemSelectorDialog.NetworkItemSelector.Configure(
164        root,
165        null,
166        typeof(IParameterizedPort)
167      );
168
169      if (networkItemSelectorDialog.ShowDialog(this) == DialogResult.OK) {
170        Content.CloneParametersFromPort(networkItemSelectorDialog.NetworkItemSelector.SelectedNetworkItem as IParameterizedPort);
171      }
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.