source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/GenericPortView.cs @ 11528

Last change on this file since 11528 was 11528, checked in by swagner, 8 years ago

#2205: Implemented review comments

  • adapted formatting of multi-line LINQ queries
  • renamed Entity to NetworkItem
File size: 7.0 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("GenericPort View")]
29  [Content(typeof(GenericPort), true)]
30  [Content(typeof(IGenericPort), false)]
31  public partial class GenericPortView : PortView {
32    protected NetworkItemSelectorDialog networkItemSelectorDialog;
33
34    public new IGenericPort Content {
35      get { return (IGenericPort)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public GenericPortView() {
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.LogMessagesChanged -= Content_LogMessagesChanged;
57      base.DeregisterContentEvents();
58    }
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.ConnectedPortChanged += Content_ConnectedPortChanged;
62      Content.PortConnectionValidChanged += Content_PortConnectionValidChanged;
63      Content.LogMessagesChanged += Content_LogMessagesChanged;
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      connectedPortView.Content = Content == null ? null : Content.ConnectedPort;
69      portParameterCollectionView.Content = Content == null ? null : Content.Parameters;
70      logMessagesCheckBox.Checked = Content == null ? false : Content.LogMessages;
71      messageCollectionView.Content = Content == null ? null : Content.Messages;
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      cloneConnectedPortParametersButton.Enabled = Content != null && Content.ConnectedPort != null && !ReadOnly;
83      logMessagesCheckBox.Enabled = Content != null && !ReadOnly;
84      messageCollectionView.Enabled = Content != null && !ReadOnly;
85    }
86
87    protected virtual void Content_ConnectedPortChanged(object sender, EventArgs e) {
88      if (InvokeRequired)
89        Invoke(new EventHandler(Content_ConnectedPortChanged), sender, e);
90      else {
91        clearConnectedPortButton.Enabled = Content.ConnectedPort != null && !ReadOnly;
92        cloneConnectedPortParametersButton.Enabled = Content.ConnectedPort != null && !ReadOnly;
93        connectedPortView.Content = Content.ConnectedPort;
94      }
95    }
96    protected virtual void Content_PortConnectionValidChanged(object sender, EventArgs e) {
97      if (InvokeRequired)
98        Invoke(new EventHandler(Content_PortConnectionValidChanged), sender, e);
99      else {
100        errorProvider.SetError(connectedPortView, Content.PortConnectionValid ? string.Empty : "Port connection is not valid");
101      }
102    }
103    protected virtual void Content_LogMessagesChanged(object sender, EventArgs e) {
104      if (InvokeRequired)
105        Invoke(new EventHandler(Content_LogMessagesChanged), sender, e);
106      else {
107        logMessagesCheckBox.Checked = Content.LogMessages;
108      }
109    }
110
111    protected virtual void sendMessageButton_Click(object sender, EventArgs e) {
112      Content.SendMessageAsync(Content.PrepareMessage());
113    }
114
115    protected virtual void setConnectedPortButton_Click(object sender, EventArgs e) {
116      if (networkItemSelectorDialog == null) {
117        networkItemSelectorDialog = new NetworkItemSelectorDialog();
118        networkItemSelectorDialog.Caption = "Select Connected Port";
119        networkItemSelectorDialog.NetworkItemSelector.Caption = "Available Ports";
120      }
121
122      try {
123        INetworkItem root = Content;
124        while (root.Parent != null)
125          root = root.Parent;
126        networkItemSelectorDialog.NetworkItemSelector.Configure(
127          root,
128          Content.ConnectedPort,
129          typeof(IConnectedPort)
130        );
131
132        if (networkItemSelectorDialog.ShowDialog(this) == DialogResult.OK) {
133          Content.ConnectedPort = networkItemSelectorDialog.NetworkItemSelector.SelectedNetworkItem as IConnectedPort;
134        }
135      }
136      catch (Exception ex) {
137        ErrorHandling.ShowErrorDialog(this, ex);
138      }
139    }
140    protected virtual void clearConnectedPortButton_Click(object sender, EventArgs e) {
141      Content.ConnectedPort = null;
142    }
143
144    protected virtual void connectedPortView_DragEnterOver(object sender, DragEventArgs e) {
145      e.Effect = DragDropEffects.None;
146      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IConnectedPort;
147
148      if (!ReadOnly && (data != null) && (Content.CanConnectToPort(data))) {
149        e.Effect = DragDropEffects.Link;
150      }
151    }
152    protected virtual void connectedPortView_DragDrop(object sender, DragEventArgs e) {
153      if (e.Effect != DragDropEffects.None) {
154        var port = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IConnectedPort;
155        Content.ConnectedPort = port;
156      }
157    }
158    protected virtual void cloneConnectedPortParametersButton_Click(object sender, EventArgs e) {
159      Content.CloneConnectedPortParameters();
160    }
161    protected virtual void logMessagesCheckBox_CheckedChanged(object sender, EventArgs e) {
162      Content.LogMessages = logMessagesCheckBox.Checked;
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.