#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.MainForm;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Optimization.Views {
///
/// The base class for visual representations of items.
///
[View("EngineAlgorithm View")]
[Content(typeof(EngineAlgorithm), true)]
public partial class EngineAlgorithmView : AlgorithmView {
private List engineTypes;
public new EngineAlgorithm Content {
get { return (EngineAlgorithm)base.Content; }
set { base.Content = value; }
}
///
/// Initializes a new instance of .
///
public EngineAlgorithmView() {
InitializeComponent();
}
///
/// Intializes a new instance of with the given .
///
/// The item that should be displayed.
public EngineAlgorithmView(EngineAlgorithm content)
: this() {
Content = content;
}
protected override void DeregisterContentEvents() {
Content.EngineChanged -= new System.EventHandler(Content_EngineChanged);
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.EngineChanged += new System.EventHandler(Content_EngineChanged);
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (engineTypes == null) {
engineTypes = ApplicationManager.Manager.GetTypes(typeof(IEngine)).
OrderBy(x => {
string name = ItemAttribute.GetName(x);
if (name != null) return name;
else return x.GetPrettyName();
}).ToList();
foreach (Type t in engineTypes) {
string name = ItemAttribute.GetName(t);
if (name == null) name = t.GetPrettyName();
engineComboBox.Items.Add(name);
}
engineTypes.Insert(0, null);
engineComboBox.Items.Insert(0, "-");
}
if (Content == null) {
engineViewHost.Content = null;
createUserDefinedAlgorithmButton.Enabled = false;
} else {
if (Content.Engine == null)
engineComboBox.SelectedIndex = engineTypes.IndexOf(null);
else
engineComboBox.SelectedIndex = engineTypes.IndexOf(Content.Engine.GetType());
engineViewHost.ViewType = null;
engineViewHost.Content = Content.Engine;
createUserDefinedAlgorithmButton.Enabled = true;
}
}
protected override void Content_Started(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_Started), sender, e);
else {
createUserDefinedAlgorithmButton.Enabled = false;
engineComboBox.Enabled = false;
engineViewHost.Enabled = false;
base.Content_Started(sender, e);
}
}
protected override void Content_Stopped(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_Stopped), sender, e);
else {
createUserDefinedAlgorithmButton.Enabled = true;
engineComboBox.Enabled = true;
engineViewHost.Enabled = true;
base.Content_Stopped(sender, e);
}
}
protected virtual void Content_EngineChanged(object sender, System.EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_EngineChanged), sender, e);
else {
if (Content.Engine == null)
engineComboBox.SelectedIndex = engineTypes.IndexOf(null);
else
engineComboBox.SelectedIndex = engineTypes.IndexOf(Content.Engine.GetType());
engineViewHost.ViewType = null;
engineViewHost.Content = Content.Engine;
}
}
protected virtual void engineComboBox_SelectedIndexChanged(object sender, EventArgs e) {
if (Content != null) {
Type t = engineTypes[engineComboBox.SelectedIndex];
if (t == null)
Content.Engine = null;
else if ((Content.Engine == null) || (Content.Engine.GetType() != t))
Content.Engine = (IEngine)Activator.CreateInstance(t);
}
}
protected virtual void createUserDefinedAlgorithmButton_Click(object sender, EventArgs e) {
MainFormManager.CreateDefaultView(Content.CreateUserDefinedAlgorithm()).Show();
}
}
}