Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/Views/OKBExperimentView.cs @ 4549

Last change on this file since 4549 was 4549, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 9.0 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.Linq;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Clients.OKB {
33  [View("OKBItem View")]
34  [Content(typeof(OKBExperiment), true)]
35  public sealed partial class OKBExperimentView : NamedItemView {
36    public new OKBExperiment Content {
37      get { return (OKBExperiment)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public OKBExperimentView() {
42      InitializeComponent();
43    }
44
45    protected override void OnInitialized(System.EventArgs e) {
46      base.OnInitialized(e);
47      OKBClient.Instance.Refreshing += new EventHandler(OKBClient_Refreshing);
48      OKBClient.Instance.Refreshed += new EventHandler(OKBClient_Refreshed);
49      PopulateComboBoxes();
50    }
51
52    protected override void DeregisterContentEvents() {
53      Content.Changed -= new EventHandler(Content_Changed);
54      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
55      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
56      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
57      Content.Prepared -= new EventHandler(Content_Prepared);
58      Content.Started -= new EventHandler(Content_Started);
59      Content.Paused -= new EventHandler(Content_Paused);
60      Content.Stopped -= new EventHandler(Content_Stopped);
61      base.DeregisterContentEvents();
62    }
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.Changed += new EventHandler(Content_Changed);
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    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      if (Content == null) {
78        problemViewHost.Content = null;
79        parametersViewHost.Content = null;
80        resultsViewHost.Content = null;
81        runsViewHost.Content = null;
82        executionTimeTextBox.Text = "-";
83      } else {
84        problemViewHost.Content = Content.Problem;
85        parametersViewHost.Content = Content.Parameters;
86        resultsViewHost.Content = Content.Results;
87        runsViewHost.Content = Content.Runs;
88        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
89      }
90    }
91    protected override void SetEnabledStateOfControls() {
92      base.SetEnabledStateOfControls();
93      executionTimeTextBox.Enabled = Content != null;
94      SetEnabledStateOfExecutableButtons();
95    }
96
97    private void PopulateComboBoxes() {
98      Platform platform = OKBClient.Instance.Platforms.FirstOrDefault(x => x.Name == "HeuristicLab 3.3");
99      if (platform != null) {
100        algorithmComboBox.DataSource = OKBClient.Instance.Algorithms.Where(x => x.PlatformId == platform.Id).ToList();
101        algorithmComboBox.DisplayMember = "Name";
102        problemComboBox.DataSource = OKBClient.Instance.Problems.Where(x => x.PlatformId == platform.Id).ToList();
103        problemComboBox.DisplayMember = "Name";
104      }
105    }
106
107    protected override void OnClosed(FormClosedEventArgs e) {
108      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
109      base.OnClosed(e);
110    }
111
112    private void OKBClient_Refreshing(object sender, EventArgs e) {
113      if (InvokeRequired) {
114        Invoke(new EventHandler(OKBClient_Refreshing), sender, e);
115      } else {
116        Cursor = Cursors.AppStarting;
117        Enabled = false;
118      }
119    }
120    private void OKBClient_Refreshed(object sender, EventArgs e) {
121      if (InvokeRequired) {
122        Invoke(new EventHandler(OKBClient_Refreshed), sender, e);
123      } else {
124        PopulateComboBoxes();
125        Enabled = true;
126        Cursor = Cursors.Default;
127      }
128    }
129
130    #region Content Events
131    private void Content_Changed(object sender, EventArgs e) {
132      OnContentChanged();
133      SetEnabledStateOfControls();
134    }
135    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
136      if (InvokeRequired)
137        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
138      else
139        ErrorHandling.ShowErrorDialog(this, e.Value);
140    }
141    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
142      if (InvokeRequired)
143        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
144      else
145        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = prepareButton.Enabled = false;
146    }
147    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
148      if (InvokeRequired)
149        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
150      else
151        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
152    }
153    private void Content_Prepared(object sender, EventArgs e) {
154      if (InvokeRequired)
155        Invoke(new EventHandler(Content_Prepared), sender, e);
156      else {
157        ReadOnly = Locked = false;
158        SetEnabledStateOfExecutableButtons();
159      }
160    }
161    private void Content_Started(object sender, EventArgs e) {
162      if (InvokeRequired)
163        Invoke(new EventHandler(Content_Started), sender, e);
164      else {
165        ReadOnly = Locked = true;
166        SetEnabledStateOfExecutableButtons();
167      }
168    }
169    private void Content_Paused(object sender, EventArgs e) {
170      if (InvokeRequired)
171        Invoke(new EventHandler(Content_Paused), sender, e);
172      else {
173        ReadOnly = Locked = false;
174        SetEnabledStateOfExecutableButtons();
175      }
176    }
177    private void Content_Stopped(object sender, EventArgs e) {
178      if (InvokeRequired)
179        Invoke(new EventHandler(Content_Stopped), sender, e);
180      else {
181        ReadOnly = Locked = false;
182        SetEnabledStateOfExecutableButtons();
183      }
184    }
185    #endregion
186
187    #region Control Events
188    private void refreshButton_Click(object sender, System.EventArgs e) {
189      OKBClient.Instance.Refresh();
190    }
191    private void algorithmComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
192      Algorithm algorithm = algorithmComboBox.SelectedValue as Algorithm;
193      if ((algorithm != null) && (Content != null)) {
194        Content.LoadAlgorithm(algorithm.Id);
195      }
196    }
197    private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
198      Problem problem = problemComboBox.SelectedValue as Problem;
199      if ((problem != null) && (Content != null)) {
200        Content.LoadProblem(problem.Id);
201      }
202    }
203    private void prepareButton_Click(object sender, EventArgs e) {
204      Content.Prepare(false);
205    }
206    private void startButton_Click(object sender, EventArgs e) {
207      Content.Start();
208    }
209    private void pauseButton_Click(object sender, EventArgs e) {
210      Content.Pause();
211    }
212    private void stopButton_Click(object sender, EventArgs e) {
213      Content.Stop();
214    }
215    #endregion
216
217    #region Helpers
218    private void SetEnabledStateOfExecutableButtons() {
219      if (Content == null) {
220        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = prepareButton.Enabled = false;
221      } else {
222        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
223        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
224        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
225        prepareButton.Enabled = Content.ExecutionState != ExecutionState.Started;
226      }
227    }
228    #endregion
229  }
230}
Note: See TracBrowser for help on using the repository browser.