Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.Views/MultiTrajectory/MultiTrajectoryView.cs @ 9622

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

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

File size: 9.2 KB
Line 
1using System;
2using System.Windows.Forms;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Core.Views;
6using HeuristicLab.MainForm;
7using HeuristicLab.Optimization;
8using HeuristicLab.PluginInfrastructure;
9
10namespace HeuristicLab.Analysis.FitnessLandscape.MultiTrajectory {
11
12
13  [View("MultiTrajectoryView")]
14  [Content(typeof(MultiTrajectoryAnalysis), IsDefaultView = true)]
15  public sealed partial class MultiTrajectoryAnalysisView : NamedItemView {
16
17    private TypeSelectorDialog algorithmTypeSelectorDialog;
18
19    public new MultiTrajectoryAnalysis Content {
20      get { return (MultiTrajectoryAnalysis)base.Content; }
21      set { base.Content = value; }
22    }
23
24    public MultiTrajectoryAnalysisView() {
25      InitializeComponent();
26    }
27
28    protected override void DeregisterContentEvents() {
29      Content.AlgorithmChanged -= Content_AlgorithmChanged;
30      Content.SampleGenerationAlgorithmChanged -= Content_SampleGenerationAlgorithmChanged;
31      Content.ExecutionStateChanged -= Content_ExecutionStateChanged;
32      Content.ExecutionTimeChanged -= Content_ExecutionTimeChanged;
33      base.DeregisterContentEvents();
34    }
35
36    protected override void RegisterContentEvents() {
37      base.RegisterContentEvents();
38      Content.AlgorithmChanged += Content_AlgorithmChanged;
39      Content.SampleGenerationAlgorithmChanged += Content_SampleGenerationAlgorithmChanged;
40      Content.ExecutionStateChanged += Content_ExecutionStateChanged;
41      Content.ExecutionTimeChanged += Content_ExecutionTimeChanged;
42    }
43
44    #region Event Handlers (Content)
45    void Content_AlgorithmChanged(object sender, EventArgs e) {
46      if (InvokeRequired) {
47        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
48      } else {
49        algorithmViewHost.Content = Content.Algorithm;
50        SetEnabledStateOfControls();
51      }
52    }
53    void Content_SampleGenerationAlgorithmChanged(object sender, EventArgs e) {
54      if (InvokeRequired) {
55        Invoke(new EventHandler(Content_SampleGenerationAlgorithmChanged), sender, e);
56      } else {
57        sampleGenerationAlgorithmViewHost.Content = Content.SampleGenerationAlgorithm;
58        SetEnabledStateOfControls();
59      }
60    }
61    void Content_ExecutionStateChanged(object sender, EventArgs e) {
62      if (InvokeRequired) {
63        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
64      } else {
65        SetEnabledStateOfControls();
66      }
67    }
68    void Content_ExecutionTimeChanged(object sender, EventArgs e) {
69      if (InvokeRequired) {
70        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
71      } else {
72        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
73      }
74    }
75    #endregion
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        parametersView.Content = null;
81        sampleGenerationAlgorithmViewHost.Content = null;
82        algorithmViewHost.Content = null;
83        runCollectionView.Content = null;
84        resultCollectionView.Content = null;
85        executionTimeTextBox.Text = "--:--";
86      } else {
87        parametersView.Content = Content.Parameters;
88        sampleGenerationAlgorithmViewHost.Content = Content.SampleGenerationAlgorithm;
89        algorithmViewHost.Content = Content.Algorithm;
90        runCollectionView.Content = Content.Runs;
91        resultCollectionView.Content = Content.Results.AsReadOnly();
92        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
93      }
94    }
95
96    protected override void SetEnabledStateOfControls() {
97      base.SetEnabledStateOfControls();
98      parametersView.Enabled = Content != null && !ReadOnly;
99      startButton.Enabled = Content != null && !ReadOnly && Content.ExecutionState != ExecutionState.Started && Content.ExecutionState != ExecutionState.Stopped;
100      pauseButton.Enabled = Content != null && !ReadOnly && Content.ExecutionState == ExecutionState.Started;
101      stopButton.Enabled = Content != null && !ReadOnly && Content.ExecutionState != ExecutionState.Prepared && Content.ExecutionState != ExecutionState.Stopped;
102      prepareButton.Enabled = Content != null && !ReadOnly && Content.ExecutionState != ExecutionState.Paused && Content.ExecutionState != ExecutionState.Started;
103    }
104
105
106    #region Event Handlers (child controls)
107    private void newAlgorithmButton_Click(object sender, System.EventArgs e) {
108      try {
109        IAlgorithm algorithm = SelectAlgorithm();
110        if (algorithm != null)
111          Content.Algorithm = algorithm;
112      } catch (Exception ex) {
113        ErrorHandling.ShowErrorDialog(this, ex);
114      }
115    }
116    private void openAlgorithmButton_Click(object sender, System.EventArgs e) {
117      LoadAlgorithm(
118        () => { newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = algorithmViewHost.Enabled = false; },
119        (alg) => { Content.Algorithm = alg; },
120        () => { newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = algorithmViewHost.Enabled = true; });
121    }
122    private void newSampleGenerationAlgorithmButton_Click(object sender, System.EventArgs e) {
123      try {
124        IAlgorithm algorithm = SelectAlgorithm();
125        if (algorithm != null)
126          Content.SampleGenerationAlgorithm = algorithm;
127      } catch (Exception ex) {
128        ErrorHandling.ShowErrorDialog(this, ex);
129      }
130    }
131    private void openSampleGenerationAlgorithmButton_Click(object sender, System.EventArgs e) {
132      LoadAlgorithm(
133        () => { newSampleGenerationAlgorithmButton.Enabled = openSampleGenerationAlgorithmButton.Enabled = sampleGenerationAlgorithmViewHost.Enabled = false; },
134        (alg) => { Content.SampleGenerationAlgorithm = alg; },
135        () => { newSampleGenerationAlgorithmButton.Enabled = openSampleGenerationAlgorithmButton.Enabled = sampleGenerationAlgorithmViewHost.Enabled = true; });
136    }
137    private void startButton_Click(object sender, System.EventArgs e) {
138      Content.Start();
139    }
140    private void pauseButton_Click(object sender, System.EventArgs e) {
141      Content.Pause();
142    }
143    private void stopButton_Click(object sender, System.EventArgs e) {
144      Content.Stop();
145    }
146    private void prepareButton_Click(object sender, System.EventArgs e) {
147      Content.Prepare();
148    }
149    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
150      e.Effect = DragDropEffects.None;     
151      if (!ReadOnly && e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IAlgorithm) {
152        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
153        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
154        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
155        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
156        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
157      }
158    }
159    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
160      if (e.Effect != DragDropEffects.None) {
161        IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
162        if (e.Effect.HasFlag(DragDropEffects.Copy)) algorithm = (IAlgorithm)algorithm.Clone();
163        if (sender == algorithmTabPage)
164          Content.Algorithm = algorithm;
165        if (sender == sampleGenerationTabPage)
166          Content.SampleGenerationAlgorithm = algorithm;
167      }
168    }
169    #endregion
170
171    #region Auxiliary Functions
172    private IAlgorithm SelectAlgorithm() {
173      if (algorithmTypeSelectorDialog == null) {
174        algorithmTypeSelectorDialog = new TypeSelectorDialog();
175        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
176        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
177        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, false);
178      }
179      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
180        return (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
181      }
182      return null;
183    }
184    private void LoadAlgorithm(Action pre, Action<IAlgorithm> set, Action post) {
185      openFileDialog.Title = "Open Algorithm";
186      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
187        if (InvokeRequired) {
188          Invoke(pre);
189        } else {
190          pre();
191        }
192        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
193          try {
194            if (error != null) throw error;
195            IAlgorithm algorithm = content as IAlgorithm;
196            if (algorithm == null)
197              Invoke(new Action(() =>
198                MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
199            else
200              set(algorithm);
201          } catch (Exception ex) {
202            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
203          } finally {
204            Invoke(post);
205          }
206        });
207      }
208    }
209    #endregion
210  }
211}
Note: See TracBrowser for help on using the repository browser.