Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation.Views/3.4/EvaluationTCPChannelView.cs @ 14559

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

#2526: Updated year of copyrights in license headers

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.ComponentModel;
24using System.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Problems.ExternalEvaluation.Views {
29  [View("TCP Channel View")]
30  [Content(typeof(EvaluationTCPChannel), IsDefaultView = true)]
31  public sealed partial class EvaluationTCPChannelView : NamedItemView {
32    public new EvaluationTCPChannel Content {
33      get { return (EvaluationTCPChannel)base.Content; }
34      set { base.Content = value; }
35    }
36
37    public EvaluationTCPChannelView() {
38      InitializeComponent();
39    }
40
41    protected override void DeregisterContentEvents() {
42      Content.Connected -= new EventHandler(Content_Connected);
43      Content.Disconnected -= new EventHandler(Content_Disconnected);
44      Content.IpAddressChanged -= new EventHandler(Content_IpAddressChanged);
45      Content.PortChanged -= new EventHandler(Content_PortChanged);
46      base.DeregisterContentEvents();
47    }
48
49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      Content.Connected += new EventHandler(Content_Connected);
52      Content.Disconnected += new EventHandler(Content_Disconnected);
53      Content.IpAddressChanged += new EventHandler(Content_IpAddressChanged);
54      Content.PortChanged += new EventHandler(Content_PortChanged);
55    }
56
57    #region Event Handlers (Content)
58    private void Content_Connected(object sender, EventArgs e) {
59      SetEnabledStateOfControls();
60    }
61    private void Content_Disconnected(object sender, EventArgs e) {
62      SetEnabledStateOfControls();
63    }
64    private void Content_IpAddressChanged(object sender, EventArgs e) {
65      ipAddressTextBox.Text = Content.IpAddress;
66    }
67    private void Content_PortChanged(object sender, EventArgs e) {
68      portTextBox.Text = Content.Port.ToString();
69    }
70    #endregion
71
72    protected override void OnContentChanged() {
73      base.OnContentChanged();
74      if (Content == null) {
75        ipAddressTextBox.Text = String.Empty;
76        portTextBox.Text = String.Empty;
77      } else {
78        ipAddressTextBox.Text = Content.IpAddress;
79        portTextBox.Text = Content.Port.ToString();
80      }
81    }
82
83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
85      bool readOnlyDriverNullOrStarted = ReadOnly || Content == null || Content.IsInitialized;
86      ipAddressTextBox.Enabled = !readOnlyDriverNullOrStarted;
87      portTextBox.Enabled = !readOnlyDriverNullOrStarted;
88      connectButton.Enabled = !readOnlyDriverNullOrStarted;
89      disconnectButton.Enabled = !ReadOnly && Content != null && Content.IsInitialized;
90    }
91
92    #region Event Handlers (child controls)
93    private void ipAddressTextBox_Validating(object sender, CancelEventArgs e) {
94      if (Content != null) {
95        try {
96          System.Net.IPAddress.Parse(ipAddressTextBox.Text);
97        }
98        catch (FormatException) {
99          e.Cancel = true;
100        }
101        if (!e.Cancel) Content.IpAddress = ipAddressTextBox.Text;
102      }
103    }
104    private void portTextBox_Validating(object sender, CancelEventArgs e) {
105      if (Content != null) {
106        int port;
107        e.Cancel = !int.TryParse(portTextBox.Text, out port);
108        if (!e.Cancel) Content.Port = port;
109      }
110    }
111    private void connectButton_Click(object sender, EventArgs e) {
112      try {
113        Content.Open();
114      }
115      catch (Exception ex) {
116        PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
117      }
118    }
119    private void disconnectButton_Click(object sender, EventArgs e) {
120      try {
121        Content.Close();
122      }
123      catch (Exception ex) {
124        PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
125      }
126    }
127    #endregion
128  }
129}
Note: See TracBrowser for help on using the repository browser.