#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.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Common;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.Xml;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Clients.OKB {
[View("AlgorithmData View")]
[Content(typeof(AlgorithmData), true)]
public partial class AlgorithmDataView : AsynchronousContentView {
private List dataTypeComboBoxValues;
private long algorithmId;
public long AlgorithmId {
get { return algorithmId; }
set {
algorithmId = value;
if (Content != null) {
Content.AlgorithmId = value;
SetEnabledStateOfControls();
}
}
}
public new AlgorithmData Content {
get { return (AlgorithmData)base.Content; }
set { base.Content = value; }
}
public AlgorithmDataView() {
InitializeComponent();
}
protected override void OnInitialized(System.EventArgs e) {
base.OnInitialized(e);
dataTypeComboBoxValues = OKBClient.Instance.DataTypes.ToList();
dataTypeComboBox.DataSource = dataTypeComboBoxValues;
dataTypeComboBox.SelectedIndex = -1;
}
protected override void DeregisterContentEvents() {
Content.PropertyChanged -= new PropertyChangedEventHandler(Content_PropertyChanged);
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.PropertyChanged += new PropertyChangedEventHandler(Content_PropertyChanged);
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
dataTypeComboBox.SelectedIndex = -1;
viewHost.Content = null;
noViewAvailableLabel.Visible = false;
} else {
dataTypeComboBox.SelectedItem = dataTypeComboBoxValues.FirstOrDefault(d => d.Id == Content.DataTypeId);
using (MemoryStream stream = new MemoryStream(Content.Data)) {
try {
viewHost.Content = XmlParser.Deserialize(stream);
noViewAvailableLabel.Visible = false;
}
catch (Exception) {
viewHost.Content = null;
noViewAvailableLabel.Visible = true;
}
stream.Close();
}
}
fileTextBox.Text = "-";
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
newDataButton.Enabled = !ReadOnly;
openFileButton.Enabled = !ReadOnly;
saveFileButton.Enabled = (Content != null) && (((Content.Data != null) && (Content.Data.Length != 0)) || (viewHost.Content != null));
storeDataButton.Enabled = saveFileButton.Enabled && (Content.AlgorithmId != 0) && !ReadOnly;
dataTypeComboBox.Enabled = Content != null && viewHost.Content == null && !ReadOnly;
fileTextBox.Enabled = Content != null;
groupBox.Enabled = Content != null;
}
private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
switch (e.PropertyName) {
case "DataTypeId":
dataTypeComboBox.SelectedItem = dataTypeComboBoxValues.FirstOrDefault(d => d.Id == Content.DataTypeId);
break;
}
}
private void refreshDataButton_Click(object sender, EventArgs e) {
BeginAsyncCall();
var call = new Action(delegate() {
AlgorithmData data = OKBClient.Instance.GetAlgorithmData(AlgorithmId);
Content = data;
});
call.BeginInvoke(delegate(IAsyncResult result) {
call.EndInvoke(result);
EndAsyncCall();
}, null);
}
private void storeDataButton_Click(object sender, EventArgs e) {
if (viewHost.Content != null) {
try {
using (MemoryStream stream = new MemoryStream()) {
IAlgorithm algorithm = viewHost.Content as IAlgorithm;
algorithm.Prepare(true);
XmlGenerator.Serialize(algorithm, stream);
stream.Close();
Content.Data = stream.ToArray();
}
}
catch (Exception ex) {
ErrorHandling.ShowErrorDialog(this, ex);
}
}
OKBClient.Instance.UpdateAlgorithmData(Content);
}
private void newDataButton_Click(object sender, EventArgs e) {
using (TypeSelectorDialog dialog = new TypeSelectorDialog()) {
dialog.Caption = "Select Algorithm";
dialog.TypeSelector.Caption = "Available Algorithms";
dialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
if (dialog.ShowDialog(this) == DialogResult.OK) {
try {
if (Content == null) Content = new AlgorithmData { AlgorithmId = AlgorithmId };
viewHost.Content = (IContent)dialog.TypeSelector.CreateInstanceOfSelectedType();
DataType dataType = OKBClient.Instance.ConvertToDataType(viewHost.Content.GetType());
dataTypeComboBoxValues = OKBClient.Instance.DataTypes.OrderBy(d => d.Name).ToList();
dataTypeComboBox.DataSource = dataTypeComboBoxValues;
dataTypeComboBox.SelectedItem = dataType;
fileTextBox.Text = "-";
noViewAvailableLabel.Visible = false;
SetEnabledStateOfControls();
}
catch (Exception ex) {
ErrorHandling.ShowErrorDialog(this, ex);
}
}
}
}
private void openFileButton_Click(object sender, EventArgs e) {
if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
try {
if (Content == null) Content = new AlgorithmData { AlgorithmId = AlgorithmId };
IAlgorithm algorithm = null;
try {
algorithm = XmlParser.Deserialize(openFileDialog.FileName);
}
catch (Exception) { }
if (algorithm != null) {
viewHost.Content = algorithm;
noViewAvailableLabel.Visible = false;
DataType dataType = OKBClient.Instance.ConvertToDataType(viewHost.Content.GetType());
dataTypeComboBoxValues = OKBClient.Instance.DataTypes.OrderBy(d => d.Name).ToList();
dataTypeComboBox.DataSource = dataTypeComboBoxValues;
dataTypeComboBox.SelectedItem = dataType;
} else {
viewHost.Content = null;
noViewAvailableLabel.Visible = true;
using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Close();
Content.Data = bytes;
}
}
fileTextBox.Text = openFileDialog.FileName;
SetEnabledStateOfControls();
}
catch (Exception ex) {
ErrorHandling.ShowErrorDialog(this, ex);
}
}
}
private void saveFileButton_Click(object sender, EventArgs e) {
if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
try {
if (viewHost.Content != null) {
XmlGenerator.Serialize(viewHost.Content, saveFileDialog.FileName);
} else {
using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
stream.Write(Content.Data, 0, Content.Data.Length);
stream.Close();
}
}
fileTextBox.Text = saveFileDialog.FileName;
}
catch (Exception ex) {
ErrorHandling.ShowErrorDialog(this, ex);
}
}
}
private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
if (Content != null) Content.DataTypeId = ((DataType)dataTypeComboBox.SelectedItem).Id;
}
private void BeginAsyncCall() {
if (InvokeRequired)
Invoke(new Action(BeginAsyncCall));
else {
Cursor = Cursors.AppStarting;
Enabled = false;
}
}
private void EndAsyncCall() {
if (InvokeRequired)
Invoke(new Action(EndAsyncCall));
else {
Cursor = Cursors.Default;
Enabled = true;
}
}
}
}