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

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

worked on HiveExperiment (#1115)

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