source: branches/HeuristicLab.DatastreamAnalysis/HeuristicLab.DatastreamAnalysis.Views/3.4/DatastreamAnalysisOptimizerView.cs @ 14536

Last change on this file since 14536 was 14536, checked in by jzenisek, 3 years ago

#2719 added datastream type; updated the optimizer view and control functionality

File size: 8.3 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Core;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Views;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.DatastreamAnalysis {
35  [View("DatastreamAnalysisOptimizer View", "")]
36  [Content(typeof(DatastreamAnalysisOptimizer), true)]
37  public partial class DatastreamAnalysisOptimizerView : IOptimizerView {
38    public DatastreamAnalysisOptimizerView() {
39      InitializeComponent();
40      Content = new DatastreamAnalysisOptimizer();
41    }
42
43    public new DatastreamAnalysisOptimizer Content {
44      get { return (DatastreamAnalysisOptimizer) base.Content; }
45      set { base.Content = value; }
46    }
47
48    //protected override void Dispose(bool disposing) {
49    //  if (disposing) {
50    //    if (components != null) components.Dispose();
51    //  }
52    //  base.Dispose(disposing);
53    //}
54
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
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        ensemblesViewHost.Content = null;
73        datastreamViewHost.Content = null;
74        resultsView.Content = null;
75        runsView.Content = null;
76      } else {
77        ensemblesViewHost.Content = Content.Ensembles;
78        datastreamViewHost.Content = Content.Datastream;
79        resultsView.Content = Content.Results.AsReadOnly();
80        runsView.Content = Content.Runs;
81      }
82    }
83
84    protected override void SetEnabledStateOfControls() {
85      base.SetEnabledStateOfControls();
86      resultsView.Enabled = Content != null;
87      runsView.Enabled = Content != null;
88    }
89
90    protected override void OnClosed(FormClosedEventArgs e) {
91      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
92        //The content must be stopped if no other view showing the content is available
93        var optimizers =
94          MainFormManager.MainForm.Views.OfType<IContentView>()
95            .Where(v => v != this)
96            .Select(v => v.Content)
97            .OfType<IOptimizer>();
98        if (!optimizers.Contains(Content)) {
99          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
100          if (!nestedOptimizers.Contains(Content)) Content.Stop();
101        }
102      }
103      base.OnClosed(e);
104    }
105
106    protected override void RegisterContentEvents() {
107      base.RegisterContentEvents();
108
109      Content.EnsemblesChanged += new EventHandler(Content_EnsemblesChanged);
110      Content.DatastreamChanged += new EventHandler(Content_DatastreamChanged);
111    }
112
113    protected override void DeregisterContentEvents() {
114      Content.EnsemblesChanged -= new EventHandler(Content_EnsemblesChanged);
115      Content.DatastreamChanged -= new EventHandler(Content_DatastreamChanged);
116
117      base.DeregisterContentEvents();
118    }
119
120    #region
121    protected override void Content_Prepared(object sender, EventArgs e) {
122      if (InvokeRequired)
123        Invoke(new EventHandler(Content_Prepared), sender, e);
124      else {
125        base.Content_Prepared(sender,e);
126        resultsView.Content = Content.Results.AsReadOnly();
127      }
128    }
129
130    protected void Content_EnsemblesChanged(object sender, EventArgs e) {
131      if (InvokeRequired)
132        Invoke(new EventHandler(Content_EnsemblesChanged), sender, e);
133      else {
134        ensemblesViewHost.Content = Content.Ensembles;
135      }
136    }
137
138    protected void Content_DatastreamChanged(object sender, EventArgs e) {
139      if (InvokeRequired)
140        Invoke(new EventHandler(Content_DatastreamChanged), sender, e);
141      else {
142        if (datastreamViewHost.Content != null && Content.Datastream != null &&
143            datastreamViewHost.Content.GetType() != Content.Datastream.GetType())
144          datastreamViewHost.ViewType = null;
145        datastreamViewHost.Content = Content.Datastream;
146      }
147    }
148    #endregion
149
150    #region event handlers
151    private void ensemblesTab_DragDrop(object sender, DragEventArgs e) {
152      if (e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) {
153        try {
154          var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
155          if (data is IRegressionEnsembleModel) {
156            data = (IRegressionEnsembleModel) data;
157            Content.Ensembles.Add((RegressionEnsembleModel) data);
158          }
159        }
160        catch (Exception ex) {
161          MessageBox.Show(ex.Message, "Set ensembles", MessageBoxButtons.OK, MessageBoxIcon.Error);
162        }
163      }
164    }
165
166    private void ensemblesTab_DragEnterOver(object sender, DragEventArgs e) {
167      e.Effect = DragDropEffects.None;
168      if (ReadOnly || !e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat))
169        return;
170
171      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
172
173      if (data is IRegressionEnsembleModel) {
174        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
175        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
176        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
177        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
178        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
179      }
180    }
181
182    private void datastreamTab_DragDrop(object sender, DragEventArgs e) {
183      if (e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) {
184        try {
185          var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
186          if (data is IValueParameter) data = ((IValueParameter) data).Value;
187          if (data is IRegressionProblemData)
188            Content.Datastream.ProblemData = (RegressionProblemData) data;
189        }
190        catch (Exception ex) {
191          MessageBox.Show(ex.Message, "Set datastream", MessageBoxButtons.OK, MessageBoxIcon.Error);
192        }
193      }
194    }
195
196    private void datastreamTab_DragEnterOver(object sender, DragEventArgs e) {
197      e.Effect = DragDropEffects.None;
198
199      if (ReadOnly || !e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat))
200        return;
201
202      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
203      if (data is IValueParameter) data = ((IValueParameter) data).Value;
204      if (data is IRegressionProblemData) {
205        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
206        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
207        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
208        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
209        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
210      }
211    }
212
213    #endregion event handlers
214  }
215}
Note: See TracBrowser for help on using the repository browser.