Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RefactorPluginInfrastructure-2522/HeuristicLab.Optimization.Views/3.3/AlgorithmView.cs @ 13820

Last change on this file since 13820 was 13338, checked in by gkronber, 9 years ago

#2522:

  • moved UI components out of HeuristicLab.PluginInfrastructure -> HeuristicLab.PluginInfrastructure.UI
  • moved ErrorDialog to HeuristicLab.MainForm.WindowsForms
  • moved ErrorHandling (for building an error message string) to HeuristicLab.Common
  • Changed exception handlers in Views to use MainForm.ShowError()
  • Changed usages for ErrorDialog in non-UI components to throw exceptions instead.
File size: 9.3 KB
RevLine 
[2851]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2851]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[2916]23using System.Collections.Generic;
[5419]24using System.Linq;
[2851]25using System.Windows.Forms;
26using HeuristicLab.Common;
[3262]27using HeuristicLab.Core;
[2851]28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
[3758]30using HeuristicLab.PluginInfrastructure;
[2851]31
[2852]32namespace HeuristicLab.Optimization.Views {
[2917]33  [View("Algorithm View")]
[2851]34  [Content(typeof(Algorithm), true)]
35  [Content(typeof(IAlgorithm), false)]
[6425]36  public partial class AlgorithmView : IOptimizerView {
[2851]37    private TypeSelectorDialog problemTypeSelectorDialog;
[6425]38    public AlgorithmView() {
39      InitializeComponent();
40    }
[2851]41
42    public new IAlgorithm Content {
43      get { return (IAlgorithm)base.Content; }
44      set { base.Content = value; }
45    }
46
[5237]47    protected override void Dispose(bool disposing) {
48      if (disposing) {
49        if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
50        if (components != null) components.Dispose();
51      }
52      base.Dispose(disposing);
53    }
54
[2916]55    protected override void OnInitialized(EventArgs e) {
56      // Set order of tab pages according to z order.
57      // NOTE: This is required due to a bug in the VS designer.
58      List<Control> tabPages = new List<Control>();
59      for (int i = 0; i < tabControl.Controls.Count; i++) {
60        tabPages.Add(tabControl.Controls[i]);
61      }
62      tabControl.Controls.Clear();
63      foreach (Control control in tabPages)
64        tabControl.Controls.Add(control);
65
66      base.OnInitialized(e);
67    }
68
[2851]69    protected override void DeregisterContentEvents() {
70      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
[4102]71      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
[2851]72      base.DeregisterContentEvents();
73    }
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
[4102]77      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
[2851]78    }
79
80    protected override void OnContentChanged() {
81      base.OnContentChanged();
82      if (Content == null) {
83        parameterCollectionView.Content = null;
84        problemViewHost.Content = null;
[2882]85        resultsView.Content = null;
[3275]86        runsView.Content = null;
[4102]87        storeAlgorithmInEachRunCheckBox.Checked = true;
[2851]88      } else {
89        parameterCollectionView.Content = Content.Parameters;
[2949]90        problemViewHost.ViewType = null;
[2851]91        problemViewHost.Content = Content.Problem;
[3226]92        resultsView.Content = Content.Results.AsReadOnly();
[3275]93        runsView.Content = Content.Runs;
[4102]94        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
[2851]95      }
96    }
97
[3904]98    protected override void SetEnabledStateOfControls() {
99      base.SetEnabledStateOfControls();
[3454]100      parameterCollectionView.Enabled = Content != null;
101      newProblemButton.Enabled = Content != null && !ReadOnly;
102      openProblemButton.Enabled = Content != null && !ReadOnly;
103      problemViewHost.Enabled = Content != null;
104      resultsView.Enabled = Content != null;
105      runsView.Enabled = Content != null;
[4102]106      storeAlgorithmInEachRunCheckBox.Enabled = Content != null && !ReadOnly;
[3367]107    }
108
[2955]109    protected override void OnClosed(FormClosedEventArgs e) {
[5419]110      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
111        //The content must be stopped if no other view showing the content is available
112        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
[5420]113        if (!optimizers.Contains(Content)) {
114          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
115          if (!nestedOptimizers.Contains(Content)) Content.Stop();
116        }
[5419]117      }
[2955]118      base.OnClosed(e);
119    }
120
[2851]121    #region Content Events
[6425]122    protected override void Content_Prepared(object sender, EventArgs e) {
123      if (InvokeRequired)
124        Invoke(new EventHandler(Content_Prepared), sender, e);
125      else {
126        base.Content_Prepared(sender, e);
127        resultsView.Content = Content.Results.AsReadOnly();
128      }
129    }
130
[2998]131    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
[2851]132      if (InvokeRequired)
133        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
134      else {
[2949]135        problemViewHost.ViewType = null;
[2851]136        problemViewHost.Content = Content.Problem;
137      }
138    }
[4102]139    protected virtual void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
140      if (InvokeRequired)
141        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
142      else
143        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
144    }
[2851]145    #endregion
146
[3299]147    #region Control Events
[2998]148    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
[2851]149      if (problemTypeSelectorDialog == null) {
150        problemTypeSelectorDialog = new TypeSelectorDialog();
151        problemTypeSelectorDialog.Caption = "Select Problem";
[3407]152        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
[3588]153        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
[2851]154      }
155      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]156        try {
157          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[13338]158        } catch (Exception ex) {
159          MainFormManager.MainForm.ShowError(ex.Message, ex);
[3407]160        }
[2851]161      }
162    }
[2998]163    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
[2851]164      openFileDialog.Title = "Open Problem";
165      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[3454]166        newProblemButton.Enabled = openProblemButton.Enabled = false;
[3177]167        problemViewHost.Enabled = false;
[2954]168
[3500]169        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
[2954]170          try {
[3500]171            if (error != null) throw error;
172            IProblem problem = content as IProblem;
[2954]173            if (problem == null)
[4068]174              Invoke(new Action(() =>
[3548]175                MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
[2954]176            else if (!Content.ProblemType.IsInstanceOfType(problem))
[3548]177              Invoke(new Action(() =>
178                MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
[2954]179            else
180              Content.Problem = problem;
[13338]181          } catch (Exception ex) {
182            Invoke(new Action(() => MainFormManager.MainForm.ShowError(ex.Message, ex)));
183          } finally {
[3500]184            Invoke(new Action(delegate() {
185              problemViewHost.Enabled = true;
186              newProblemButton.Enabled = openProblemButton.Enabled = true;
187            }));
188          }
189        });
[2851]190      }
191    }
[4102]192    protected virtual void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
193      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
194    }
[4522]195    protected virtual void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
[3299]196      e.Effect = DragDropEffects.None;
[5837]197      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null) && Content.ProblemType.IsAssignableFrom(e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat).GetType())) {
[3694]198        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3299]199        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]200        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
201        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
202        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[3299]203      }
204    }
[4522]205    protected virtual void problemTabPage_DragDrop(object sender, DragEventArgs e) {
[3299]206      if (e.Effect != DragDropEffects.None) {
[5837]207        IProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IProblem;
[5744]208        if (e.Effect.HasFlag(DragDropEffects.Copy)) problem = (IProblem)problem.Clone();
[3299]209        Content.Problem = problem;
210      }
211    }
[2851]212    #endregion
213  }
214}
Note: See TracBrowser for help on using the repository browser.