#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.Linq; using System.Windows.Forms; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.MainForm.WindowsForms; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Clients.OKB { [View("OKBItem View")] [Content(typeof(OKBExperiment), true)] public sealed partial class OKBExperimentView : NamedItemView { public new OKBExperiment Content { get { return (OKBExperiment)base.Content; } set { base.Content = value; } } public OKBExperimentView() { InitializeComponent(); } protected override void OnInitialized(System.EventArgs e) { base.OnInitialized(e); OKBClient.Instance.Refreshing += new EventHandler(OKBClient_Refreshing); OKBClient.Instance.Refreshed += new EventHandler(OKBClient_Refreshed); PopulateComboBoxes(); } protected override void DeregisterContentEvents() { Content.AlgorithmIdChanged -= new EventHandler(Content_AlgorithmIdChanged); Content.ProblemIdChanged -= new EventHandler(Content_ProblemIdChanged); 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); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.AlgorithmIdChanged += new EventHandler(Content_AlgorithmIdChanged); Content.ProblemIdChanged += new EventHandler(Content_ProblemIdChanged); 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); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { algorithmComboBox.SelectedIndex = -1; problemComboBox.SelectedIndex = -1; problemParametersViewHost.Content = null; algorithmParametersViewHost.Content = null; resultsViewHost.Content = null; runsViewHost.Content = null; executionTimeTextBox.Text = "-"; } else { algorithmComboBox.SelectedItem = OKBClient.Instance.Algorithms.FirstOrDefault(x => x.Id == Content.AlgorithmId); problemComboBox.SelectedItem = OKBClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId); problemParametersViewHost.Content = Content.ProblemParameters; algorithmParametersViewHost.Content = Content.AlgorithmParameters; resultsViewHost.Content = Content.Results; runsViewHost.Content = Content.Runs; executionTimeTextBox.Text = Content.ExecutionTime.ToString(); } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); algorithmComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (algorithmComboBox.Items.Count > 0); problemComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (problemComboBox.Items.Count > 0); tabControl.Enabled = Content != null; executionTimeTextBox.Enabled = Content != null; SetEnabledStateOfExecutableButtons(); } private void PopulateComboBoxes() { algorithmComboBox.DataSource = null; problemComboBox.DataSource = null; Platform platform = OKBClient.Instance.Platforms.FirstOrDefault(x => x.Name == "HeuristicLab 3.3"); if (platform != null) { algorithmComboBox.DataSource = OKBClient.Instance.Algorithms.Where(x => x.PlatformId == platform.Id).ToList(); algorithmComboBox.DisplayMember = "Name"; problemComboBox.DataSource = OKBClient.Instance.Problems.Where(x => x.PlatformId == platform.Id).ToList(); problemComboBox.DisplayMember = "Name"; } } protected override void OnClosed(FormClosedEventArgs e) { if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop(); OKBClient.Instance.Refreshing -= new EventHandler(OKBClient_Refreshing); OKBClient.Instance.Refreshed -= new EventHandler(OKBClient_Refreshed); base.OnClosed(e); } private void OKBClient_Refreshing(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(OKBClient_Refreshing), sender, e); } else { Cursor = Cursors.AppStarting; algorithmComboBox.Enabled = problemComboBox.Enabled = tabControl.Enabled = prepareButton.Enabled = startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = executionTimeTextBox.Enabled = false; } } private void OKBClient_Refreshed(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(OKBClient_Refreshed), sender, e); } else { PopulateComboBoxes(); SetEnabledStateOfControls(); Cursor = Cursors.Default; } } #region Content Events private void Content_AlgorithmIdChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_AlgorithmIdChanged), sender, e); else { algorithmComboBox.SelectedItem = OKBClient.Instance.Algorithms.FirstOrDefault(x => x.Id == Content.AlgorithmId); algorithmParametersViewHost.Content = Content.AlgorithmParameters; resultsViewHost.Content = Content.Results; runsViewHost.Content = Content.Runs; executionTimeTextBox.Text = Content.ExecutionTime.ToString(); SetEnabledStateOfExecutableButtons(); } } private void Content_ProblemIdChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_ProblemIdChanged), sender, e); else { problemComboBox.SelectedItem = OKBClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId); problemParametersViewHost.Content = Content.ProblemParameters; SetEnabledStateOfExecutableButtons(); } } 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_ExecutionStateChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e); else startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = prepareButton.Enabled = false; } private void Content_ExecutionTimeChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e); else executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString(); } private void Content_Prepared(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Prepared), sender, e); else { resultsViewHost.Content = Content.Results; ReadOnly = Locked = false; SetEnabledStateOfExecutableButtons(); } } private void Content_Started(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Started), sender, e); else { ReadOnly = Locked = true; SetEnabledStateOfExecutableButtons(); } } private void Content_Paused(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Paused), sender, e); else { ReadOnly = Locked = false; SetEnabledStateOfExecutableButtons(); } } private void Content_Stopped(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_Stopped), sender, e); else { ReadOnly = Locked = false; SetEnabledStateOfExecutableButtons(); } } #endregion #region Control Events private void refreshButton_Click(object sender, System.EventArgs e) { OKBClient.Instance.Refresh(); } private void algorithmComboBox_SelectedValueChanged(object sender, System.EventArgs e) { Algorithm algorithm = algorithmComboBox.SelectedValue as Algorithm; if (Content != null) Content.Load(algorithm); } private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) { Problem problem = problemComboBox.SelectedValue as Problem; if (Content != null) Content.Load(problem); } private void prepareButton_Click(object sender, EventArgs e) { Content.Prepare(false); } 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(); } #endregion #region Helpers private void SetEnabledStateOfExecutableButtons() { if ((Content == null) || (Content.AlgorithmId == 0) || (Content.ProblemId == 0)) { startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = prepareButton.Enabled = false; } else { startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused); pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started; stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused); prepareButton.Enabled = Content.ExecutionState != ExecutionState.Started; } } #endregion } }