#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.Threading; using System.Windows.Forms; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Hive.ExperimentManager.Views { /// /// The base class for visual representations of items. /// [View("Experiment View")] [Content(typeof(HiveExperiment), true)] public sealed partial class HiveExperimentView : NamedItemView { private ProgressView progressView; public new HiveExperiment Content { get { return (HiveExperiment)base.Content; } set { base.Content = value; } } /// /// Initializes a new instance of . /// public HiveExperimentView() { InitializeComponent(); downloadExperimentPanel.Dock = DockStyle.Fill; } protected override void DeregisterContentEvents() { Content.ExceptionOccurred -= new EventHandler>(Content_ExceptionOccurred); Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged); Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged); Content.Prepared -= new EventHandler(Content_Prepared); Content.Started -= new EventHandler(Content_Started); Content.Paused -= new EventHandler(Content_Paused); Content.Stopped -= new EventHandler(Content_Stopped); Content.IsResultsPollingChanged -= new EventHandler(Content_IsResultsPollingChanged); Content.HiveJobChanged -= new EventHandler(Content_HiveJobChanged); Content.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ExceptionOccurred += new EventHandler>(Content_ExceptionOccurred); Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged); Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged); Content.Prepared += new EventHandler(Content_Prepared); Content.Started += new EventHandler(Content_Started); Content.Paused += new EventHandler(Content_Paused); Content.Stopped += new EventHandler(Content_Stopped); Content.IsResultsPollingChanged += new EventHandler(Content_IsResultsPollingChanged); Content.HiveJobChanged += new EventHandler(Content_HiveJobChanged); Content.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { executionTimeTextBox.Text = string.Empty; resourceIdsTextBox.Text = string.Empty; logView.Content = null; } else { executionTimeTextBox.Text = Content.ExecutionTime.ToString(); resourceIdsTextBox.Text = Content.ResourceIds; logView.Content = Content.Log; } Content_HiveJobChanged(this, EventArgs.Empty); Content_IsProgressingChanged(this, EventArgs.Empty); SetEnabledStateOfControls(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); executionTimeTextBox.Enabled = Content != null; experimentNamedItemView.ReadOnly = true; if (Content != null) { this.nameTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared; this.descriptionTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared; this.resourceIdsTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared; this.hiveJobView.ReadOnly = Content.ExecutionState != ExecutionState.Prepared; viewExperimentButton.Enabled = Content.GetExperiment() != null; this.Locked = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults; downloadExperimentPanel.Visible = Content.HiveExperimentId != Guid.Empty && Content.GetExperiment() == null; } SetEnabledStateOfExecutableButtons(); } protected override void OnClosed(FormClosedEventArgs e) { if (Content != null) { if (Content.IsPollingResults) Content.StopResultPolling(); } base.OnClosed(e); } #region Content Events 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 = descriptionTextBox.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 = descriptionTextBox.Enabled = false; SetEnabledStateOfControls(); } } private void Content_Paused(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Paused), sender, e); else { nameTextBox.Enabled = descriptionTextBox.Enabled = true; SetEnabledStateOfControls(); } } private void Content_Stopped(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Stopped), sender, e); else { nameTextBox.Enabled = descriptionTextBox.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.ExecutionTime.ToString(); } private void Content_ExceptionOccurred(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler>(Content_ExceptionOccurred), sender, e); else ErrorHandling.ShowErrorDialog(this, e.Value); } private void Content_IsResultsPollingChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_IsResultsPollingChanged), sender, e); else { SetEnabledStateOfControls(); } } void Content_HiveJobChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_HiveJobChanged), sender, e); else { if (Content != null) { hiveJobView.Content = Content.HiveJob; experimentNamedItemView.Content = Content.GetExperiment(); } else { hiveJobView.Content = null; experimentNamedItemView.Content = null; } SetEnabledStateOfControls(); } } #endregion #region Control events private void startButton_Click(object sender, EventArgs e) { Content.Start(); } private void pauseButton_Click(object sender, EventArgs e) { Content.Pause(); } private void stopButton_Click(object sender, EventArgs e) { Content.Stop(); } private void resetButton_Click(object sender, EventArgs e) { Content.Prepare(); } private void resourceIdsTextBox_Validated(object sender, EventArgs e) { Content.ResourceIds = resourceIdsTextBox.Text; } private void newExperimentButton_Click(object sender, EventArgs e) { Content.SetExperiment(new HeuristicLab.Optimization.Experiment()); } private void openExperimentButton_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "Open Experimenter"; openFileDialog.FileName = "Item"; openFileDialog.Multiselect = false; openFileDialog.DefaultExt = "hl"; openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { Content.SetExperiment((HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName)); } } private void showExperimentButton_Click(object sender, EventArgs e) { MainFormManager.MainForm.ShowContent(Content.GetExperiment()); } private void disconnectButton_Click(object sender, EventArgs e) { if (Content != null) { Content.StopResultPolling(); SetEnabledStateOfControls(); } } private void reconnectButton_Click(object sender, EventArgs e) { if (Content != null) { Content.StartResultPolling(); SetEnabledStateOfControls(); } } #endregion #region Helpers private void SetEnabledStateOfExecutableButtons() { if (Content == null) { startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = reconnectButton.Enabled = disconnectButton.Enabled = false; } else { startButton.Enabled = Content.GetExperiment() != null && Content.ExecutionState == ExecutionState.Prepared; pauseButton.Enabled = false; stopButton.Enabled = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults; resetButton.Enabled = false; reconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && !Content.IsPollingResults; disconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && Content.IsPollingResults; } } #endregion private void downloadExperimentButton_Click(object sender, EventArgs e) { var invoker = new MethodInvoker(Content.LoadHiveJob); invoker.BeginInvoke((ar) => { try { invoker.EndInvoke(ar); } catch (Exception ex) { ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex); } }, null); } private void Content_IsProgressingChanged(object sender, EventArgs e) { if (this.InvokeRequired) { Invoke(new EventHandler(Content_IsProgressingChanged), sender, e); } else { if (Content != null && Content.IsProgressing) { SetProgressView(); } else { FinishProgressView(); } } } private void SetProgressView() { if (progressView == null) { progressView = new ProgressView(this, Content.Progress); } progressView.Progress = Content.Progress; } private void FinishProgressView() { if (progressView != null) { progressView.Finish(); progressView = null; SetEnabledStateOfControls(); } } } }