Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment.Views/3.3/HiveExperimentView.cs @ 4170

Last change on this file since 4170 was 4170, checked in by cneumuel, 14 years ago

refactoring of Result-Polling of HiveExperiment, polling is now much faster and code is cleaner (1092#)

File size: 10.4 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.PluginInfrastructure;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Views;
31
32namespace HeuristicLab.Hive.Experiment.Views {
33  /// <summary>
34  /// The base class for visual representations of items.
35  /// </summary>
36  [View("Experiment View")]
37  [Content(typeof(HiveExperiment), true)]
38  public sealed partial class HiveExperimentView : NamedItemView {
39    public new HiveExperiment Content {
40      get { return (HiveExperiment)base.Content; }
41      set { base.Content = value; }
42    }
43
44    /// <summary>
45    /// Initializes a new instance of <see cref="ItemBaseView"/>.
46    /// </summary>
47    public HiveExperimentView() {
48      InitializeComponent();
49    }
50
51    protected override void DeregisterContentEvents() {
52      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
53      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
54      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
55      Content.Prepared -= new EventHandler(Content_Prepared);
56      Content.Started -= new EventHandler(Content_Started);
57      Content.Paused -= new EventHandler(Content_Paused);
58      Content.Stopped -= new EventHandler(Content_Stopped);
59      Content.ExperimentChanged -= new EventHandler(Content_ExperimentChanged);
60      Content.IsResultsPollingChanged -= new EventHandler(Content_IsResultsPollingChanged);
61      base.DeregisterContentEvents();
62    }
63
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
67      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
68      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
69      Content.Prepared += new EventHandler(Content_Prepared);
70      Content.Started += new EventHandler(Content_Started);
71      Content.Paused += new EventHandler(Content_Paused);
72      Content.Stopped += new EventHandler(Content_Stopped);
73      Content.IsResultsPollingChanged += new EventHandler(Content_IsResultsPollingChanged);
74      Content.ExperimentChanged += new EventHandler(Content_ExperimentChanged);
75    }
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        executionTimeTextBox.Text = string.Empty;
81      } else {
82        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
83        resourceIdsTextBox.Text = Content.ResourceIds;
84        serverUrlTextBox.Text = Content.ServerUrl;
85        experimentNamedItemView.Content = Content.Experiment;
86        logView.Content = Content.Log;
87        jobListView.Content = Content.JobItems;
88        Content_ExperimentChanged(this, EventArgs.Empty);
89      }
90    }
91
92    protected override void SetEnabledStateOfControls() {
93      base.SetEnabledStateOfControls();
94      executionTimeTextBox.Enabled = Content != null;
95      if (Content != null) {
96        viewExperimentButton.Enabled = Content.Experiment != null;
97        experimentNamedItemView.Locked = Content.Experiment == null;
98      }
99      SetEnabledStateOfExecutableButtons();
100    }
101
102    protected override void OnClosed(FormClosedEventArgs e) {
103      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
104      base.OnClosed(e);
105    }
106
107    private void Content_ExperimentChanged(object sender, EventArgs e) {
108      experimentNamedItemView.Content = Content.Experiment;
109      if (Content.Experiment != null) {
110        runCollectionView.Content = Content.Experiment.Runs;
111        if (Content.ExecutionState == ExecutionState.Stopped) {
112          Content.Prepare();
113        }
114      }
115      SetEnabledStateOfControls();
116    }
117
118    #region Content Events
119    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
120      if (InvokeRequired)
121        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
122      else
123        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
124    }
125    private void Content_Prepared(object sender, EventArgs e) {
126      if (InvokeRequired)
127        Invoke(new EventHandler(Content_Prepared), sender, e);
128      else {
129        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
130        Locked = false;
131        SetEnabledStateOfExecutableButtons();
132      }
133    }
134    private void Content_Started(object sender, EventArgs e) {
135      if (InvokeRequired)
136        Invoke(new EventHandler(Content_Started), sender, e);
137      else {
138        nameTextBox.Enabled = descriptionTextBox.Enabled = false;
139        SetEnabledStateOfExecutableButtons();
140      }
141    }
142    private void Content_Paused(object sender, EventArgs e) {
143      if (InvokeRequired)
144        Invoke(new EventHandler(Content_Paused), sender, e);
145      else {
146        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
147        SetEnabledStateOfExecutableButtons();
148      }
149    }
150    private void Content_Stopped(object sender, EventArgs e) {
151      if (InvokeRequired)
152        Invoke(new EventHandler(Content_Stopped), sender, e);
153      else {
154        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
155        Locked = false;
156        SetEnabledStateOfExecutableButtons();
157      }
158    }
159    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
160      if (InvokeRequired)
161        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
162      else
163        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
164    }
165    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
166      if (InvokeRequired)
167        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
168      else
169        ErrorHandling.ShowErrorDialog(this, e.Value);
170    }
171    private void Content_IsResultsPollingChanged(object sender, EventArgs e) {
172      if (InvokeRequired)
173        Invoke(new EventHandler(Content_IsResultsPollingChanged), sender, e);
174      else {
175        SetEnabledStateOfControls();
176      }
177    }
178    #endregion
179
180    #region Control events
181    private void startButton_Click(object sender, EventArgs e) {
182      Content.Start();
183    }
184    private void pauseButton_Click(object sender, EventArgs e) {
185      Content.Pause();
186    }
187    private void stopButton_Click(object sender, EventArgs e) {
188      Content.Stop();
189    }
190    private void resetButton_Click(object sender, EventArgs e) {
191      Content.Prepare();
192    }
193
194    private void serverUrlTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
195      if (string.IsNullOrEmpty(serverUrlTextBox.Text)) {
196        e.Cancel = true;
197        errorProvider.SetError(serverUrlTextBox, "ServerUrl cannot be empty");
198        serverUrlTextBox.SelectAll();
199        return;
200      }
201      Content.ServerUrl = serverUrlTextBox.Text;
202    }
203
204    private void serverUrlTextBox_Validated(object sender, EventArgs e) {
205      errorProvider.SetError(serverUrlTextBox, string.Empty);
206    }
207
208    private void resourceIdsTextBox_Validated(object sender, EventArgs e) {
209      Content.ResourceIds = resourceIdsTextBox.Text;
210    }
211
212    private void newExperimentButton_Click(object sender, EventArgs e) {
213      Content.Experiment = new HeuristicLab.Optimization.Experiment();
214    }
215
216    private void openExperimentButton_Click(object sender, EventArgs e) {
217      OpenFileDialog openFileDialog = new OpenFileDialog();
218      openFileDialog.Title = "Open Experimenter";
219      openFileDialog.FileName = "Item";
220      openFileDialog.Multiselect = false;
221      openFileDialog.DefaultExt = "hl";
222      openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
223
224      if (openFileDialog.ShowDialog() == DialogResult.OK) {
225        Content.Experiment = (HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName);
226      }
227    }
228
229    private void showExperimentButton_Click(object sender, EventArgs e) {
230      MainFormManager.MainForm.ShowContent(Content.Experiment);
231    }
232
233    private void disconnectButton_Click(object sender, EventArgs e) {
234      if (Content != null) {
235        Content.StopResultPolling();
236        SetEnabledStateOfControls();
237      }
238    }
239
240    private void reconnectButton_Click(object sender, EventArgs e) {
241      if (Content != null) {
242        Content.StartResultPolling();
243        SetEnabledStateOfControls();
244      }
245    }
246    #endregion
247
248    #region Helpers
249    private void SetEnabledStateOfExecutableButtons() {
250      if (Content == null) {
251        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = reconnectButton.Enabled = disconnectButton.Enabled = false;
252      } else {
253        startButton.Enabled = Content.ExecutionState == ExecutionState.Prepared;
254        pauseButton.Enabled = false; // disabled for now
255        stopButton.Enabled = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
256        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
257        reconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && !Content.IsPollingResults;
258        disconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && Content.IsPollingResults;
259
260        this.Locked = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
261      }
262    }
263    #endregion
264  }
265}
Note: See TracBrowser for help on using the repository browser.