Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 8.2 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.IO;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.Xml;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Clients.OKB {
35  [View("AlgorithmData View")]
36  [Content(typeof(AlgorithmData), true)]
37  public partial class AlgorithmDataView : AsynchronousContentView {
38    public long AlgorithmId { get; set; }
39
40    public new AlgorithmData Content {
41      get { return (AlgorithmData)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public AlgorithmDataView() {
46      InitializeComponent();
47    }
48
49    protected override void OnInitialized(System.EventArgs e) {
50      base.OnInitialized(e);
51      dataTypeComboBox.DataSource = Administrator.Instance.DataTypes.ToList();
52      dataTypeComboBox.SelectedIndex = -1;
53    }
54
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57      if (Content == null) {
58        dataTypeComboBox.SelectedIndex = -1;
59        viewHost.Content = null;
60        noViewAvailableLabel.Visible = false;
61      } else {
62        dataTypeComboBox.SelectedItem = Administrator.Instance.DataTypes.FirstOrDefault(d => d.Id == Content.DataTypeId);
63
64        using (MemoryStream stream = new MemoryStream(Content.Data)) {
65          try {
66            viewHost.Content = XmlParser.Deserialize<IContent>(stream);
67            noViewAvailableLabel.Visible = false;
68          }
69          catch (Exception) {
70            viewHost.Content = null;
71            noViewAvailableLabel.Visible = true;
72          }
73          stream.Close();
74        }
75      }
76      fileTextBox.Text = "-";
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
81      storeDataButton.Enabled = saveFileButton.Enabled = Content != null;
82      dataTypeComboBox.Enabled = Content != null && viewHost.Content == null;
83      fileTextBox.Enabled = Content != null;
84      groupBox.Enabled = Content != null;
85    }
86
87    private void refreshDataButton_Click(object sender, EventArgs e) {
88      AlgorithmData data = Administrator.Instance.GetAlgorithmData(AlgorithmId);
89      Content = data;
90    }
91    private void storeDataButton_Click(object sender, EventArgs e) {
92      if (viewHost.Content != null) {
93        try {
94          using (MemoryStream stream = new MemoryStream()) {
95            IAlgorithm algorithm = viewHost.Content as IAlgorithm;
96            algorithm.Prepare(true);
97            algorithm.Problem = null;
98            XmlGenerator.Serialize(algorithm, stream);
99            stream.Close();
100            Content.Data = stream.ToArray();
101          }
102        }
103        catch (Exception ex) {
104          ErrorHandling.ShowErrorDialog(this, ex);
105        }
106      }
107      Administrator.Instance.UpdateAlgorithmData(Content);
108    }
109    private void newDataButton_Click(object sender, EventArgs e) {
110      using (TypeSelectorDialog dialog = new TypeSelectorDialog()) {
111        dialog.Caption = "Select Algorithm";
112        dialog.TypeSelector.Caption = "Available Algorithms";
113        dialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
114        if (dialog.ShowDialog(this) == DialogResult.OK) {
115          try {
116            if (Content == null) Content = new AlgorithmData { AlgorithmId = AlgorithmId, Data = new byte[0] };
117            viewHost.Content = (IContent)dialog.TypeSelector.CreateInstanceOfSelectedType();
118            DataType dataType = Administrator.Instance.DataTypes.FirstOrDefault(d => d.Name == viewHost.Content.GetType().AssemblyQualifiedName);
119            if (dataType == null) {
120              dataType = new DataType { Name = viewHost.Content.GetType().AssemblyQualifiedName, SqlName = "Blob" };
121              dataType.PlatformId = Administrator.Instance.Platforms.FirstOrDefault(p => p.Name == "HeuristicLab 3.3").Id;
122
123              Administrator.Instance.Store(dataType);
124              Administrator.Instance.DataTypes.Add(dataType);
125              dataTypeComboBox.DataSource = Administrator.Instance.DataTypes.OrderBy(d => d.Name).ToList();
126            }
127            dataTypeComboBox.SelectedItem = dataType;
128            dataTypeComboBox.Enabled = false;
129            fileTextBox.Text = "-";
130            noViewAvailableLabel.Visible = false;
131          }
132          catch (Exception ex) {
133            ErrorHandling.ShowErrorDialog(this, ex);
134          }
135        }
136      }
137    }
138    private void openFileButton_Click(object sender, EventArgs e) {
139      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
140        try {
141          if (Content == null) Content = new AlgorithmData { AlgorithmId = AlgorithmId, Data = new byte[0] };
142          IAlgorithm algorithm = null;
143          try {
144            algorithm = XmlParser.Deserialize<IAlgorithm>(openFileDialog.FileName);
145          }
146          catch (Exception) { }
147
148          if (algorithm != null) {
149            viewHost.Content = algorithm;
150            noViewAvailableLabel.Visible = false;
151            DataType dataType = Administrator.Instance.DataTypes.FirstOrDefault(d => d.Name == viewHost.Content.GetType().AssemblyQualifiedName);
152            if (dataType == null) {
153              dataType = new DataType { Name = viewHost.Content.GetType().AssemblyQualifiedName, SqlName = "Blob" };
154              dataType.PlatformId = Administrator.Instance.Platforms.FirstOrDefault(p => p.Name == "HeuristicLab 3.3").Id;
155
156              Administrator.Instance.Store(dataType);
157              Administrator.Instance.DataTypes.Add(dataType);
158              dataTypeComboBox.DataSource = Administrator.Instance.DataTypes.OrderBy(d => d.Name).ToList();
159            }
160            dataTypeComboBox.SelectedItem = dataType;
161          } else {
162            viewHost.Content = null;
163            noViewAvailableLabel.Visible = true;
164            using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
165              byte[] bytes = new byte[stream.Length];
166              stream.Read(bytes, 0, bytes.Length);
167              stream.Close();
168              Content.Data = bytes;
169            }
170          }
171          dataTypeComboBox.Enabled = viewHost.Content == null;
172          fileTextBox.Text = openFileDialog.FileName;
173        }
174        catch (Exception ex) {
175          ErrorHandling.ShowErrorDialog(this, ex);
176        }
177      }
178    }
179    private void saveFileButton_Click(object sender, EventArgs e) {
180      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
181        try {
182          if (viewHost.Content != null) {
183            XmlGenerator.Serialize(viewHost.Content, saveFileDialog.FileName);
184          } else {
185            using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
186              stream.Write(Content.Data, 0, Content.Data.Length);
187              stream.Close();
188            }
189          }
190          fileTextBox.Text = saveFileDialog.FileName;
191        }
192        catch (Exception ex) {
193          ErrorHandling.ShowErrorDialog(this, ex);
194        }
195      }
196    }
197    private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
198      if (Content != null) Content.DataTypeId = ((DataType)dataTypeComboBox.SelectedItem).Id;
199    }
200  }
201}
Note: See TracBrowser for help on using the repository browser.