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

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

#2205: Worked on optimization networks

File size: 6.2 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.Optimization.Networks.Views {
28  [View("GenericPort View")]
29  [Content(typeof(GenericPort), true)]
30  [Content(typeof(IGenericPort), false)]
31  public partial class GenericPortView : PortView {
32    protected EntitySelectorDialog entitySelectorDialog;
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 (entitySelectorDialog != null) entitySelectorDialog.Dispose();
48        if (components != null) components.Dispose();
49      }
50      base.Dispose(disposing);
51    }
52
53    protected override void DeregisterContentEvents() {
54      Content.InterfaceChanged -= Content_InterfaceChanged;
55      Content.ConnectedPortChanged -= Content_ConnectedPortChanged;
56      base.DeregisterContentEvents();
57    }
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      Content.InterfaceChanged += Content_InterfaceChanged;
61      Content.ConnectedPortChanged += Content_ConnectedPortChanged;
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      connectedPortView.Content = Content == null ? null : Content.ConnectedPort;
67      portParameterCollectionView.Content = Content == null ? null : Content.Parameters;
68      messageCollectionView.Content = Content == null ? null : Content.Messages;
69      errorProvider.SetError(connectedPortView, ((Content == null) || Content.PortConnectionValid) ? string.Empty : "Port connection is not valid");
70    }
71
72    protected override void SetEnabledStateOfControls() {
73      base.SetEnabledStateOfControls();
74      sendMessageButton.Enabled = Content != null;
75      connectedPortGroupBox.Enabled = Content != null && !ReadOnly;
76      setConnectedPortButton.Enabled = Content != null && !ReadOnly;
77      clearConnectedPortButton.Enabled = Content != null && Content.ConnectedPort != null && !ReadOnly;
78      portParameterCollectionView.Enabled = Content != null && !ReadOnly;
79      cloneConnectedPortParametersButton.Enabled = Content != null && Content.ConnectedPort != null && !ReadOnly;
80      messageCollectionView.Enabled = Content != null && !ReadOnly;
81    }
82
83    protected virtual void Content_ConnectedPortChanged(object sender, EventArgs e) {
84      if (InvokeRequired)
85        Invoke(new EventHandler(Content_ConnectedPortChanged), sender, e);
86      else {
87        clearConnectedPortButton.Enabled = Content.ConnectedPort != null && !ReadOnly;
88        cloneConnectedPortParametersButton.Enabled = Content.ConnectedPort != null && !ReadOnly;
89        connectedPortView.Content = Content.ConnectedPort;
90      }
91    }
92    protected virtual void Content_InterfaceChanged(object sender, EventArgs e) {
93      if (InvokeRequired)
94        Invoke(new EventHandler(Content_InterfaceChanged), sender, e);
95      else {
96        errorProvider.SetError(connectedPortView, Content.PortConnectionValid ? string.Empty : "Port connection is not valid");
97      }
98    }
99
100    protected virtual void sendMessageButton_Click(object sender, EventArgs e) {
101      Content.SendMessageAsync(Content.PrepareMessage());
102    }
103
104    protected virtual void setConnectedPortButton_Click(object sender, EventArgs e) {
105      if (entitySelectorDialog == null) {
106        entitySelectorDialog = new EntitySelectorDialog();
107        entitySelectorDialog.Caption = "Select Connected Port";
108        entitySelectorDialog.EntitySelector.Caption = "Available Ports";
109      }
110
111      try {
112        IEntity root = Content;
113        while (root.Parent != null)
114          root = root.Parent;
115        entitySelectorDialog.EntitySelector.Configure(
116          root,
117          Content.ConnectedPort,
118          typeof(IConnectedPort)
119        );
120
121        if (entitySelectorDialog.ShowDialog(this) == DialogResult.OK) {
122          Content.ConnectedPort = entitySelectorDialog.EntitySelector.SelectedEntity as IConnectedPort;
123        }
124      }
125      catch (Exception ex) {
126        ErrorHandling.ShowErrorDialog(this, ex);
127      }
128    }
129    protected virtual void clearConnectedPortButton_Click(object sender, EventArgs e) {
130      Content.ConnectedPort = null;
131    }
132
133    protected virtual void connectedPortView_DragEnterOver(object sender, DragEventArgs e) {
134      e.Effect = DragDropEffects.None;
135      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IConnectedPort;
136
137      if (!ReadOnly && (data != null) && (Content.CanConnectToPort(data))) {
138        e.Effect = DragDropEffects.Link;
139      }
140    }
141    protected virtual void connectedPortView_DragDrop(object sender, DragEventArgs e) {
142      if (e.Effect != DragDropEffects.None) {
143        var port = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IConnectedPort;
144        Content.ConnectedPort = port;
145      }
146    }
147    protected virtual void cloneConnectedPortParametersButton_Click(object sender, EventArgs e) {
148      Content.CloneConnectedPortParameters();
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.