using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using HeuristicLab.Core.Views; using HeuristicLab.OKB.Client; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.PluginInfrastructure; using System.Threading; namespace HeuristicLab.OKB.AlgorithmHost { [MainForm.View("ParameterizedNamedItemContainer View")] [MainForm.Content(typeof(ParameterizedNamedItemContainer), true)] public partial class ParameterizedNamedItemContainerView : ItemView { private Mapping mapping; public new ParameterizedNamedItemContainer Content { get { return (ParameterizedNamedItemContainer)base.Content; } set { base.Content = value; } } public ParameterizedNamedItemContainerView() { mapping = new Mapping(); InitializeComponent(); InitializeParameterList(); } public void InitializeParameterList() { backgroundWorker.RunWorkerAsync(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ItemChanged += new EventHandler(Content_ItemChanged); } protected override void DeregisterContentEvents() { Content.ItemChanged -= new EventHandler(Content_ItemChanged); base.DeregisterContentEvents(); } protected override void OnContentChanged() { base.OnContentChanged(); parameterMappingView.Content = null; itemViewHost.Content = null; itemViewHost.ViewType = null; if (Content == null) { mapping.Map = null; } else { itemViewHost.Content = Content.Item; mapping.Map = Content.ParameterMapping; parameterMappingView.Content = mapping; } } void Content_ItemChanged(object sender, EventArgs e) { OnContentChanged(); } protected void openItemButton_Click(object sender, EventArgs e) { openFileDialog.Title = "Open Item"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { newItemButton.Enabled = openItemButton.Enabled = false; itemViewHost.Enabled = false; ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) { try { if (error != null) throw error; if (content == null) { MessageBox.Show(this, "The selected file could not be loaded", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { IParameterizedNamedItem item = content as IParameterizedNamedItem; if (item == null && item.GetType() != Content.ItemType) { MessageBox.Show(this, string.Format("The selected file does not contain an object of type {0}", Content.ItemType.Name), "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { Content.Item = item; } } } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } finally { Invoke(new Action(delegate() { itemViewHost.Enabled = true; newItemButton.Enabled = openItemButton.Enabled = true; })); } }); } } private TypeSelectorDialog itemTypeSelectorDialog; protected void newAlgorithmButton_Click(object sender, EventArgs e) { if (itemTypeSelectorDialog == null) { itemTypeSelectorDialog = new TypeSelectorDialog(); itemTypeSelectorDialog.Caption = "Select Item"; itemTypeSelectorDialog.TypeSelector.Caption = "Available Items"; itemTypeSelectorDialog.TypeSelector.Configure(Content.ItemType, false, true); } if (itemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) { try { Content.Item = (IParameterizedNamedItem)itemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType(); } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } } } protected DataTable backgroundLoadedParameters = null; protected void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (InvokeRequired) Invoke(new ProgressChangedEventHandler(backgroundWorker_ProgressChanged), sender, e); else { if (e.ProgressPercentage < 100) { progressBar.Visible = true; } else { progressBar.Visible = false; } } } protected virtual void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { backgroundWorker.ReportProgress(0); var client = ClientFactory.Create(); int count; DataTable pars = client.PrepareDataTable(out count, "Parameter"); pars.BeginLoadData(); DataTable newRows; do { newRows = client.GetNextRows(100); pars.Merge(newRows); } while (newRows != null && newRows.Rows.Count > 0); client.FinishFetchingRows(); client.Close(); pars.EndLoadData(); backgroundLoadedParameters = pars; backgroundWorker.ReportProgress(100); } protected virtual void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { parameterMappingView.Enabled = false; if (backgroundLoadedParameters != null) { mapping.Values.Clear(); mapping.Values.Add(null); mapping.Values.AddRange(backgroundLoadedParameters.Rows.Cast().Select(r => (string)r["Name"])); parameterMappingView.Enabled = true; } } protected virtual void refreshButton_Click(object sender, EventArgs e) { InitializeParameterList(); } } }