Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Clients.OKB.Views/3.3/Administration/Views/ProblemView.cs @ 15866

Last change on this file since 15866 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 12.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.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("Problem View")]
37  [Content(typeof(Problem), true)]
38  public partial class ProblemView : NamedOKBItemView {
39    private List<Platform> platformComboBoxValues;
40    private List<ProblemClass> problemClassComboBoxValues;
41    private TypeSelectorDialog typeSelectorDialog;
42    private byte[] data;
43
44    public new Problem Content {
45      get { return (Problem)base.Content; }
46      set { base.Content = value; }
47    }
48
49    public ProblemView() {
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      problemClassComboBox.SelectedValueChanged -= new EventHandler(problemClassComboBox_SelectedValueChanged);
71      if (AdministrationClient.Instance.ProblemClasses != null)
72        problemClassComboBoxValues = AdministrationClient.Instance.ProblemClasses.ToList();
73      problemClassComboBox.DataSource = problemClassComboBoxValues;
74      problemClassComboBox.SelectedValueChanged += new EventHandler(problemClassComboBox_SelectedValueChanged);
75
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        problemUserView.Content = null;
84        problemUserView.FetchSelectedUsers = null;
85      } else {
86        platformComboBox.SelectedItem = platformComboBoxValues.FirstOrDefault(p => p.Id == Content.PlatformId);
87        problemClassComboBox.SelectedItem = problemClassComboBoxValues.FirstOrDefault(a => a.Id == Content.ProblemClassId);
88        dataTypeNameTextBox.Text = Content.DataTypeName;
89        dataTypeTypeNameTextBox.Text = Content.DataTypeTypeName;
90        problemUserView.Content = Access.AccessClient.Instance;
91        problemUserView.FetchSelectedUsers = new Func<List<Guid>>(delegate { return AdministrationClient.GetProblemUsers(Content.Id); });
92      }
93    }
94
95    protected override void SetEnabledStateOfControls() {
96      base.SetEnabledStateOfControls();
97      platformComboBox.Enabled = (Content != null) && !ReadOnly;
98      problemClassComboBox.Enabled = (Content != null) && !ReadOnly;
99      dataTypeGroupBox.Enabled = (Content != null) && !ReadOnly;
100      problemUserView.Enabled = (problemUserView.Content != null) && !ReadOnly;
101      storeUsersButton.Enabled = (problemUserView.GetCheckedUsers() != 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 "ProblemClassId":
125          problemClassComboBox.SelectedItem = problemClassComboBoxValues.FirstOrDefault(a => a.Id == Content.ProblemClassId);
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 problemClassComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
145      if (Content != null) {
146        ProblemClass selected = problemClassComboBox.SelectedItem as ProblemClass;
147        if (selected != null) Content.ProblemClassId = 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.UpdateProblemUsers(Content.Id, problemUserView.GetCheckedUsers().Select(x => x.Id).ToList());
163        storeUsersButton.Enabled = false;
164      } catch (Exception ex) {
165        ErrorHandling.ShowErrorDialog(this, "Store authorized users and groups failed.", ex);
166      }
167    }
168    private void problemUserView_SelectedUsersChanged(object sender, EventArgs e) {
169      storeUsersButton.Enabled = !ReadOnly;
170    }
171
172
173    private void refreshDataButton_Click(object sender, EventArgs e) {
174      CallAsync(
175        () => {
176          data = null;
177          dataViewHost.Content = null;
178          data = AdministrationClient.GetProblemData(Content.Id);
179          if (data != null) {
180            using (MemoryStream stream = new MemoryStream(data)) {
181              try {
182                dataViewHost.Content = XmlParser.Deserialize<IContent>(stream);
183              } catch (Exception) { }
184              stream.Close();
185            }
186          }
187        },
188        "Refresh problem data failed.",
189        () => SetEnabledStateOfControls()
190      );
191    }
192    private void storeDataButton_Click(object sender, EventArgs e) {
193      CallAsync(
194        () => {
195          if (dataViewHost.Content != null) {
196            using (MemoryStream stream = new MemoryStream()) {
197              XmlGenerator.Serialize(dataViewHost.Content, stream);
198              stream.Close();
199              data = stream.ToArray();
200            }
201          }
202          AdministrationClient.UpdateProblemData(Content.Id, data);
203        },
204        "Store problem 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 Problem";
212        typeSelectorDialog.TypeSelector.Caption = "Available Problems";
213        typeSelectorDialog.TypeSelector.Configure(typeof(IProblem), 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        } catch (Exception ex) {
222          ErrorHandling.ShowErrorDialog(this, "Create new problem data failed.", ex);
223        }
224        SetEnabledStateOfControls();
225      }
226    }
227    private void openFileButton_Click(object sender, EventArgs e) {
228      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
229        CallAsync(
230          () => {
231            IContent problem = null;
232            try {
233              problem = XmlParser.Deserialize<IContent>(openFileDialog.FileName);
234            } catch (Exception) { }
235
236            if (problem != null) {
237              Content.DataTypeName = problem.GetType().Name;
238              Content.DataTypeTypeName = problem.GetType().AssemblyQualifiedName;
239              data = null;
240            } else {
241              using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
242                data = new byte[stream.Length];
243                stream.Read(data, 0, data.Length);
244                stream.Close();
245              }
246            }
247            dataViewHost.Content = problem;
248          },
249          "Save problem data into file failed.",
250          () => SetEnabledStateOfControls()
251        );
252      }
253    }
254    private void saveFileButton_Click(object sender, EventArgs e) {
255      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
256        CallAsync(
257          () => {
258            if (dataViewHost.Content != null) {
259              XmlGenerator.Serialize(dataViewHost.Content, saveFileDialog.FileName);
260            } else {
261              using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
262                stream.Write(data, 0, data.Length);
263                stream.Close();
264              }
265            }
266          },
267          "Save problem data into file failed.",
268          null
269        );
270      }
271    }
272    #endregion
273
274    #region Helpers
275    private void CallAsync(Action call, string errorMessage, Action continueWith) {
276      BeginAsyncCall();
277      call.BeginInvoke(delegate (IAsyncResult result) {
278        Exception exception = null;
279        try {
280          call.EndInvoke(result);
281        } catch (Exception ex) {
282          exception = ex;
283        }
284        EndAsyncCall(errorMessage, exception, continueWith);
285      }, null);
286    }
287    private void BeginAsyncCall() {
288      if (InvokeRequired)
289        Invoke(new Action(BeginAsyncCall));
290      else {
291        Cursor = Cursors.AppStarting;
292        Enabled = false;
293      }
294    }
295    private void EndAsyncCall(string errorMessage, Exception exception, Action continueWith) {
296      if (InvokeRequired)
297        Invoke(new Action<string, Exception, Action>(EndAsyncCall), errorMessage, exception, continueWith);
298      else {
299        Cursor = Cursors.Default;
300        Enabled = true;
301        if (exception != null) ErrorHandling.ShowErrorDialog(this, errorMessage, exception);
302        if (continueWith != null) continueWith();
303      }
304    }
305    #endregion
306  }
307}
Note: See TracBrowser for help on using the repository browser.