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 |
|
---|
22 | using HeuristicLab.MainForm;
|
---|
23 | using HeuristicLab.PluginInfrastructure;
|
---|
24 | using System;
|
---|
25 | using System.Windows.Forms;
|
---|
26 |
|
---|
27 | namespace 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.Parameters.IsReadOnly && !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 | connectedPortView.Content = Content.ConnectedPort;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | protected virtual void Content_PortConnectionValidChanged(object sender, EventArgs e) {
|
---|
95 | if (InvokeRequired)
|
---|
96 | Invoke(new EventHandler(Content_PortConnectionValidChanged), sender, e);
|
---|
97 | else {
|
---|
98 | errorProvider.SetError(connectedPortView, Content.PortConnectionValid ? string.Empty : "Port connection is not valid");
|
---|
99 | }
|
---|
100 | }
|
---|
101 | protected virtual void Content_LastMessageChanged(object sender, EventArgs e) {
|
---|
102 | if (InvokeRequired)
|
---|
103 | Invoke(new EventHandler(Content_LastMessageChanged), sender, e);
|
---|
104 | else {
|
---|
105 | lastMessageView.Content = Content.LastMessage;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected virtual void sendMessageButton_Click(object sender, EventArgs e) {
|
---|
110 | Content.SendMessageAsync(Content.PrepareMessage());
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected virtual void setConnectedPortButton_Click(object sender, EventArgs e) {
|
---|
114 | if (networkItemSelectorDialog == null) networkItemSelectorDialog = new NetworkItemSelectorDialog();
|
---|
115 | networkItemSelectorDialog.Caption = "Select Connected Port";
|
---|
116 | networkItemSelectorDialog.NetworkItemSelector.Caption = "Available Ports";
|
---|
117 |
|
---|
118 | try {
|
---|
119 | INetworkItem root = Content;
|
---|
120 | while (root.Parent != null)
|
---|
121 | root = root.Parent;
|
---|
122 | networkItemSelectorDialog.NetworkItemSelector.Configure(
|
---|
123 | root,
|
---|
124 | Content.ConnectedPort,
|
---|
125 | typeof(IMessagePort)
|
---|
126 | );
|
---|
127 |
|
---|
128 | if (networkItemSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
129 | Content.ConnectedPort = networkItemSelectorDialog.NetworkItemSelector.SelectedNetworkItem as IMessagePort;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | catch (Exception ex) {
|
---|
133 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | protected virtual void clearConnectedPortButton_Click(object sender, EventArgs e) {
|
---|
137 | Content.ConnectedPort = null;
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected virtual void connectedPortView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
141 | e.Effect = DragDropEffects.None;
|
---|
142 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IPort;
|
---|
143 |
|
---|
144 | if (!ReadOnly && (data != null) && (Content.CanConnectToPort(data))) {
|
---|
145 | e.Effect = DragDropEffects.Link;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | protected virtual void connectedPortView_DragDrop(object sender, DragEventArgs e) {
|
---|
149 | if (e.Effect != DragDropEffects.None) {
|
---|
150 | var port = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IMessagePort;
|
---|
151 | Content.ConnectedPort = port;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | protected virtual void cloneParametersFromPortButton_Click(object sender, EventArgs e) {
|
---|
155 | if (networkItemSelectorDialog == null) networkItemSelectorDialog = new NetworkItemSelectorDialog();
|
---|
156 | networkItemSelectorDialog.Caption = "Select Port to Clone Parameters from";
|
---|
157 | networkItemSelectorDialog.NetworkItemSelector.Caption = "Available Ports";
|
---|
158 |
|
---|
159 | INetworkItem root = Content;
|
---|
160 | while (root.Parent != null)
|
---|
161 | root = root.Parent;
|
---|
162 | networkItemSelectorDialog.NetworkItemSelector.Configure(
|
---|
163 | root,
|
---|
164 | Content,
|
---|
165 | typeof(IParameterizedPort)
|
---|
166 | );
|
---|
167 |
|
---|
168 | if (networkItemSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
169 | Content.CloneParametersFromPort(networkItemSelectorDialog.NetworkItemSelector.SelectedNetworkItem as IParameterizedPort);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|