#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.Threading; using System.Windows.Forms; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Clients.Hive.Views { /// /// The base class for visual representations of items. /// [View("Experiment View")] [Content(typeof(HiveExperiment), true)] public sealed partial class HiveExperimentView : ItemView { private ProgressView progressView; public new HiveExperiment Content { get { return (HiveExperiment)base.Content; } set { base.Content = value; } } /// /// Initializes a new instance of . /// public HiveExperimentView() { InitializeComponent(); } 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.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged); Content.HiveJobChanged -= new EventHandler(Content_HiveJobChanged); Content.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged); Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged); 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.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged); Content.HiveJobChanged += new EventHandler(Content_HiveJobChanged); Content.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged); Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged); } 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.Name; executionTimeTextBox.Text = Content.ExecutionTime.ToString(); resourceNamesTextBox.Text = Content.ResourceNames; useLocalPluginsCheckBox.Checked = Content.UseLocalPlugins; //includeJobsCheckBox.Checked = Content.IncludeJobs; refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically; jobsTextBox.Text = Content.JobCount.ToString(); calculatingTextBox.Text = Content.CalculatingCount.ToString(); finishedTextBox.Text = Content.FinishedCount.ToString(); } Content_HiveJobChanged(this, EventArgs.Empty); Content_IsProgressingChanged(this, EventArgs.Empty); SetEnabledStateOfControls(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); executionTimeTextBox.Enabled = Content != null; experimentNamedItemView.ReadOnly = true; jobsTextBox.ReadOnly = true; calculatingTextBox.ReadOnly = true; finishedTextBox.ReadOnly = true; if (Content != null) { bool alreadyUploaded = Content.RootJobId != null && Content.Id != Guid.Empty; bool jobsLoaded = Content.HiveJob != null && Content.HiveJob.Job.Id != Guid.Empty; this.nameTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded; this.resourceNamesTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded; this.jobsTreeView.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded; this.useLocalPluginsCheckBox.Enabled = !(Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded); this.refreshAutomaticallyCheckBox.Enabled = alreadyUploaded && jobsLoaded && Content.ExecutionState == ExecutionState.Started; this.viewExperimentButton.Enabled = Content.GetExperiment() != null; this.openExperimentButton.Enabled = !alreadyUploaded && Content.ExecutionState == ExecutionState.Prepared; this.newExperimentButton.Enabled = !alreadyUploaded && Content.ExecutionState == ExecutionState.Prepared; this.refreshButton.Enabled = alreadyUploaded; this.Locked = Content.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 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.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_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) { jobsTreeView.Content = Content.HiveJob; experimentNamedItemView.Content = Content.GetExperiment(); } else { jobsTreeView.Content = null; experimentNamedItemView.Content = null; } SetEnabledStateOfControls(); } } private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (InvokeRequired) Invoke(new PropertyChangedEventHandler(Content_PropertyChanged), sender, e); else { jobsTextBox.Text = Content.JobCount.ToString(); calculatingTextBox.Text = Content.CalculatingCount.ToString(); finishedTextBox.Text = Content.FinishedCount.ToString(); } } #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); } private void stopButton_Click(object sender, EventArgs e) { ExperimentManagerClient.StopExperiment(Content); } private void resetButton_Click(object sender, EventArgs e) { } private void nameTextBox_Validated(object sender, EventArgs e) { if (Content.Name != nameTextBox.Text) Content.Name = nameTextBox.Text; } private void resourceNamesTextBox_Validated(object sender, EventArgs e) { if (Content.ResourceNames != resourceNamesTextBox.Text) Content.ResourceNames = resourceNamesTextBox.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 viewExperimentButton_Click(object sender, EventArgs e) { MainFormManager.MainForm.ShowContent(Content.GetExperiment()); } 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.UseLocalPlugins = useLocalPluginsCheckBox.Checked; } private void refreshButton_Click(object sender, EventArgs e) { var invoker = new Action(ExperimentManagerClient.LoadExperiment); invoker.BeginInvoke(Content, (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.GetExperiment() != null && Content.ExecutionState == ExecutionState.Prepared; pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started; stopButton.Enabled = Content.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.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(); } } #endregion } }