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

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

further improvement and stabilisation of HiveExperiment (#1115)

File size: 9.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      base.DeregisterContentEvents();
61    }
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
65      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
66      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
67      Content.Prepared += new EventHandler(Content_Prepared);
68      Content.Started += new EventHandler(Content_Started);
69      Content.Paused += new EventHandler(Content_Paused);
70      Content.Stopped += new EventHandler(Content_Stopped);
71      Content.ExperimentChanged += new EventHandler(Content_ExperimentChanged);
72    }
73
74    protected override void OnContentChanged() {
75      base.OnContentChanged();
76      if (Content == null) {
77        executionTimeTextBox.Text = string.Empty;
78      } else {
79        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
80        resourceIdsTextBox.Text = Content.ResourceIds;
81        serverUrlTextBox.Text = Content.ServerUrl;
82        experimentNamedItemView.Content = Content.Experiment;
83        logView.Content = Content.Log;
84        jobListView.Content = Content.JobItems;
85      }
86    }
87
88    protected override void SetEnabledStateOfControls() {
89      base.SetEnabledStateOfControls();
90      executionTimeTextBox.Enabled = Content != null;
91      if (Content != null) {
92        openExperimentButton.Enabled = Content.Experiment != null;
93        experimentNamedItemView.Locked = Content.Experiment == null;
94      }
95      SetEnabledStateOfExecutableButtons();
96    }
97
98    protected override void OnClosed(FormClosedEventArgs e) {
99      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
100      base.OnClosed(e);
101    }
102
103    private void Content_ExperimentChanged(object sender, EventArgs e) {
104      experimentNamedItemView.Content = Content.Experiment;
105      if (Content.Experiment != null) {
106        Content.Prepare();
107      }
108      SetEnabledStateOfControls();
109    }
110
111    #region Content Events
112    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
113      if (InvokeRequired)
114        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
115      else
116        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
117    }
118    private void Content_Prepared(object sender, EventArgs e) {
119      if (InvokeRequired)
120        Invoke(new EventHandler(Content_Prepared), sender, e);
121      else {
122        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
123        Locked = false;
124        SetEnabledStateOfExecutableButtons();
125      }
126    }
127    private void Content_Started(object sender, EventArgs e) {
128      if (InvokeRequired)
129        Invoke(new EventHandler(Content_Started), sender, e);
130      else {
131        nameTextBox.Enabled = descriptionTextBox.Enabled = false;
132        Locked = true;
133        SetEnabledStateOfExecutableButtons();
134      }
135    }
136    private void Content_Paused(object sender, EventArgs e) {
137      if (InvokeRequired)
138        Invoke(new EventHandler(Content_Paused), sender, e);
139      else {
140        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
141        Locked = false;
142        SetEnabledStateOfExecutableButtons();
143      }
144    }
145    private void Content_Stopped(object sender, EventArgs e) {
146      if (InvokeRequired)
147        Invoke(new EventHandler(Content_Stopped), sender, e);
148      else {
149        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
150        Locked = false;
151        SetEnabledStateOfExecutableButtons();
152      }
153    }
154    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
157      else
158        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
159    }
160    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
161      if (InvokeRequired)
162        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
163      else
164        ErrorHandling.ShowErrorDialog(this, e.Value);
165    }
166    #endregion
167
168    #region Control events
169    private void startButton_Click(object sender, EventArgs e) {
170      Content.Start();
171    }
172    private void pauseButton_Click(object sender, EventArgs e) {
173      Content.Pause();
174    }
175    private void stopButton_Click(object sender, EventArgs e) {
176      Content.Stop();
177    }
178    private void resetButton_Click(object sender, EventArgs e) {
179      Content.Prepare();
180    }
181    #endregion
182
183    #region Helpers
184    private void SetEnabledStateOfExecutableButtons() {
185      if (Content == null) {
186        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
187      } else {
188        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
189        //pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
190        pauseButton.Enabled = false; // disabled for now
191        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
192        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
193       
194      }
195    }
196    #endregion
197
198    private void serverUrlTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
199      if (string.IsNullOrEmpty(serverUrlTextBox.Text)) {
200        e.Cancel = true;
201        errorProvider.SetError(serverUrlTextBox, "ServerUrl cannot be empty");
202        serverUrlTextBox.SelectAll();
203        return;
204      }
205      Content.ServerUrl = serverUrlTextBox.Text;
206    }
207
208    private void serverUrlTextBox_Validated(object sender, EventArgs e) {
209      errorProvider.SetError(serverUrlTextBox, string.Empty);
210    }
211
212    private void resourceIdsTextBox_Validated(object sender, EventArgs e) {
213      Content.ResourceIds = resourceIdsTextBox.Text;
214    }
215
216    private void newExperimentButton_Click(object sender, EventArgs e) {
217      Content.Experiment = new HeuristicLab.Optimization.Experiment();
218    }
219
220    private void loadExperimentButton_Click(object sender, EventArgs e) {
221      OpenFileDialog openFileDialog = new OpenFileDialog();
222      openFileDialog.Title = "Open Experimenter";
223      openFileDialog.FileName = "Item";
224      openFileDialog.Multiselect = false;
225      openFileDialog.DefaultExt = "hl";
226      openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
227
228      if (openFileDialog.ShowDialog() == DialogResult.OK) {
229        Content.Experiment = (HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName);
230      }
231    }
232
233    private void openExperimentButton_Click(object sender, EventArgs e) {
234      MainFormManager.MainForm.ShowContent(Content.Experiment);
235    }
236
237  }
238}
Note: See TracBrowser for help on using the repository browser.