Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Clients.OKB.Views/3.3/Administration/Views/ProblemView.cs @ 15428

Last change on this file since 15428 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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      }
165      catch (Exception ex) {
166        ErrorHandling.ShowErrorDialog(this, "Store authorized users and groups failed.", ex);
167      }
168    }
169    private void problemUserView_SelectedUsersChanged(object sender, EventArgs e) {
170      storeUsersButton.Enabled = !ReadOnly;
171    }
172
173
174    private void refreshDataButton_Click(object sender, EventArgs e) {
175      CallAsync(
176        () => {
177          data = null;
178          dataViewHost.Content = null;
179          data = AdministrationClient.GetProblemData(Content.Id);
180          if (data != null) {
181            using (MemoryStream stream = new MemoryStream(data)) {
182              try {
183                dataViewHost.Content = XmlParser.Deserialize<IContent>(stream);
184              }
185              catch (Exception) { }
186              stream.Close();
187            }
188          }
189        },
190        "Refresh problem data failed.",
191        () => SetEnabledStateOfControls()
192      );
193    }
194    private void storeDataButton_Click(object sender, EventArgs e) {
195      CallAsync(
196        () => {
197          if (dataViewHost.Content != null) {
198            using (MemoryStream stream = new MemoryStream()) {
199              XmlGenerator.Serialize(dataViewHost.Content, stream);
200              stream.Close();
201              data = stream.ToArray();
202            }
203          }
204          AdministrationClient.UpdateProblemData(Content.Id, data);
205        },
206        "Store problem data failed.",
207        null
208      );
209    }
210    private void newDataButton_Click(object sender, EventArgs e) {
211      if (typeSelectorDialog == null) {
212        typeSelectorDialog = new TypeSelectorDialog();
213        typeSelectorDialog.Caption = "Select Problem";
214        typeSelectorDialog.TypeSelector.Caption = "Available Problems";
215        typeSelectorDialog.TypeSelector.Configure(typeof(IProblem), false, true);
216      }
217      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
218        try {
219          Content.DataTypeName = typeSelectorDialog.TypeSelector.SelectedType.Name;
220          Content.DataTypeTypeName = typeSelectorDialog.TypeSelector.SelectedType.AssemblyQualifiedName;
221          data = null;
222          dataViewHost.Content = (IContent)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
223        }
224        catch (Exception ex) {
225          ErrorHandling.ShowErrorDialog(this, "Create new problem data failed.", ex);
226        }
227        SetEnabledStateOfControls();
228      }
229    }
230    private void openFileButton_Click(object sender, EventArgs e) {
231      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
232        CallAsync(
233          () => {
234            IContent problem = null;
235            try {
236              problem = XmlParser.Deserialize<IContent>(openFileDialog.FileName);
237            }
238            catch (Exception) { }
239
240            if (problem != null) {
241              Content.DataTypeName = problem.GetType().Name;
242              Content.DataTypeTypeName = problem.GetType().AssemblyQualifiedName;
243              data = null;
244            } else {
245              using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
246                data = new byte[stream.Length];
247                stream.Read(data, 0, data.Length);
248                stream.Close();
249              }
250            }
251            dataViewHost.Content = problem;
252          },
253          "Save problem data into file failed.",
254          () => SetEnabledStateOfControls()
255        );
256      }
257    }
258    private void saveFileButton_Click(object sender, EventArgs e) {
259      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
260        CallAsync(
261          () => {
262            if (dataViewHost.Content != null) {
263              XmlGenerator.Serialize(dataViewHost.Content, saveFileDialog.FileName);
264            } else {
265              using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
266                stream.Write(data, 0, data.Length);
267                stream.Close();
268              }
269            }
270          },
271          "Save problem data into file failed.",
272          null
273        );
274      }
275    }
276    #endregion
277
278    #region Helpers
279    private void CallAsync(Action call, string errorMessage, Action continueWith) {
280      BeginAsyncCall();
281      call.BeginInvoke(delegate(IAsyncResult result) {
282        Exception exception = null;
283        try {
284          call.EndInvoke(result);
285        }
286        catch (Exception ex) {
287          exception = ex;
288        }
289        EndAsyncCall(errorMessage, exception, continueWith);
290      }, null);
291    }
292    private void BeginAsyncCall() {
293      if (InvokeRequired)
294        Invoke(new Action(BeginAsyncCall));
295      else {
296        Cursor = Cursors.AppStarting;
297        Enabled = false;
298      }
299    }
300    private void EndAsyncCall(string errorMessage, Exception exception, Action continueWith) {
301      if (InvokeRequired)
302        Invoke(new Action<string, Exception, Action>(EndAsyncCall), errorMessage, exception, continueWith);
303      else {
304        Cursor = Cursors.Default;
305        Enabled = true;
306        if (exception != null) ErrorHandling.ShowErrorDialog(this, errorMessage, exception);
307        if (continueWith != null) continueWith();
308      }
309    }
310    #endregion
311  }
312}
Note: See TracBrowser for help on using the repository browser.