#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.ComponentModel; using System.Linq; using System.Threading; using System.Windows.Forms; using HeuristicLab.Clients.Hive.ExperimentManager; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Optimization; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Clients.Hive.Views.ExperimentManager { /// /// The base class for visual representations of items. /// [View("Hive Experiment View")] [Content(typeof(RefreshableHiveExperiment), true)] public sealed partial class RefreshableHiveExperimentView : ItemView { private ProgressView progressView; public new RefreshableHiveExperiment Content { get { return (RefreshableHiveExperiment)base.Content; } set { base.Content = value; } } /// /// Initializes a new instance of . /// public RefreshableHiveExperimentView() { InitializeComponent(); } protected override void DeregisterContentEvents() { Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged); Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged); Content.HiveExperimentChanged -= new EventHandler(Content_HiveExperimentChanged); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged); Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged); Content.HiveExperimentChanged += new EventHandler(Content_HiveExperimentChanged); } private void RegisterHiveExperimentEvents() { Content.HiveExperiment.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged); Content.HiveExperiment.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged); Content.HiveExperiment.HiveJobsChanged += new EventHandler(Content_HiveJobChanged); Content.HiveExperiment.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged); } private void DeregisterHiveExperimentEvents() { Content.HiveExperiment.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged); Content.HiveExperiment.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged); Content.HiveExperiment.HiveJobsChanged -= new EventHandler(Content_HiveJobChanged); Content.HiveExperiment.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged); } private void RegisterHiveJobEvents() { Content.HiveExperiment.HiveJobs.ItemsAdded += new Collections.CollectionItemsChangedEventHandler(HiveJobs_ItemsAdded_Removed_Reset); Content.HiveExperiment.HiveJobs.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler(HiveJobs_ItemsAdded_Removed_Reset); Content.HiveExperiment.HiveJobs.CollectionReset += new Collections.CollectionItemsChangedEventHandler(HiveJobs_ItemsAdded_Removed_Reset); } private void DeregisterHiveJobEvents() { Content.HiveExperiment.HiveJobs.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler(HiveJobs_ItemsAdded_Removed_Reset); Content.HiveExperiment.HiveJobs.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler(HiveJobs_ItemsAdded_Removed_Reset); Content.HiveExperiment.HiveJobs.CollectionReset -= new Collections.CollectionItemsChangedEventHandler(HiveJobs_ItemsAdded_Removed_Reset); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { nameTextBox.Text = string.Empty; executionTimeTextBox.Text = string.Empty; resourceNamesTextBox.Text = string.Empty; useLocalPluginsCheckBox.Checked = false; logView.Content = null; //includeJobsCheckBox.Checked = false; refreshAutomaticallyCheckBox.Checked = false; jobsTextBox.Text = "0"; calculatingTextBox.Text = "0"; finishedTextBox.Text = "0"; } else { nameTextBox.Text = Content.HiveExperiment.Name; executionTimeTextBox.Text = Content.HiveExperiment.ExecutionTime.ToString(); resourceNamesTextBox.Text = Content.HiveExperiment.ResourceNames; useLocalPluginsCheckBox.Checked = Content.HiveExperiment.UseLocalPlugins; //includeJobsCheckBox.Checked = Content.IncludeJobs; refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically; jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString(); calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString(); finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString(); } Content_HiveExperimentChanged(this, EventArgs.Empty); Content_HiveJobChanged(this, EventArgs.Empty); Content_IsProgressingChanged(this, EventArgs.Empty); SetEnabledStateOfControls(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); executionTimeTextBox.Enabled = Content != null; jobsTextBox.ReadOnly = true; calculatingTextBox.ReadOnly = true; finishedTextBox.ReadOnly = true; if (Content != null) { bool alreadyUploaded = Content.Id != Guid.Empty; bool jobsLoaded = Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.All(x => x.Job.Id != Guid.Empty); this.nameTextBox.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded; this.resourceNamesTextBox.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded; this.jobsTreeView.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded; this.useLocalPluginsCheckBox.Enabled = !(Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded); this.refreshAutomaticallyCheckBox.Enabled = alreadyUploaded && jobsLoaded && Content.HiveExperiment.ExecutionState == ExecutionState.Started; this.refreshButton.Enabled = alreadyUploaded; this.Locked = Content.HiveExperiment.ExecutionState == ExecutionState.Started; } SetEnabledStateOfExecutableButtons(); } protected override void OnClosed(FormClosedEventArgs e) { if (Content != null) { if (Content.RefreshAutomatically) Content.StopResultPolling(); } base.OnClosed(e); } #region Content Events private void HiveJobs_ItemsAdded_Removed_Reset(object sender, CollectionItemsChangedEventArgs e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler(HiveJobs_ItemsAdded_Removed_Reset), sender, e); else { SetEnabledStateOfControls(); } } private void Content_ExecutionStateChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e); else SetEnabledStateOfControls(); } private void Content_Prepared(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Prepared), sender, e); else { nameTextBox.Enabled = true; Locked = false; SetEnabledStateOfControls(); } } private void Content_Started(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Started), sender, e); else { nameTextBox.Enabled = false; SetEnabledStateOfControls(); } } private void Content_Paused(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Paused), sender, e); else { nameTextBox.Enabled = true; SetEnabledStateOfControls(); } } private void Content_Stopped(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Stopped), sender, e); else { nameTextBox.Enabled = true; Locked = false; SetEnabledStateOfControls(); } } private void Content_ExecutionTimeChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e); else executionTimeTextBox.Text = Content.HiveExperiment.ExecutionTime.ToString(); } private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e); else { refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically; SetEnabledStateOfControls(); } } private void Content_HiveJobChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_HiveJobChanged), sender, e); else { if (Content != null && Content.HiveExperiment.HiveJobs != null) { jobsTreeView.Content = Content.HiveExperiment.HiveJobs; RegisterHiveJobEvents(); } else { jobsTreeView.Content = null; } SetEnabledStateOfControls(); } } private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (InvokeRequired) Invoke(new PropertyChangedEventHandler(Content_PropertyChanged), sender, e); else { jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString(); calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString(); finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString(); } } private void Content_HiveExperimentChanged(object sender, EventArgs e) { if (Content.HiveExperiment != null) { RegisterHiveExperimentEvents(); Content_IsProgressingChanged(sender, e); } } #endregion #region Control events private void startButton_Click(object sender, EventArgs e) { ExperimentManagerClient.StartExperiment(new Action((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex)), Content); } private void pauseButton_Click(object sender, EventArgs e) { ExperimentManagerClient.PauseExperiment(Content.HiveExperiment); } private void stopButton_Click(object sender, EventArgs e) { ExperimentManagerClient.StopExperiment(Content.HiveExperiment); } private void resetButton_Click(object sender, EventArgs e) { } private void nameTextBox_Validated(object sender, EventArgs e) { if (Content.HiveExperiment.Name != nameTextBox.Text) Content.HiveExperiment.Name = nameTextBox.Text; } private void resourceNamesTextBox_Validated(object sender, EventArgs e) { if (Content.HiveExperiment.ResourceNames != resourceNamesTextBox.Text) Content.HiveExperiment.ResourceNames = resourceNamesTextBox.Text; } private void includeJobsCheckBox_CheckedChanged(object sender, EventArgs e) { //if (Content != null) Content.IncludeJobs = includeJobsCheckBox.Checked; } private void refreshAutomaticallyCheckBox_CheckedChanged(object sender, EventArgs e) { if (Content != null) { Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked; } } private void useLocalPluginsCheckBox_CheckedChanged(object sender, EventArgs e) { if (Content != null) Content.HiveExperiment.UseLocalPlugins = useLocalPluginsCheckBox.Checked; } private void refreshButton_Click(object sender, EventArgs e) { var invoker = new Action(ExperimentManagerClient.LoadExperiment); invoker.BeginInvoke(Content.HiveExperiment, (ar) => { try { invoker.EndInvoke(ar); } catch (Exception ex) { ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex); } }, null); } #endregion #region Helpers private void SetEnabledStateOfExecutableButtons() { if (Content == null) { startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false; } else { startButton.Enabled = Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.Count > 0 && Content.HiveExperiment.ExecutionState == ExecutionState.Prepared; pauseButton.Enabled = Content.HiveExperiment.ExecutionState == ExecutionState.Started; stopButton.Enabled = Content.HiveExperiment.ExecutionState == ExecutionState.Started; resetButton.Enabled = false; } } #endregion #region Progress reporting private void Content_IsProgressingChanged(object sender, EventArgs e) { if (this.InvokeRequired) { Invoke(new EventHandler(Content_IsProgressingChanged), sender, e); } else { if (Content != null && Content.HiveExperiment.IsProgressing) { SetProgressView(); } else { FinishProgressView(); } } } private void SetProgressView() { if (progressView == null) { progressView = new ProgressView(this, Content.HiveExperiment.Progress); } progressView.Progress = Content.HiveExperiment.Progress; } private void FinishProgressView() { if (progressView != null) { progressView.Finish(); progressView = null; SetEnabledStateOfControls(); } } #endregion #region Drag & Drop private void jobsTreeView_DragOver(object sender, DragEventArgs e) { jobsTreeView_DragEnter(sender, e); } private void jobsTreeView_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.None; var obj = e.Data.GetData(Constants.DragDropDataFormat); if (obj is IOptimizer) { e.Effect = DragDropEffects.Copy; } } private void jobsTreeView_DragDrop(object sender, DragEventArgs e) { if (e.Effect != DragDropEffects.None) { var obj = e.Data.GetData(Constants.DragDropDataFormat); var optimizer = obj as IOptimizer; if (optimizer != null) { Content.HiveExperiment.HiveJobs.Add(new OptimizerHiveJob(optimizer)); } } } #endregion } }