Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.Views/EngineAlgorithm/EngineAlgorithmOperatorView.cs @ 7403

Last change on this file since 7403 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 5.0 KB
Line 
1using System;
2using System.Windows.Forms;
3using HeuristicLab.Common;
4using HeuristicLab.Core.Views;
5using HeuristicLab.MainForm;
6using HeuristicLab.Operators.Views;
7using HeuristicLab.Optimization;
8using HeuristicLab.PluginInfrastructure;
9
10namespace HeuristicLab.Operators {
11
12  [View("EngineAlgorithmOperator View")]
13  [Content(typeof(EngineAlgorithmOperator), IsDefaultView = true)]
14  public sealed partial class EngineAlgorithmOperatorView : AlgorithmOperatorView {
15
16    private TypeSelectorDialog algorithmTypeSelectorDialog;
17
18    public new EngineAlgorithmOperator Content {
19      get { return (EngineAlgorithmOperator)base.Content; }
20      set { base.Content = value; }
21    }
22
23    public EngineAlgorithmOperatorView() {
24      InitializeComponent();
25    }
26
27    protected override void DeregisterContentEvents() {
28      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
29      base.DeregisterContentEvents();
30    }
31
32    protected override void RegisterContentEvents() {
33      base.RegisterContentEvents();
34      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
35    }
36
37    protected override void OnContentChanged() {
38      base.OnContentChanged();
39      if (Content == null) {
40        algorithmViewHost.Content = null;
41      } else {
42        algorithmViewHost.Content = Content.Algorithm;
43      }
44    }
45
46
47    protected override void SetEnabledStateOfControls() {
48      base.SetEnabledStateOfControls();
49    }
50
51    #region Event Handlers (Content)
52    void Content_AlgorithmChanged(object sender, EventArgs e) {
53      if (InvokeRequired) {
54        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
55      } else {
56        algorithmViewHost.Content = Content.Algorithm;
57      }
58    }
59    #endregion
60
61    #region Event Handlers (child controls)
62    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
63      if (e.Effect != DragDropEffects.None) {
64        EngineAlgorithm algorithm = e.Data.GetData("Value") as EngineAlgorithm;
65        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy)
66          algorithm = (EngineAlgorithm)algorithm.Clone();
67        Content.Algorithm = algorithm;
68      }
69    }
70
71    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
72      e.Effect = DragDropEffects.None;
73      Type type = e.Data.GetData("Type") as Type;
74      if ((type != null) && (typeof(EngineAlgorithm).IsAssignableFrom(type))) {
75        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
76        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
77        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
78        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
79        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
80      }
81    }
82
83    private void newAlgorithmButton_Click(object sender, System.EventArgs e) {
84      if (algorithmTypeSelectorDialog == null) {
85        algorithmTypeSelectorDialog = new TypeSelectorDialog();
86        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
87        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithm";
88        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(EngineAlgorithm), false, true);
89      }
90      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
91        try {
92          Content.Algorithm = (EngineAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
93        } catch (Exception ex) {
94          ErrorHandling.ShowErrorDialog(this, ex);
95        }
96      }
97    }
98
99    private void openAlgorithmButton_Click(object sender, System.EventArgs e) {
100      openFileDialog.Title = "Open Algorithm";
101      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
102        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
103        algorithmViewHost.Enabled = false;
104
105        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
106          try {
107            if (error != null) throw error;
108            EngineAlgorithm algorithm = content as EngineAlgorithm;
109            if (algorithm == null)
110              Invoke(new Action(() =>
111                MessageBox.Show(this, "The selected file does not contain an engine algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
112            else
113              Content.Algorithm = algorithm;
114          } catch (Exception ex) {
115            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
116          } finally {
117            Invoke(new Action(delegate() {
118              algorithmViewHost.Enabled = true;
119              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
120            }));
121          }
122        });
123      }
124    }
125    #endregion
126  }
127}
Note: See TracBrowser for help on using the repository browser.