Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.OKB.Views/3.3/Administration/Views/AlgorithmView.cs @ 12062

Last change on this file since 12062 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 12.7 KB
RevLine 
[8055]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8055]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.IO;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.Optimization;
32using HeuristicLab.Persistence.Default.Xml;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Clients.OKB.Administration {
36  [View("Algorithm View")]
37  [Content(typeof(Algorithm), true)]
38  public partial class AlgorithmView : NamedOKBItemView {
39    private List<Platform> platformComboBoxValues;
40    private List<AlgorithmClass> algorithmClassComboBoxValues;
41    private TypeSelectorDialog typeSelectorDialog;
42    private byte[] data;
43
44    public new Algorithm Content {
45      get { return (Algorithm)base.Content; }
46      set { base.Content = value; }
47    }
48
49    public AlgorithmView() {
50      InitializeComponent();
51    }
52
53    protected override void Dispose(bool disposing) {
54      if (disposing) {
55        if (components != null) components.Dispose();
56        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
57      }
58      base.Dispose(disposing);
59    }
60
61    protected override void OnContentChanged() {
62      base.OnContentChanged();
63
64      platformComboBox.SelectedValueChanged -= new EventHandler(platformComboBox_SelectedValueChanged);
65      if (AdministrationClient.Instance.Platforms != null)
66        platformComboBoxValues = AdministrationClient.Instance.Platforms.ToList();
67      platformComboBox.DataSource = platformComboBoxValues;
68      platformComboBox.SelectedValueChanged += new EventHandler(platformComboBox_SelectedValueChanged);
69
70      algorithmClassComboBox.SelectedValueChanged -= new EventHandler(algorithmClassComboBox_SelectedValueChanged);
71      if (AdministrationClient.Instance.AlgorithmClasses != null)
72        algorithmClassComboBoxValues = AdministrationClient.Instance.AlgorithmClasses.ToList();
73      algorithmClassComboBox.DataSource = algorithmClassComboBoxValues;
74      algorithmClassComboBox.SelectedValueChanged += new EventHandler(algorithmClassComboBox_SelectedValueChanged);
75
76      data = null;
77      dataViewHost.Content = null;
78      if (Content == null) {
79        platformComboBox.SelectedIndex = -1;
80        algorithmClassComboBox.SelectedIndex = -1;
81        dataTypeNameTextBox.Text = string.Empty;
82        dataTypeTypeNameTextBox.Text = string.Empty;
83        refreshableLightweightUserView.Content = null;
84        refreshableLightweightUserView.FetchSelectedUsers = null;
85      } else {
86        platformComboBox.SelectedItem = platformComboBoxValues.FirstOrDefault(p => p.Id == Content.PlatformId);
87        algorithmClassComboBox.SelectedItem = algorithmClassComboBoxValues.FirstOrDefault(a => a.Id == Content.AlgorithmClassId);
88        dataTypeNameTextBox.Text = Content.DataTypeName;
89        dataTypeTypeNameTextBox.Text = Content.DataTypeTypeName;
90        refreshableLightweightUserView.Content = Access.AccessClient.Instance;
91        refreshableLightweightUserView.FetchSelectedUsers = new Func<List<Guid>>(delegate { return AdministrationClient.GetAlgorithmUsers(Content.Id); });
92      }
93    }
94
95    protected override void SetEnabledStateOfControls() {
96      base.SetEnabledStateOfControls();
97      platformComboBox.Enabled = (Content != null) && !ReadOnly;
98      algorithmClassComboBox.Enabled = (Content != null) && !ReadOnly;
99      dataTypeGroupBox.Enabled = (Content != null) && !ReadOnly;
100      storeUsersButton.Enabled = (refreshableLightweightUserView.GetCheckedUsers() != null) && !ReadOnly;
101      refreshableLightweightUserView.Enabled = (refreshableLightweightUserView.Content != null) && !ReadOnly;
102      refreshDataButton.Enabled = (Content != null) && (Content.Id != 0);
103      storeDataButton.Enabled = ((data != null) || (dataViewHost.Content != null)) && !ReadOnly;
104      openFileButton.Enabled = (Content != null) && (Content.Id != 0);
105      saveFileButton.Enabled = (data != null) || (dataViewHost.Content != null);
106      noViewAvailableLabel.Visible = dataViewHost.Content == null;
107
108      bool isHL33Platform = platformComboBox.Text == "HeuristicLab 3.3";
109      dataTypeNameTextBox.ReadOnly = isHL33Platform;
110      dataTypeTypeNameTextBox.ReadOnly = isHL33Platform;
111      newDataButton.Enabled = isHL33Platform && (Content.Id != 0) && !ReadOnly;
112    }
113
114    #region Content Events
115    protected override void OnContentPropertyChanged(string propertyName) {
116      switch (propertyName) {
117        case "Id":
118          SetEnabledStateOfControls();
119          break;
120        case "PlatformId":
121          platformComboBox.SelectedItem = platformComboBoxValues.FirstOrDefault(p => p.Id == Content.PlatformId);
122          SetEnabledStateOfControls();
123          break;
124        case "AlgorithmClassId":
125          algorithmClassComboBox.SelectedItem = algorithmClassComboBoxValues.FirstOrDefault(a => a.Id == Content.AlgorithmClassId);
126          break;
127        case "DataTypeName":
128          dataTypeNameTextBox.Text = Content.DataTypeName;
129          break;
130        case "DataTypeTypeName":
131          dataTypeTypeNameTextBox.Text = Content.DataTypeTypeName;
132          break;
133      }
134    }
135    #endregion
136
137    #region Control Events
138    private void platformComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
139      if (Content != null) {
140        Platform selected = platformComboBox.SelectedItem as Platform;
141        if (selected != null) Content.PlatformId = selected.Id;
142      }
143    }
144    private void algorithmClassComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
145      if (Content != null) {
146        AlgorithmClass selected = algorithmClassComboBox.SelectedItem as AlgorithmClass;
147        if (selected != null) Content.AlgorithmClassId = selected.Id;
148      }
149    }
150
151    private void dataTypeNameTextBox_TextChanged(object sender, EventArgs e) {
152      if (dataTypeNameTextBox.Text != Content.DataTypeName)
153        Content.DataTypeName = dataTypeNameTextBox.Text;
154    }
155    private void dataTypeTypeNameTextBox_TextChanged(object sender, EventArgs e) {
156      if (dataTypeTypeNameTextBox.Text != Content.DataTypeTypeName)
157        Content.DataTypeTypeName = dataTypeTypeNameTextBox.Text;
158    }
159
160    private void storeUsersButton_Click(object sender, System.EventArgs e) {
161      try {
162        AdministrationClient.UpdateAlgorithmUsers(Content.Id, refreshableLightweightUserView.GetCheckedUsers().Select(x => x.Id).ToList());
163        storeUsersButton.Enabled = false;
164      }
165      catch (Exception ex) {
166        ErrorHandling.ShowErrorDialog(this, "Store authorized users and groups failed.", ex);
167      }
168    }
169
170    private void refreshDataButton_Click(object sender, EventArgs e) {
171      CallAsync(
172        () => {
173          data = null;
174          dataViewHost.Content = null;
175          data = AdministrationClient.GetAlgorithmData(Content.Id);
176          if (data != null) {
177            using (MemoryStream stream = new MemoryStream(data)) {
178              try {
179                dataViewHost.Content = XmlParser.Deserialize<IContent>(stream);
180              }
181              catch (Exception) { }
182              stream.Close();
183            }
184          }
185        },
186        "Refresh algorithm data failed.",
187        () => SetEnabledStateOfControls()
188      );
189    }
190    private void storeDataButton_Click(object sender, EventArgs e) {
191      CallAsync(
192        () => {
193          if (dataViewHost.Content != null) {
194            using (MemoryStream stream = new MemoryStream()) {
195              IAlgorithm algorithm = dataViewHost.Content as IAlgorithm;
196              algorithm.Prepare(true);
197              XmlGenerator.Serialize(algorithm, stream);
198              stream.Close();
199              data = stream.ToArray();
200            }
201          }
202          AdministrationClient.UpdateAlgorithmData(Content.Id, data);
203        },
204        "Store algorithm data failed.",
205        null
206      );
207    }
208    private void newDataButton_Click(object sender, EventArgs e) {
209      if (typeSelectorDialog == null) {
210        typeSelectorDialog = new TypeSelectorDialog();
211        typeSelectorDialog.Caption = "Select Algorithm";
212        typeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
213        typeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
214      }
215      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
216        try {
217          Content.DataTypeName = typeSelectorDialog.TypeSelector.SelectedType.Name;
218          Content.DataTypeTypeName = typeSelectorDialog.TypeSelector.SelectedType.AssemblyQualifiedName;
219          data = null;
220          dataViewHost.Content = (IContent)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
221        }
222        catch (Exception ex) {
223          ErrorHandling.ShowErrorDialog(this, "Create new algorithm data failed.", ex);
224        }
225        SetEnabledStateOfControls();
226      }
227    }
228    private void openFileButton_Click(object sender, EventArgs e) {
229      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
230        CallAsync(
231          () => {
232            IContent algorithm = null;
233            try {
234              algorithm = XmlParser.Deserialize<IContent>(openFileDialog.FileName);
235            }
236            catch (Exception) { }
237
238            if (algorithm != null) {
239              Content.DataTypeName = algorithm.GetType().Name;
240              Content.DataTypeTypeName = algorithm.GetType().AssemblyQualifiedName;
241              data = null;
242            } else {
243              using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
244                data = new byte[stream.Length];
245                stream.Read(data, 0, data.Length);
246                stream.Close();
247              }
248            }
249            dataViewHost.Content = algorithm;
250          },
251          "Save algorithm data into file failed.",
252          () => SetEnabledStateOfControls()
253        );
254      }
255    }
256    private void saveFileButton_Click(object sender, EventArgs e) {
257      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
258        CallAsync(
259          () => {
260            if (dataViewHost.Content != null) {
261              XmlGenerator.Serialize(dataViewHost.Content, saveFileDialog.FileName);
262            } else {
263              using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
264                stream.Write(data, 0, data.Length);
265                stream.Close();
266              }
267            }
268          },
269          "Save algorithm data into file failed.",
270          null
271        );
272      }
273    }
274    #endregion
275
276    #region Helpers
277    private void CallAsync(Action call, string errorMessage, Action continueWith) {
278      BeginAsyncCall();
279      call.BeginInvoke(delegate(IAsyncResult result) {
280        Exception exception = null;
281        try {
282          call.EndInvoke(result);
283        }
284        catch (Exception ex) {
285          exception = ex;
286        }
287        EndAsyncCall(errorMessage, exception, continueWith);
288      }, null);
289    }
290    private void BeginAsyncCall() {
291      if (InvokeRequired)
292        Invoke(new Action(BeginAsyncCall));
293      else {
294        Cursor = Cursors.AppStarting;
295        Enabled = false;
296      }
297    }
298    private void EndAsyncCall(string errorMessage, Exception exception, Action continueWith) {
299      if (InvokeRequired)
300        Invoke(new Action<string, Exception, Action>(EndAsyncCall), errorMessage, exception, continueWith);
301      else {
302        Cursor = Cursors.Default;
303        Enabled = true;
304        if (exception != null) ErrorHandling.ShowErrorDialog(this, errorMessage, exception);
305        if (continueWith != null) continueWith();
306      }
307    }
308    #endregion
309
310    private void refreshableLightweightUserView_SelectedUsersChanged(object sender, EventArgs e) {
311      storeUsersButton.Enabled = !ReadOnly;
312    }
313  }
314}
Note: See TracBrowser for help on using the repository browser.