#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Clients.Access;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
namespace HeuristicLab.Clients.OKB.RunCreation {
[View("OKBExperimentUpload View")]
[Content(typeof(Experiment), false)]
public partial class OKBExperimentUploadView : ItemView {
public new Experiment Content {
get { return (Experiment)base.Content; }
set { base.Content = value; }
}
List runConfigViews = new List();
private List problems = new List();
private List algorithms = new List();
public OKBExperimentUploadView() {
InitializeComponent();
tableLayoutPanel.RowCount = 0;
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
ClearRuns();
} else {
DisplayRuns();
}
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
}
protected override void DeregisterContentEvents() {
RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
base.DeregisterContentEvents();
}
private void DisplayError(Exception ex) {
PluginInfrastructure.ErrorHandling.ShowErrorDialog("An error occured while retrieving algorithm and problem information from the OKB.", ex);
}
private void DisplayRuns() {
if (RunCreationClient.Instance.Algorithms == null || RunCreationClient.Instance.Algorithms.Count() == 0) {
RunCreationClient.Instance.RefreshAsync(DisplayError);
} else {
CreateUI();
}
}
private void CreateUI() {
problems.AddRange(RunCreationClient.Instance.Problems);
algorithms.AddRange(RunCreationClient.Instance.Algorithms);
foreach (var run in Content.Runs) {
OKBRunConfigSelectionView orcsv = new OKBRunConfigSelectionView(run, algorithms, problems);
runConfigViews.Add(orcsv);
tableLayoutPanel.Controls.Add(orcsv);
}
}
private void ClearRuns() {
foreach (var runConfigView in runConfigViews) {
runConfigView.Hide();
tableLayoutPanel.Controls.Remove(runConfigView);
runConfigView.Dispose();
}
runConfigViews.Clear();
}
private void RunCreationClient_Refreshing(object sender, EventArgs e) {
if (InvokeRequired) {
Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
} else {
//TODO: display loading dialog
Cursor = Cursors.AppStarting;
Enabled = false;
}
}
private void RunCreationClient_Refreshed(object sender, EventArgs e) {
if (InvokeRequired) {
Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
} else {
CreateUI();
Enabled = true;
SetEnabledStateOfControls();
Cursor = Cursors.Default;
}
}
private void btnUpload_Click(object sender, EventArgs e) {
foreach (var runConfigView in runConfigViews) {
if (runConfigView.UploadToOKB()) {
//TODO: show progress
OKBRun run = new OKBRun(runConfigView.GetSelectedAlgorithm().Id, runConfigView.GetSelectedProblem().Id, runConfigView.ExperimentRun, UserInformation.Instance.User.Id);
try { run.Store(); }
catch (Exception ex) {
PluginInfrastructure.ErrorHandling.ShowErrorDialog("An exception occured while uploading the runs to the OKB.", ex);
}
}
}
}
}
}