Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation.Views/3.3/EvaluationTCPChannelView.cs @ 4058

Last change on this file since 4058 was 3904, checked in by mkommend, 14 years ago

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

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