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 @ 4144

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

improved HiveExperiment GUI (HL3.3 look&feel, RunCollectionView, JobItem.ToString) (#1115)

File size: 10.3 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        Content.Prepare();
112      }
113      SetEnabledStateOfControls();
114    }
115
116    #region Content Events
117    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
118      if (InvokeRequired)
119        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
120      else
121        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
122    }
123    private void Content_Prepared(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_Prepared), sender, e);
126      else {
127        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
128        Locked = false;
129        SetEnabledStateOfExecutableButtons();
130      }
131    }
132    private void Content_Started(object sender, EventArgs e) {
133      if (InvokeRequired)
134        Invoke(new EventHandler(Content_Started), sender, e);
135      else {
136        nameTextBox.Enabled = descriptionTextBox.Enabled = false;
137        SetEnabledStateOfExecutableButtons();
138      }
139    }
140    private void Content_Paused(object sender, EventArgs e) {
141      if (InvokeRequired)
142        Invoke(new EventHandler(Content_Paused), sender, e);
143      else {
144        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
145        SetEnabledStateOfExecutableButtons();
146      }
147    }
148    private void Content_Stopped(object sender, EventArgs e) {
149      if (InvokeRequired)
150        Invoke(new EventHandler(Content_Stopped), sender, e);
151      else {
152        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
153        Locked = false;
154        SetEnabledStateOfExecutableButtons();
155      }
156    }
157    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
158      if (InvokeRequired)
159        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
160      else
161        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
162    }
163    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
164      if (InvokeRequired)
165        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
166      else
167        ErrorHandling.ShowErrorDialog(this, e.Value);
168    }
169    private void Content_IsResultsPollingChanged(object sender, EventArgs e) {
170      if (InvokeRequired)
171        Invoke(new EventHandler(Content_IsResultsPollingChanged), sender, e);
172      else {
173        SetEnabledStateOfControls();
174      }
175    }
176    #endregion
177
178    #region Control events
179    private void startButton_Click(object sender, EventArgs e) {
180      Content.Start();
181    }
182    private void pauseButton_Click(object sender, EventArgs e) {
183      Content.Pause();
184    }
185    private void stopButton_Click(object sender, EventArgs e) {
186      Content.Stop();
187    }
188    private void resetButton_Click(object sender, EventArgs e) {
189      Content.Prepare();
190    }
191
192    private void serverUrlTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
193      if (string.IsNullOrEmpty(serverUrlTextBox.Text)) {
194        e.Cancel = true;
195        errorProvider.SetError(serverUrlTextBox, "ServerUrl cannot be empty");
196        serverUrlTextBox.SelectAll();
197        return;
198      }
199      Content.ServerUrl = serverUrlTextBox.Text;
200    }
201
202    private void serverUrlTextBox_Validated(object sender, EventArgs e) {
203      errorProvider.SetError(serverUrlTextBox, string.Empty);
204    }
205
206    private void resourceIdsTextBox_Validated(object sender, EventArgs e) {
207      Content.ResourceIds = resourceIdsTextBox.Text;
208    }
209
210    private void newExperimentButton_Click(object sender, EventArgs e) {
211      Content.Experiment = new HeuristicLab.Optimization.Experiment();
212    }
213
214    private void openExperimentButton_Click(object sender, EventArgs e) {
215      OpenFileDialog openFileDialog = new OpenFileDialog();
216      openFileDialog.Title = "Open Experimenter";
217      openFileDialog.FileName = "Item";
218      openFileDialog.Multiselect = false;
219      openFileDialog.DefaultExt = "hl";
220      openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
221
222      if (openFileDialog.ShowDialog() == DialogResult.OK) {
223        Content.Experiment = (HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName);
224      }
225    }
226
227    private void showExperimentButton_Click(object sender, EventArgs e) {
228      MainFormManager.MainForm.ShowContent(Content.Experiment);
229    }
230
231    private void disconnectButton_Click(object sender, EventArgs e) {
232      if (Content != null) {
233        Content.StopResultPolling();
234        SetEnabledStateOfControls();
235      }
236    }
237
238    private void reconnectButton_Click(object sender, EventArgs e) {
239      if (Content != null) {
240        Content.StartResultPolling();
241        SetEnabledStateOfControls();
242      }
243    }
244    #endregion
245
246    #region Helpers
247    private void SetEnabledStateOfExecutableButtons() {
248      if (Content == null) {
249        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = reconnectButton.Enabled = disconnectButton.Enabled = false;
250      } else {
251        startButton.Enabled = Content.ExecutionState == ExecutionState.Prepared;
252        pauseButton.Enabled = false; // disabled for now
253        stopButton.Enabled = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
254        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
255        reconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && !Content.IsPollingResults;
256        disconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && Content.IsPollingResults;
257
258        this.Locked = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
259      }
260    }
261    #endregion
262  }
263}
Note: See TracBrowser for help on using the repository browser.