using System; using System.Windows.Forms; using HeuristicLab.Common; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Operators.Views; using HeuristicLab.Optimization; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Operators { [View("EngineAlgorithmOperator View")] [Content(typeof(EngineAlgorithmOperator), IsDefaultView = true)] public sealed partial class EngineAlgorithmOperatorView : AlgorithmOperatorView { private TypeSelectorDialog algorithmTypeSelectorDialog; public new EngineAlgorithmOperator Content { get { return (EngineAlgorithmOperator)base.Content; } set { base.Content = value; } } public EngineAlgorithmOperatorView() { InitializeComponent(); } protected override void DeregisterContentEvents() { Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { algorithmViewHost.Content = null; } else { algorithmViewHost.Content = Content.Algorithm; } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); } #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; } } #endregion #region Event Handlers (child controls) private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) { if (e.Effect != DragDropEffects.None) { EngineAlgorithm algorithm = e.Data.GetData("Value") as EngineAlgorithm; if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (EngineAlgorithm)algorithm.Clone(); Content.Algorithm = algorithm; } } private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.None; Type type = e.Data.GetData("Type") as Type; if ((type != null) && (typeof(EngineAlgorithm).IsAssignableFrom(type))) { 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 newAlgorithmButton_Click(object sender, System.EventArgs e) { if (algorithmTypeSelectorDialog == null) { algorithmTypeSelectorDialog = new TypeSelectorDialog(); algorithmTypeSelectorDialog.Caption = "Select Algorithm"; algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithm"; algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(EngineAlgorithm), false, true); } if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) { try { Content.Algorithm = (EngineAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType(); } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } } } private void openAlgorithmButton_Click(object sender, System.EventArgs e) { openFileDialog.Title = "Open Algorithm"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false; algorithmViewHost.Enabled = false; ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) { try { if (error != null) throw error; EngineAlgorithm algorithm = content as EngineAlgorithm; if (algorithm == null) Invoke(new Action(() => MessageBox.Show(this, "The selected file does not contain an engine algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error))); else Content.Algorithm = algorithm; } catch (Exception ex) { Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex))); } finally { Invoke(new Action(delegate() { algorithmViewHost.Enabled = true; newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true; })); } }); } } #endregion } }