Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/Views/AlgorithmDataView.cs @ 4558

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

Worked on OKB (#1174)

File size: 10.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.IO;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32using HeuristicLab.Optimization;
33using HeuristicLab.Persistence.Default.Xml;
34using HeuristicLab.PluginInfrastructure;
35
36namespace HeuristicLab.Clients.OKB {
37  [View("AlgorithmData View")]
38  [Content(typeof(AlgorithmData), true)]
39  public partial class AlgorithmDataView : AsynchronousContentView {
40    private List<DataType> dataTypeComboBoxValues;
41
42    private long algorithmId;
43    public long AlgorithmId {
44      get { return algorithmId; }
45      set {
46        algorithmId = value;
47        if (Content != null) {
48          Content.AlgorithmId = value;
49          SetEnabledStateOfControls();
50        }
51      }
52    }
53
54    public new AlgorithmData Content {
55      get { return (AlgorithmData)base.Content; }
56      set { base.Content = value; }
57    }
58
59    public AlgorithmDataView() {
60      InitializeComponent();
61    }
62
63    protected override void OnInitialized(System.EventArgs e) {
64      base.OnInitialized(e);
65      dataTypeComboBoxValues = OKBClient.Instance.DataTypes.ToList();
66      dataTypeComboBox.DataSource = dataTypeComboBoxValues;
67      dataTypeComboBox.SelectedIndex = -1;
68    }
69
70    protected override void DeregisterContentEvents() {
71      Content.PropertyChanged -= new PropertyChangedEventHandler(Content_PropertyChanged);
72      base.DeregisterContentEvents();
73    }
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.PropertyChanged += new PropertyChangedEventHandler(Content_PropertyChanged);
77    }
78
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      if (Content == null) {
82        dataTypeComboBox.SelectedIndex = -1;
83        viewHost.Content = null;
84        noViewAvailableLabel.Visible = false;
85      } else {
86        dataTypeComboBox.SelectedItem = dataTypeComboBoxValues.FirstOrDefault(d => d.Id == Content.DataTypeId);
87
88        using (MemoryStream stream = new MemoryStream(Content.Data)) {
89          try {
90            viewHost.Content = XmlParser.Deserialize<IContent>(stream);
91            noViewAvailableLabel.Visible = false;
92          }
93          catch (Exception) {
94            viewHost.Content = null;
95            noViewAvailableLabel.Visible = true;
96          }
97          stream.Close();
98        }
99      }
100      fileTextBox.Text = "-";
101    }
102
103    protected override void SetEnabledStateOfControls() {
104      base.SetEnabledStateOfControls();
105      newDataButton.Enabled = !ReadOnly;
106      openFileButton.Enabled = !ReadOnly;
107      saveFileButton.Enabled = (Content != null) && (((Content.Data != null) && (Content.Data.Length != 0)) || (viewHost.Content != null));
108      storeDataButton.Enabled = saveFileButton.Enabled && (Content.AlgorithmId != 0) && !ReadOnly;
109      dataTypeComboBox.Enabled = Content != null && viewHost.Content == null && !ReadOnly;
110      fileTextBox.Enabled = Content != null;
111      groupBox.Enabled = Content != null;
112    }
113
114    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
115      switch (e.PropertyName) {
116        case "DataTypeId":
117          dataTypeComboBox.SelectedItem = dataTypeComboBoxValues.FirstOrDefault(d => d.Id == Content.DataTypeId);
118          break;
119      }
120    }
121
122    private void refreshDataButton_Click(object sender, EventArgs e) {
123      BeginAsyncCall();
124      var call = new Action(delegate() {
125        AlgorithmData data = OKBClient.Instance.GetAlgorithmData(AlgorithmId);
126        Content = data;
127      });
128      call.BeginInvoke(delegate(IAsyncResult result) {
129        call.EndInvoke(result);
130        EndAsyncCall();
131      }, null);
132    }
133    private void storeDataButton_Click(object sender, EventArgs e) {
134      if (viewHost.Content != null) {
135        try {
136          using (MemoryStream stream = new MemoryStream()) {
137            IAlgorithm algorithm = viewHost.Content as IAlgorithm;
138            algorithm.Prepare(true);
139            XmlGenerator.Serialize(algorithm, stream);
140            stream.Close();
141            Content.Data = stream.ToArray();
142          }
143        }
144        catch (Exception ex) {
145          ErrorHandling.ShowErrorDialog(this, ex);
146        }
147      }
148      OKBClient.Instance.UpdateAlgorithmData(Content);
149    }
150    private void newDataButton_Click(object sender, EventArgs e) {
151      using (TypeSelectorDialog dialog = new TypeSelectorDialog()) {
152        dialog.Caption = "Select Algorithm";
153        dialog.TypeSelector.Caption = "Available Algorithms";
154        dialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
155        if (dialog.ShowDialog(this) == DialogResult.OK) {
156          try {
157            if (Content == null) Content = new AlgorithmData { AlgorithmId = AlgorithmId };
158            viewHost.Content = (IContent)dialog.TypeSelector.CreateInstanceOfSelectedType();
159            DataType dataType = OKBClient.Instance.DataTypes.FirstOrDefault(d => d.Name == viewHost.Content.GetType().AssemblyQualifiedName);
160            if (dataType == null) {
161              dataType = new DataType { Name = viewHost.Content.GetType().AssemblyQualifiedName, SqlName = "varbinary" };
162              dataType.PlatformId = OKBClient.Instance.Platforms.FirstOrDefault(p => p.Name == "HeuristicLab 3.3").Id;
163              dataType.Store();
164              OKBClient.Instance.DataTypes.Add(dataType);
165              dataTypeComboBoxValues = OKBClient.Instance.DataTypes.OrderBy(d => d.Name).ToList();
166              dataTypeComboBox.DataSource = dataTypeComboBoxValues;
167            }
168            dataTypeComboBox.SelectedItem = dataType;
169            fileTextBox.Text = "-";
170            noViewAvailableLabel.Visible = false;
171            SetEnabledStateOfControls();
172          }
173          catch (Exception ex) {
174            ErrorHandling.ShowErrorDialog(this, ex);
175          }
176        }
177      }
178    }
179    private void openFileButton_Click(object sender, EventArgs e) {
180      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
181        try {
182          if (Content == null) Content = new AlgorithmData { AlgorithmId = AlgorithmId };
183          IAlgorithm algorithm = null;
184          try {
185            algorithm = XmlParser.Deserialize<IAlgorithm>(openFileDialog.FileName);
186          }
187          catch (Exception) { }
188
189          if (algorithm != null) {
190            viewHost.Content = algorithm;
191            noViewAvailableLabel.Visible = false;
192            DataType dataType = OKBClient.Instance.DataTypes.FirstOrDefault(d => d.Name == viewHost.Content.GetType().AssemblyQualifiedName);
193            if (dataType == null) {
194              dataType = new DataType { Name = viewHost.Content.GetType().AssemblyQualifiedName, SqlName = "varbinary" };
195              dataType.PlatformId = OKBClient.Instance.Platforms.FirstOrDefault(p => p.Name == "HeuristicLab 3.3").Id;
196              dataType.Store();
197              OKBClient.Instance.DataTypes.Add(dataType);
198              dataTypeComboBoxValues = OKBClient.Instance.DataTypes.OrderBy(d => d.Name).ToList();
199              dataTypeComboBox.DataSource = dataTypeComboBoxValues;
200            }
201            dataTypeComboBox.SelectedItem = dataType;
202          } else {
203            viewHost.Content = null;
204            noViewAvailableLabel.Visible = true;
205            using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
206              byte[] bytes = new byte[stream.Length];
207              stream.Read(bytes, 0, bytes.Length);
208              stream.Close();
209              Content.Data = bytes;
210            }
211          }
212          fileTextBox.Text = openFileDialog.FileName;
213          SetEnabledStateOfControls();
214        }
215        catch (Exception ex) {
216          ErrorHandling.ShowErrorDialog(this, ex);
217        }
218      }
219    }
220    private void saveFileButton_Click(object sender, EventArgs e) {
221      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
222        try {
223          if (viewHost.Content != null) {
224            XmlGenerator.Serialize(viewHost.Content, saveFileDialog.FileName);
225          } else {
226            using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
227              stream.Write(Content.Data, 0, Content.Data.Length);
228              stream.Close();
229            }
230          }
231          fileTextBox.Text = saveFileDialog.FileName;
232        }
233        catch (Exception ex) {
234          ErrorHandling.ShowErrorDialog(this, ex);
235        }
236      }
237    }
238    private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
239      if (Content != null) Content.DataTypeId = ((DataType)dataTypeComboBox.SelectedItem).Id;
240    }
241
242    private void BeginAsyncCall() {
243      if (InvokeRequired)
244        Invoke(new Action(BeginAsyncCall));
245      else {
246        Cursor = Cursors.AppStarting;
247        Enabled = false;
248      }
249    }
250    private void EndAsyncCall() {
251      if (InvokeRequired)
252        Invoke(new Action(EndAsyncCall));
253      else {
254        Cursor = Cursors.Default;
255        Enabled = true;
256      }
257    }
258  }
259}
Note: See TracBrowser for help on using the repository browser.