using System; using System.Windows.Forms; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Optimization; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Analysis.FitnessLandscape.MultiTrajectory { [View("MultiTrajectoryView")] [Content(typeof(MultiTrajectoryAnalysis), IsDefaultView = true)] public sealed partial class MultiTrajectoryAnalysisView : NamedItemView { private TypeSelectorDialog algorithmTypeSelectorDialog; public new MultiTrajectoryAnalysis Content { get { return (MultiTrajectoryAnalysis)base.Content; } set { base.Content = value; } } public MultiTrajectoryAnalysisView() { InitializeComponent(); } protected override void DeregisterContentEvents() { Content.AlgorithmChanged -= Content_AlgorithmChanged; Content.SampleGenerationAlgorithmChanged -= Content_SampleGenerationAlgorithmChanged; Content.ExecutionStateChanged -= Content_ExecutionStateChanged; Content.ExecutionTimeChanged -= Content_ExecutionTimeChanged; base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.AlgorithmChanged += Content_AlgorithmChanged; Content.SampleGenerationAlgorithmChanged += Content_SampleGenerationAlgorithmChanged; Content.ExecutionStateChanged += Content_ExecutionStateChanged; Content.ExecutionTimeChanged += Content_ExecutionTimeChanged; } #region Event Handlers (Content) void Content_AlgorithmChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(Content_AlgorithmChanged), sender, e); } else { algorithmViewHost.Content = Content.Algorithm; SetEnabledStateOfControls(); } } void Content_SampleGenerationAlgorithmChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(Content_SampleGenerationAlgorithmChanged), sender, e); } else { sampleGenerationAlgorithmViewHost.Content = Content.SampleGenerationAlgorithm; SetEnabledStateOfControls(); } } void Content_ExecutionStateChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e); } else { SetEnabledStateOfControls(); } } void Content_ExecutionTimeChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e); } else { executionTimeTextBox.Text = Content.ExecutionTime.ToString(); } } #endregion protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { parametersView.Content = null; sampleGenerationAlgorithmViewHost.Content = null; algorithmViewHost.Content = null; runCollectionView.Content = null; resultCollectionView.Content = null; executionTimeTextBox.Text = "--:--"; } else { parametersView.Content = Content.Parameters; sampleGenerationAlgorithmViewHost.Content = Content.SampleGenerationAlgorithm; algorithmViewHost.Content = Content.Algorithm; runCollectionView.Content = Content.Runs; resultCollectionView.Content = Content.Results.AsReadOnly(); executionTimeTextBox.Text = Content.ExecutionTime.ToString(); } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); parametersView.Enabled = Content != null && !ReadOnly; startButton.Enabled = Content != null && !ReadOnly && Content.ExecutionState != ExecutionState.Started && Content.ExecutionState != ExecutionState.Stopped; pauseButton.Enabled = Content != null && !ReadOnly && Content.ExecutionState == ExecutionState.Started; stopButton.Enabled = Content != null && !ReadOnly && Content.ExecutionState != ExecutionState.Prepared && Content.ExecutionState != ExecutionState.Stopped; prepareButton.Enabled = Content != null && !ReadOnly && Content.ExecutionState != ExecutionState.Paused && Content.ExecutionState != ExecutionState.Started; } #region Event Handlers (child controls) private void newAlgorithmButton_Click(object sender, System.EventArgs e) { try { IAlgorithm algorithm = SelectAlgorithm(); if (algorithm != null) Content.Algorithm = algorithm; } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } } private void openAlgorithmButton_Click(object sender, System.EventArgs e) { LoadAlgorithm( () => { newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = algorithmViewHost.Enabled = false; }, (alg) => { Content.Algorithm = alg; }, () => { newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = algorithmViewHost.Enabled = true; }); } private void newSampleGenerationAlgorithmButton_Click(object sender, System.EventArgs e) { try { IAlgorithm algorithm = SelectAlgorithm(); if (algorithm != null) Content.SampleGenerationAlgorithm = algorithm; } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } } private void openSampleGenerationAlgorithmButton_Click(object sender, System.EventArgs e) { LoadAlgorithm( () => { newSampleGenerationAlgorithmButton.Enabled = openSampleGenerationAlgorithmButton.Enabled = sampleGenerationAlgorithmViewHost.Enabled = false; }, (alg) => { Content.SampleGenerationAlgorithm = alg; }, () => { newSampleGenerationAlgorithmButton.Enabled = openSampleGenerationAlgorithmButton.Enabled = sampleGenerationAlgorithmViewHost.Enabled = true; }); } private void startButton_Click(object sender, System.EventArgs e) { Content.Start(); } private void pauseButton_Click(object sender, System.EventArgs e) { Content.Pause(); } private void stopButton_Click(object sender, System.EventArgs e) { Content.Stop(); } private void prepareButton_Click(object sender, System.EventArgs e) { Content.Prepare(); } private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.None; if (!ReadOnly && e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IAlgorithm) { if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy; else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move; else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link; } } private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) { if (e.Effect != DragDropEffects.None) { IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm; if (e.Effect.HasFlag(DragDropEffects.Copy)) algorithm = (IAlgorithm)algorithm.Clone(); if (sender == algorithmTabPage) Content.Algorithm = algorithm; if (sender == sampleGenerationTabPage) Content.SampleGenerationAlgorithm = algorithm; } } #endregion #region Auxiliary Functions private IAlgorithm SelectAlgorithm() { if (algorithmTypeSelectorDialog == null) { algorithmTypeSelectorDialog = new TypeSelectorDialog(); algorithmTypeSelectorDialog.Caption = "Select Algorithm"; algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms"; algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, false); } if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) { return (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType(); } return null; } private void LoadAlgorithm(Action pre, Action set, Action post) { openFileDialog.Title = "Open Algorithm"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { if (InvokeRequired) { Invoke(pre); } else { pre(); } ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) { try { if (error != null) throw error; IAlgorithm algorithm = content as IAlgorithm; if (algorithm == null) Invoke(new Action(() => MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error))); else set(algorithm); } catch (Exception ex) { Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex))); } finally { Invoke(post); } }); } } #endregion } }