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

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

added authorizationManager which checks for permission to specific jobs (#1168)

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