[6033] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.ComponentModel;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Threading;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Clients.Hive.ExperimentManager;
|
---|
| 28 | using HeuristicLab.Collections;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
| 31 | using HeuristicLab.Core.Views;
|
---|
| 32 | using HeuristicLab.MainForm;
|
---|
| 33 | using HeuristicLab.Optimization;
|
---|
| 34 | using HeuristicLab.PluginInfrastructure;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Clients.Hive.Views.ExperimentManager {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// The base class for visual representations of items.
|
---|
| 39 | /// </summary>
|
---|
| 40 | [View("Hive Experiment View")]
|
---|
| 41 | [Content(typeof(RefreshableHiveExperiment), true)]
|
---|
| 42 | public sealed partial class RefreshableHiveExperimentView : ItemView {
|
---|
| 43 | private ProgressView progressView;
|
---|
| 44 |
|
---|
| 45 | public new RefreshableHiveExperiment Content {
|
---|
| 46 | get { return (RefreshableHiveExperiment)base.Content; }
|
---|
| 47 | set { base.Content = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /// <summary>
|
---|
| 51 | /// Initializes a new instance of <see cref="ItemBaseView"/>.
|
---|
| 52 | /// </summary>
|
---|
| 53 | public RefreshableHiveExperimentView() {
|
---|
| 54 | InitializeComponent();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | protected override void DeregisterContentEvents() {
|
---|
| 58 | Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
|
---|
| 59 | Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
|
---|
| 60 | Content.HiveExperimentChanged -= new EventHandler(Content_HiveExperimentChanged);
|
---|
| 61 | base.DeregisterContentEvents();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | protected override void RegisterContentEvents() {
|
---|
| 65 | base.RegisterContentEvents();
|
---|
| 66 | Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged);
|
---|
| 67 | Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
|
---|
| 68 | Content.HiveExperimentChanged += new EventHandler(Content_HiveExperimentChanged);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | private void RegisterHiveExperimentEvents() {
|
---|
| 72 | Content.HiveExperiment.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
| 73 | Content.HiveExperiment.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
| 74 | Content.HiveExperiment.HiveJobsChanged += new EventHandler(Content_HiveJobChanged);
|
---|
| 75 | Content.HiveExperiment.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged);
|
---|
| 76 | }
|
---|
| 77 | private void DeregisterHiveExperimentEvents() {
|
---|
| 78 | Content.HiveExperiment.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
| 79 | Content.HiveExperiment.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
| 80 | Content.HiveExperiment.HiveJobsChanged -= new EventHandler(Content_HiveJobChanged);
|
---|
| 81 | Content.HiveExperiment.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | private void RegisterHiveJobEvents() {
|
---|
| 85 | Content.HiveExperiment.HiveJobs.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
|
---|
| 86 | Content.HiveExperiment.HiveJobs.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
|
---|
| 87 | Content.HiveExperiment.HiveJobs.CollectionReset += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
|
---|
| 88 | }
|
---|
| 89 | private void DeregisterHiveJobEvents() {
|
---|
| 90 | Content.HiveExperiment.HiveJobs.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
|
---|
| 91 | Content.HiveExperiment.HiveJobs.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
|
---|
| 92 | Content.HiveExperiment.HiveJobs.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | protected override void OnContentChanged() {
|
---|
| 96 | base.OnContentChanged();
|
---|
| 97 | if (Content == null) {
|
---|
| 98 | nameTextBox.Text = string.Empty;
|
---|
| 99 | executionTimeTextBox.Text = string.Empty;
|
---|
| 100 | resourceNamesTextBox.Text = string.Empty;
|
---|
| 101 | useLocalPluginsCheckBox.Checked = false;
|
---|
| 102 | logView.Content = null;
|
---|
| 103 | //includeJobsCheckBox.Checked = false;
|
---|
| 104 | refreshAutomaticallyCheckBox.Checked = false;
|
---|
| 105 | jobsTextBox.Text = "0";
|
---|
| 106 | calculatingTextBox.Text = "0";
|
---|
| 107 | finishedTextBox.Text = "0";
|
---|
| 108 | } else {
|
---|
| 109 | nameTextBox.Text = Content.HiveExperiment.Name;
|
---|
| 110 | executionTimeTextBox.Text = Content.HiveExperiment.ExecutionTime.ToString();
|
---|
| 111 | resourceNamesTextBox.Text = Content.HiveExperiment.ResourceNames;
|
---|
| 112 | useLocalPluginsCheckBox.Checked = Content.HiveExperiment.UseLocalPlugins;
|
---|
| 113 | //includeJobsCheckBox.Checked = Content.IncludeJobs;
|
---|
| 114 | refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
|
---|
| 115 | jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString();
|
---|
| 116 | calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString();
|
---|
| 117 | finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString();
|
---|
| 118 | }
|
---|
| 119 | Content_HiveExperimentChanged(this, EventArgs.Empty);
|
---|
| 120 | Content_HiveJobChanged(this, EventArgs.Empty);
|
---|
| 121 | Content_IsProgressingChanged(this, EventArgs.Empty);
|
---|
| 122 | SetEnabledStateOfControls();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | protected override void SetEnabledStateOfControls() {
|
---|
| 126 | base.SetEnabledStateOfControls();
|
---|
| 127 | executionTimeTextBox.Enabled = Content != null;
|
---|
| 128 | jobsTextBox.ReadOnly = true;
|
---|
| 129 | calculatingTextBox.ReadOnly = true;
|
---|
| 130 | finishedTextBox.ReadOnly = true;
|
---|
| 131 |
|
---|
| 132 | if (Content != null) {
|
---|
| 133 | bool alreadyUploaded = Content.Id != Guid.Empty;
|
---|
| 134 | bool jobsLoaded = Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.All(x => x.Job.Id != Guid.Empty);
|
---|
| 135 |
|
---|
| 136 | this.nameTextBox.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
|
---|
| 137 | this.resourceNamesTextBox.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
|
---|
| 138 | this.jobsTreeView.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
|
---|
| 139 | this.useLocalPluginsCheckBox.Enabled = !(Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded);
|
---|
| 140 | this.refreshAutomaticallyCheckBox.Enabled = alreadyUploaded && jobsLoaded && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
|
---|
| 141 | this.refreshButton.Enabled = alreadyUploaded;
|
---|
| 142 |
|
---|
| 143 | this.Locked = Content.HiveExperiment.ExecutionState == ExecutionState.Started;
|
---|
| 144 | }
|
---|
| 145 | SetEnabledStateOfExecutableButtons();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
| 149 | if (Content != null) {
|
---|
| 150 | if (Content.RefreshAutomatically)
|
---|
| 151 | Content.StopResultPolling();
|
---|
| 152 | }
|
---|
| 153 | base.OnClosed(e);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | #region Content Events
|
---|
| 157 | private void HiveJobs_ItemsAdded_Removed_Reset(object sender, CollectionItemsChangedEventArgs<HiveJob> e) {
|
---|
| 158 | if (InvokeRequired)
|
---|
| 159 | Invoke(new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset), sender, e);
|
---|
| 160 | else {
|
---|
| 161 | SetEnabledStateOfControls();
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | private void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
| 165 | if (InvokeRequired)
|
---|
| 166 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
| 167 | else
|
---|
| 168 | SetEnabledStateOfControls();
|
---|
| 169 | }
|
---|
| 170 | private void Content_Prepared(object sender, EventArgs e) {
|
---|
| 171 | if (InvokeRequired)
|
---|
| 172 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
| 173 | else {
|
---|
| 174 | nameTextBox.Enabled = true;
|
---|
| 175 | Locked = false;
|
---|
| 176 | SetEnabledStateOfControls();
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | private void Content_Started(object sender, EventArgs e) {
|
---|
| 180 | if (InvokeRequired)
|
---|
| 181 | Invoke(new EventHandler(Content_Started), sender, e);
|
---|
| 182 | else {
|
---|
| 183 | nameTextBox.Enabled = false;
|
---|
| 184 | SetEnabledStateOfControls();
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | private void Content_Paused(object sender, EventArgs e) {
|
---|
| 188 | if (InvokeRequired)
|
---|
| 189 | Invoke(new EventHandler(Content_Paused), sender, e);
|
---|
| 190 | else {
|
---|
| 191 | nameTextBox.Enabled = true;
|
---|
| 192 | SetEnabledStateOfControls();
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | private void Content_Stopped(object sender, EventArgs e) {
|
---|
| 196 | if (InvokeRequired)
|
---|
| 197 | Invoke(new EventHandler(Content_Stopped), sender, e);
|
---|
| 198 | else {
|
---|
| 199 | nameTextBox.Enabled = true;
|
---|
| 200 | Locked = false;
|
---|
| 201 | SetEnabledStateOfControls();
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 205 | if (InvokeRequired)
|
---|
| 206 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
| 207 | else
|
---|
| 208 | executionTimeTextBox.Text = Content.HiveExperiment.ExecutionTime.ToString();
|
---|
| 209 | }
|
---|
| 210 | private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
|
---|
| 211 | if (InvokeRequired)
|
---|
| 212 | Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
|
---|
| 213 | else {
|
---|
| 214 | refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
|
---|
| 215 | SetEnabledStateOfControls();
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | private void Content_HiveJobChanged(object sender, EventArgs e) {
|
---|
| 219 | if (InvokeRequired)
|
---|
| 220 | Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
|
---|
| 221 | else {
|
---|
| 222 | if (Content != null && Content.HiveExperiment.HiveJobs != null) {
|
---|
| 223 | jobsTreeView.Content = Content.HiveExperiment.HiveJobs;
|
---|
| 224 | RegisterHiveJobEvents();
|
---|
| 225 | } else {
|
---|
| 226 | jobsTreeView.Content = null;
|
---|
| 227 | }
|
---|
| 228 | SetEnabledStateOfControls();
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 233 | if (InvokeRequired)
|
---|
| 234 | Invoke(new PropertyChangedEventHandler(Content_PropertyChanged), sender, e);
|
---|
| 235 | else {
|
---|
| 236 | jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString();
|
---|
| 237 | calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString();
|
---|
| 238 | finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString();
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | private void Content_HiveExperimentChanged(object sender, EventArgs e) {
|
---|
| 243 | if (Content.HiveExperiment != null) {
|
---|
| 244 | RegisterHiveExperimentEvents();
|
---|
| 245 | Content_IsProgressingChanged(sender, e);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | #endregion
|
---|
| 249 |
|
---|
| 250 | #region Control events
|
---|
| 251 | private void startButton_Click(object sender, EventArgs e) {
|
---|
| 252 | ExperimentManagerClient.StartExperiment(new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex)), Content);
|
---|
| 253 | }
|
---|
| 254 | private void pauseButton_Click(object sender, EventArgs e) {
|
---|
| 255 | ExperimentManagerClient.PauseExperiment(Content.HiveExperiment);
|
---|
| 256 | }
|
---|
| 257 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
| 258 | ExperimentManagerClient.StopExperiment(Content.HiveExperiment);
|
---|
| 259 | }
|
---|
| 260 | private void resetButton_Click(object sender, EventArgs e) { }
|
---|
| 261 |
|
---|
| 262 | private void nameTextBox_Validated(object sender, EventArgs e) {
|
---|
| 263 | if (Content.HiveExperiment.Name != nameTextBox.Text)
|
---|
| 264 | Content.HiveExperiment.Name = nameTextBox.Text;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
|
---|
| 268 | if (Content.HiveExperiment.ResourceNames != resourceNamesTextBox.Text)
|
---|
| 269 | Content.HiveExperiment.ResourceNames = resourceNamesTextBox.Text;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | private void includeJobsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 273 | //if (Content != null) Content.IncludeJobs = includeJobsCheckBox.Checked;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | private void refreshAutomaticallyCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 277 | if (Content != null) {
|
---|
| 278 | Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | private void useLocalPluginsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 283 | if (Content != null) Content.HiveExperiment.UseLocalPlugins = useLocalPluginsCheckBox.Checked;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | private void refreshButton_Click(object sender, EventArgs e) {
|
---|
| 287 | var invoker = new Action<HiveExperiment>(ExperimentManagerClient.LoadExperiment);
|
---|
| 288 | invoker.BeginInvoke(Content.HiveExperiment, (ar) => {
|
---|
| 289 | try {
|
---|
| 290 | invoker.EndInvoke(ar);
|
---|
| 291 | }
|
---|
| 292 | catch (Exception ex) {
|
---|
| 293 | ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
|
---|
| 294 | }
|
---|
| 295 | }, null);
|
---|
| 296 | }
|
---|
| 297 | #endregion
|
---|
| 298 |
|
---|
| 299 | #region Helpers
|
---|
| 300 | private void SetEnabledStateOfExecutableButtons() {
|
---|
| 301 | if (Content == null) {
|
---|
| 302 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
| 303 | } else {
|
---|
| 304 | startButton.Enabled = Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.Count > 0 && Content.HiveExperiment.ExecutionState == ExecutionState.Prepared;
|
---|
| 305 | pauseButton.Enabled = Content.HiveExperiment.ExecutionState == ExecutionState.Started;
|
---|
| 306 | stopButton.Enabled = Content.HiveExperiment.ExecutionState == ExecutionState.Started;
|
---|
| 307 | resetButton.Enabled = false;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | #endregion
|
---|
| 311 |
|
---|
| 312 | #region Progress reporting
|
---|
| 313 | private void Content_IsProgressingChanged(object sender, EventArgs e) {
|
---|
| 314 | if (this.InvokeRequired) {
|
---|
| 315 | Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
|
---|
| 316 | } else {
|
---|
| 317 | if (Content != null && Content.HiveExperiment.IsProgressing) {
|
---|
| 318 | SetProgressView();
|
---|
| 319 | } else {
|
---|
| 320 | FinishProgressView();
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | private void SetProgressView() {
|
---|
| 326 | if (progressView == null) {
|
---|
| 327 | progressView = new ProgressView(this, Content.HiveExperiment.Progress);
|
---|
| 328 | }
|
---|
| 329 | progressView.Progress = Content.HiveExperiment.Progress;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | private void FinishProgressView() {
|
---|
| 333 | if (progressView != null) {
|
---|
| 334 | progressView.Finish();
|
---|
| 335 | progressView = null;
|
---|
| 336 | SetEnabledStateOfControls();
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | #endregion
|
---|
| 340 |
|
---|
| 341 | #region Drag & Drop
|
---|
| 342 | private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
|
---|
| 343 | jobsTreeView_DragEnter(sender, e);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
|
---|
| 347 | e.Effect = DragDropEffects.None;
|
---|
| 348 | var obj = e.Data.GetData(Constants.DragDropDataFormat);
|
---|
| 349 | if (obj is IOptimizer) {
|
---|
| 350 | e.Effect = DragDropEffects.Copy;
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 355 | if (e.Effect != DragDropEffects.None) {
|
---|
| 356 | var obj = e.Data.GetData(Constants.DragDropDataFormat);
|
---|
| 357 |
|
---|
| 358 | var optimizer = obj as IOptimizer;
|
---|
| 359 | if (optimizer != null) {
|
---|
| 360 | Content.HiveExperiment.HiveJobs.Add(new OptimizerHiveJob(optimizer));
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 | #endregion
|
---|
| 365 | }
|
---|
| 366 | }
|
---|