#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.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("ProblemData View")]
[Content(typeof(ProblemData), true)]
public partial class ProblemDataView : AsynchronousContentView {
private long problemId;
public long ProblemId {
get { return problemId; }
set {
problemId = value;
if (Content != null) {
Content.ProblemId = value;
SetEnabledStateOfControls();
}
}
}
public new ProblemData Content {
get { return (ProblemData)base.Content; }
set { base.Content = value; }
}
public ProblemDataView() {
InitializeComponent();
}
protected override void OnInitialized(System.EventArgs e) {
base.OnInitialized(e);
dataTypeComboBox.DataSource = Administrator.Instance.DataTypes.ToList();
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 = Administrator.Instance.DataTypes.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.ProblemId != 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 = Administrator.Instance.DataTypes.FirstOrDefault(d => d.Id == Content.DataTypeId);
break;
}
}
private void refreshDataButton_Click(object sender, EventArgs e) {
BeginAsyncCall();
var call = new Action(delegate() {
ProblemData data = Administrator.Instance.GetProblemData(ProblemId);
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()) {
IProblem problem = viewHost.Content as IProblem;
XmlGenerator.Serialize(problem, stream);
stream.Close();
Content.Data = stream.ToArray();
}
}
catch (Exception ex) {
ErrorHandling.ShowErrorDialog(this, ex);
}
}
Administrator.Instance.UpdateProblemData(Content);
}
private void newDataButton_Click(object sender, EventArgs e) {
using (TypeSelectorDialog dialog = new TypeSelectorDialog()) {
dialog.Caption = "Select Problem";
dialog.TypeSelector.Caption = "Available Problems";
dialog.TypeSelector.Configure(typeof(IProblem), false, true);
if (dialog.ShowDialog(this) == DialogResult.OK) {
try {
if (Content == null) Content = new ProblemData { ProblemId = ProblemId };
viewHost.Content = (IContent)dialog.TypeSelector.CreateInstanceOfSelectedType();
DataType dataType = Administrator.Instance.DataTypes.FirstOrDefault(d => d.Name == viewHost.Content.GetType().AssemblyQualifiedName);
if (dataType == null) {
dataType = new DataType { Name = viewHost.Content.GetType().AssemblyQualifiedName, SqlName = "varbinary" };
dataType.PlatformId = Administrator.Instance.Platforms.FirstOrDefault(p => p.Name == "HeuristicLab 3.3").Id;
dataType.Store();
Administrator.Instance.DataTypes.Add(dataType);
dataTypeComboBox.DataSource = Administrator.Instance.DataTypes.OrderBy(d => d.Name).ToList();
}
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 ProblemData { ProblemId = ProblemId };
IProblem problem = null;
try {
problem = XmlParser.Deserialize(openFileDialog.FileName);
}
catch (Exception) { }
if (problem != null) {
viewHost.Content = problem;
noViewAvailableLabel.Visible = false;
DataType dataType = Administrator.Instance.DataTypes.FirstOrDefault(d => d.Name == viewHost.Content.GetType().AssemblyQualifiedName);
if (dataType == null) {
dataType = new DataType { Name = viewHost.Content.GetType().AssemblyQualifiedName, SqlName = "varbinary" };
dataType.PlatformId = Administrator.Instance.Platforms.FirstOrDefault(p => p.Name == "HeuristicLab 3.3").Id;
dataType.Store();
Administrator.Instance.DataTypes.Add(dataType);
dataTypeComboBox.DataSource = Administrator.Instance.DataTypes.OrderBy(d => d.Name).ToList();
}
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;
}
}
}
}