Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 12.9 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.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("Problem View")]
38  [Content(typeof(Problem), true)]
39  public partial class ProblemView : NamedOKBItemView {
40    private List<Platform> platformComboBoxValues;
41    private List<ProblemClass> problemClassComboBoxValues;
42    private TypeSelectorDialog typeSelectorDialog;
43    private byte[] data;
44
45    public new Problem Content {
46      get { return (Problem)base.Content; }
47      set { base.Content = value; }
48    }
49
50    public ProblemView() {
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      problemClassComboBox.SelectedValueChanged -= new EventHandler(problemClassComboBox_SelectedValueChanged);
71      problemClassComboBoxValues = AdministrationClient.Instance.ProblemClasses.ToList();
72      problemClassComboBox.DataSource = problemClassComboBoxValues;
73      problemClassComboBox.SelectedValueChanged += new EventHandler(problemClassComboBox_SelectedValueChanged);
74
75      usersListBox.DataSource = null;
76      data = null;
77      dataViewHost.Content = null;
78      if (Content == null) {
79        platformComboBox.SelectedIndex = -1;
80        problemClassComboBox.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        problemClassComboBox.SelectedItem = problemClassComboBoxValues.FirstOrDefault(a => a.Id == Content.ProblemClassId);
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      problemClassComboBox.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 "ProblemClassId":
122          problemClassComboBox.SelectedItem = problemClassComboBoxValues.FirstOrDefault(a => a.Id == Content.ProblemClassId);
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 problemClassComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
142      if (Content != null) {
143        ProblemClass selected = problemClassComboBox.SelectedItem as ProblemClass;
144        if (selected != null) Content.ProblemClassId = 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.GetProblemUsers(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.UpdateProblemUsers(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.GetProblemData(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 problem 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              XmlGenerator.Serialize(dataViewHost.Content, stream);
214              stream.Close();
215              data = stream.ToArray();
216            }
217          }
218          AdministrationClient.UpdateProblemData(Content.Id, data);
219        },
220        "Store problem data failed.",
221        null
222      );
223    }
224    private void newDataButton_Click(object sender, EventArgs e) {
225      if (typeSelectorDialog == null) {
226        typeSelectorDialog = new TypeSelectorDialog();
227        typeSelectorDialog.Caption = "Select Problem";
228        typeSelectorDialog.TypeSelector.Caption = "Available Problems";
229        typeSelectorDialog.TypeSelector.Configure(typeof(IProblem), false, true);
230      }
231      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
232        try {
233          Content.DataTypeName = typeSelectorDialog.TypeSelector.SelectedType.Name;
234          Content.DataTypeTypeName = typeSelectorDialog.TypeSelector.SelectedType.AssemblyQualifiedName;
235          data = null;
236          dataViewHost.Content = (IContent)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
237        }
238        catch (Exception ex) {
239          ErrorHandling.ShowErrorDialog(this, "Create new problem data failed.", ex);
240        }
241        SetEnabledStateOfControls();
242      }
243    }
244    private void openFileButton_Click(object sender, EventArgs e) {
245      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
246        CallAsync(
247          () => {
248            IContent problem = null;
249            try {
250              problem = XmlParser.Deserialize<IContent>(openFileDialog.FileName);
251            }
252            catch (Exception) { }
253
254            if (problem != null) {
255              Content.DataTypeName = problem.GetType().Name;
256              Content.DataTypeTypeName = problem.GetType().AssemblyQualifiedName;
257              data = null;
258            } else {
259              using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
260                data = new byte[stream.Length];
261                stream.Read(data, 0, data.Length);
262                stream.Close();
263              }
264            }
265            dataViewHost.Content = problem;
266          },
267          "Save problem data into file failed.",
268          () => SetEnabledStateOfControls()
269        );
270      }
271    }
272    private void saveFileButton_Click(object sender, EventArgs e) {
273      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
274        CallAsync(
275          () => {
276            if (dataViewHost.Content != null) {
277              XmlGenerator.Serialize(dataViewHost.Content, saveFileDialog.FileName);
278            } else {
279              using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
280                stream.Write(data, 0, data.Length);
281                stream.Close();
282              }
283            }
284          },
285          "Save problem data into file failed.",
286          null
287        );
288      }
289    }
290    #endregion
291
292    #region Helpers
293    private void CallAsync(Action call, string errorMessage, Action continueWith) {
294      BeginAsyncCall();
295      call.BeginInvoke(delegate(IAsyncResult result) {
296        Exception exception = null;
297        try {
298          call.EndInvoke(result);
299        }
300        catch (Exception ex) {
301          exception = ex;
302        }
303        EndAsyncCall(errorMessage, exception, continueWith);
304      }, null);
305    }
306    private void BeginAsyncCall() {
307      if (InvokeRequired)
308        Invoke(new Action(BeginAsyncCall));
309      else {
310        Cursor = Cursors.AppStarting;
311        Enabled = false;
312      }
313    }
314    private void EndAsyncCall(string errorMessage, Exception exception, Action continueWith) {
315      if (InvokeRequired)
316        Invoke(new Action<string, Exception, Action>(EndAsyncCall), errorMessage, exception, continueWith);
317      else {
318        Cursor = Cursors.Default;
319        Enabled = true;
320        if (exception != null) ErrorHandling.ShowErrorDialog(this, errorMessage, exception);
321        if (continueWith != null) continueWith();
322      }
323    }
324    #endregion
325  }
326}
Note: See TracBrowser for help on using the repository browser.