Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.AlgorithmHost/ParameterizedNamedItemContainerView.cs @ 6036

Last change on this file since 6036 was 4311, checked in by swagner, 14 years ago

Integrated OKB clients for HL 3.3 (#1166)

File size: 5.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Linq;
5using System.Data;
6using System.Drawing;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.Core.Views;
10using HeuristicLab.OKB.Client;
11using HeuristicLab.Common;
12using HeuristicLab.Core;
13using HeuristicLab.PluginInfrastructure;
14using System.Threading;
15
16namespace HeuristicLab.OKB.AlgorithmHost {
17
18  [MainForm.View("ParameterizedNamedItemContainer View")]
19  [MainForm.Content(typeof(ParameterizedNamedItemContainer), true)] 
20  public partial class ParameterizedNamedItemContainerView : ItemView {
21
22    private Mapping mapping;
23
24    public new ParameterizedNamedItemContainer Content {
25      get { return (ParameterizedNamedItemContainer)base.Content; }
26      set { base.Content = value; }
27    }
28
29    public ParameterizedNamedItemContainerView() {
30      mapping = new Mapping();
31      InitializeComponent();
32      InitializeParameterList();
33    }
34
35    public void InitializeParameterList() {
36      backgroundWorker.RunWorkerAsync();
37    }
38
39    protected override void RegisterContentEvents() {
40      base.RegisterContentEvents();
41      Content.ItemChanged += new EventHandler(Content_ItemChanged);
42    }
43
44   
45    protected override void DeregisterContentEvents() {
46      Content.ItemChanged -= new EventHandler(Content_ItemChanged);
47      base.DeregisterContentEvents();
48    }
49   
50    protected override void OnContentChanged() {
51      base.OnContentChanged();
52      parameterMappingView.Content = null;
53      itemViewHost.Content = null;
54      itemViewHost.ViewType = null;
55      if (Content == null) {
56        mapping.Map = null;
57      } else {
58        itemViewHost.Content = Content.Item;
59        mapping.Map = Content.ParameterMapping;
60        parameterMappingView.Content = mapping;
61      }
62    }
63
64    void Content_ItemChanged(object sender, EventArgs e) {
65      OnContentChanged();
66    }
67
68    protected void openItemButton_Click(object sender, EventArgs e) {
69      openFileDialog.Title = "Open Item";
70      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
71        newItemButton.Enabled = openItemButton.Enabled = false;
72        itemViewHost.Enabled = false;
73
74        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
75          try {
76            if (error != null) throw error;
77            if (content == null) {
78              MessageBox.Show(this, "The selected file could not be loaded", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
79            } else {
80              IParameterizedNamedItem item = content as IParameterizedNamedItem;
81              if (item == null && item.GetType() != Content.ItemType) {
82                MessageBox.Show(this,
83                  string.Format("The selected file does not contain an object of type {0}", Content.ItemType.Name),
84                  "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
85              } else {
86                Content.Item = item;
87              }
88            }
89          } catch (Exception ex) {
90            ErrorHandling.ShowErrorDialog(this, ex);
91          } finally {
92            Invoke(new Action(delegate() {
93              itemViewHost.Enabled = true;
94              newItemButton.Enabled = openItemButton.Enabled = true;
95            }));
96          }
97        });
98      }
99    }
100
101    private TypeSelectorDialog itemTypeSelectorDialog;
102
103    protected void newAlgorithmButton_Click(object sender, EventArgs e) {
104      if (itemTypeSelectorDialog == null) {
105        itemTypeSelectorDialog = new TypeSelectorDialog();
106        itemTypeSelectorDialog.Caption = "Select Item";
107        itemTypeSelectorDialog.TypeSelector.Caption = "Available Items";
108        itemTypeSelectorDialog.TypeSelector.Configure(Content.ItemType, false, true);
109      }
110      if (itemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
111        try {
112          Content.Item = (IParameterizedNamedItem)itemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
113        } catch (Exception ex) {
114          ErrorHandling.ShowErrorDialog(this, ex);
115        }
116      }
117    }
118
119    protected DataTable backgroundLoadedParameters = null;
120
121    protected void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
122      if (InvokeRequired)
123        Invoke(new ProgressChangedEventHandler(backgroundWorker_ProgressChanged), sender, e);
124      else {
125        if (e.ProgressPercentage < 100) {
126          progressBar.Visible = true;
127        } else {
128          progressBar.Visible = false;
129        }
130      }
131    }
132
133    protected virtual void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
134      backgroundWorker.ReportProgress(0);
135      var client = ClientFactory.Create<OKBTables.TableServiceClient, OKBTables.ITableService>();
136      int count;
137      DataTable pars = client.PrepareDataTable(out count, "Parameter");
138      pars.BeginLoadData();
139      DataTable newRows;
140      do {
141        newRows = client.GetNextRows(100);
142        pars.Merge(newRows);
143      } while (newRows != null && newRows.Rows.Count > 0);
144      client.FinishFetchingRows();
145      client.Close();
146      pars.EndLoadData();
147      backgroundLoadedParameters = pars;
148      backgroundWorker.ReportProgress(100);
149    }
150 
151    protected virtual void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
152      parameterMappingView.Enabled = false;
153      if (backgroundLoadedParameters != null) {
154        mapping.Values.Clear();
155        mapping.Values.Add(null);
156        mapping.Values.AddRange(backgroundLoadedParameters.Rows.Cast<DataRow>().Select(r => (string)r["Name"]));
157        parameterMappingView.Enabled = true;
158      }
159    }
160
161    protected virtual void refreshButton_Click(object sender, EventArgs e) {
162      InitializeParameterList();
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.