#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Core;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
using HeuristicLab.Optimization.Views;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.DatastreamAnalysis {
[View("DatastreamAnalysisOptimizer View", "")]
[Content(typeof(DatastreamAnalysisOptimizer), true)]
public partial class DatastreamAnalysisOptimizerView : IOptimizerView {
public DatastreamAnalysisOptimizerView() {
InitializeComponent();
Content = new DatastreamAnalysisOptimizer();
}
public new DatastreamAnalysisOptimizer Content {
get { return (DatastreamAnalysisOptimizer) base.Content; }
set { base.Content = value; }
}
//protected override void Dispose(bool disposing) {
// if (disposing) {
// if (components != null) components.Dispose();
// }
// base.Dispose(disposing);
//}
protected override void OnInitialized(EventArgs e) {
// set order of tab pages according to z order.
// NOTE: this is required due to a bug in the VS designer.
List tabPages = new List();
for (int i = 0; i < tabControl.Controls.Count; i++) {
tabPages.Add(tabControl.Controls[i]);
}
tabControl.Controls.Clear();
foreach(Control control in tabPages)
tabControl.Controls.Add(control);
base.OnInitialized(e);
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
ensemblesViewHost.Content = null;
datastreamViewHost.Content = null;
resultsView.Content = null;
runsView.Content = null;
} else {
ensemblesViewHost.ViewType = null;
ensemblesViewHost.Content = Content.Ensembles;
datastreamViewHost.ViewType = null;
datastreamViewHost.Content = Content.Datastream;
resultsView.Content = Content.Results.AsReadOnly();
runsView.Content = Content.Runs;
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
resultsView.Enabled = Content != null;
runsView.Enabled = Content != null;
}
protected override void OnClosed(FormClosedEventArgs e) {
if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
//The content must be stopped if no other view showing the content is available
var optimizers =
MainFormManager.MainForm.Views.OfType()
.Where(v => v != this)
.Select(v => v.Content)
.OfType();
if (!optimizers.Contains(Content)) {
var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
if (!nestedOptimizers.Contains(Content)) Content.Stop();
}
}
base.OnClosed(e);
}
//protected override void startButton_Click(object sender, EventArgs e) {
// base.startButton_Click(sender, e);
//}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.EnsemblesChanged += new EventHandler(Content_EnsemblesChanged);
Content.DatastreamChanged += new EventHandler(Content_DatastreamChanged);
}
protected override void DeregisterContentEvents() {
Content.EnsemblesChanged -= new EventHandler(Content_EnsemblesChanged);
Content.DatastreamChanged -= new EventHandler(Content_DatastreamChanged);
base.DeregisterContentEvents();
}
#region content events
protected override void Content_Prepared(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_Prepared), sender, e);
else {
base.Content_Prepared(sender,e);
resultsView.Content = Content.Results.AsReadOnly();
}
}
protected void Content_EnsemblesChanged(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_EnsemblesChanged), sender, e);
else {
ensemblesViewHost.ViewType = null;
ensemblesViewHost.Content = Content.Ensembles;
}
}
protected void Content_DatastreamChanged(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_DatastreamChanged), sender, e);
else {
if (datastreamViewHost.Content != null && Content.Datastream != null &&
datastreamViewHost.Content.GetType() != Content.Datastream.GetType())
datastreamViewHost.ViewType = null;
datastreamViewHost.Content = Content.Datastream;
}
}
#endregion
#region event handlers
private void ensemblesTab_DragDrop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) {
try {
var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
if (data is IProxyEnsembleModel) {
data = (IProxyEnsembleModel) data;
Content.Ensembles.Add((ProxyEnsembleModel) data);
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Set ensembles", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ensemblesTab_DragEnterOver(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.None;
if (ReadOnly || !e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat))
return;
var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
if (data is IProxyEnsembleModel) {
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.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
}
}
private void datastreamTab_DragDrop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) {
try {
var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
if (data is IValueParameter) data = ((IValueParameter) data).Value;
if (data is IRegressionProblemData)
Content.Datastream.ProblemData = (RegressionProblemData) data;
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Set datastream", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void datastreamTab_DragEnterOver(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.None;
if (ReadOnly || !e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat))
return;
var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
if (data is IValueParameter) data = ((IValueParameter) data).Value;
if (data is IRegressionProblemData) {
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.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
}
}
#endregion event handlers
}
}