Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/Administration/Views/AlgorithmView.cs @ 5902

Last change on this file since 5902 was 5902, checked in by swagner, 13 years ago

Worked on OKB (#1174)

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