Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7624 was 7427, checked in by ascheibe, 13 years ago

#1174 updated views to work with the changes of r7426

File size: 12.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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      platformComboBoxValues = AdministrationClient.Instance.Platforms.ToList();
66      platformComboBox.DataSource = platformComboBoxValues;
67      platformComboBox.SelectedValueChanged += new EventHandler(platformComboBox_SelectedValueChanged);
68
69      problemClassComboBox.SelectedValueChanged -= new EventHandler(problemClassComboBox_SelectedValueChanged);
70      problemClassComboBoxValues = AdministrationClient.Instance.ProblemClasses.ToList();
71      problemClassComboBox.DataSource = problemClassComboBoxValues;
72      problemClassComboBox.SelectedValueChanged += new EventHandler(problemClassComboBox_SelectedValueChanged);
73
74      data = null;
75      dataViewHost.Content = null;
76      if (Content == null) {
77        platformComboBox.SelectedIndex = -1;
78        problemClassComboBox.SelectedIndex = -1;
79        dataTypeNameTextBox.Text = string.Empty;
80        dataTypeTypeNameTextBox.Text = string.Empty;
81        problemUserView.Content = null;
82        problemUserView.FetchSelectedUsers = null;
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        problemUserView.Content = Access.AccessClient.Instance;
89        problemUserView.FetchSelectedUsers = new Func<List<Guid>>(delegate { return AdministrationClient.GetProblemUsers(Content.Id); });
90      }
91    }
92
93    protected override void SetEnabledStateOfControls() {
94      base.SetEnabledStateOfControls();
95      platformComboBox.Enabled = (Content != null) && !ReadOnly;
96      problemClassComboBox.Enabled = (Content != null) && !ReadOnly;
97      dataTypeGroupBox.Enabled = (Content != null) && !ReadOnly;
98      problemUserView.Enabled = (problemUserView.Content != null) && !ReadOnly;
99      storeUsersButton.Enabled = (problemUserView.GetCheckedUsers() != null) && !ReadOnly;
100      refreshDataButton.Enabled = (Content != null) && (Content.Id != 0);
101      storeDataButton.Enabled = ((data != null) || (dataViewHost.Content != null)) && !ReadOnly;
102      openFileButton.Enabled = (Content != null) && (Content.Id != 0);
103      saveFileButton.Enabled = (data != null) || (dataViewHost.Content != null);
104      noViewAvailableLabel.Visible = dataViewHost.Content == null;
105
106      bool isHL33Platform = platformComboBox.Text == "HeuristicLab 3.3";
107      dataTypeNameTextBox.ReadOnly = isHL33Platform;
108      dataTypeTypeNameTextBox.ReadOnly = isHL33Platform;
109      newDataButton.Enabled = isHL33Platform && (Content.Id != 0) && !ReadOnly;
110    }
111
112    #region Content Events
113    protected override void OnContentPropertyChanged(string propertyName) {
114      switch (propertyName) {
115        case "Id":
116          SetEnabledStateOfControls();
117          break;
118        case "PlatformId":
119          platformComboBox.SelectedItem = platformComboBoxValues.FirstOrDefault(p => p.Id == Content.PlatformId);
120          SetEnabledStateOfControls();
121          break;
122        case "ProblemClassId":
123          problemClassComboBox.SelectedItem = problemClassComboBoxValues.FirstOrDefault(a => a.Id == Content.ProblemClassId);
124          break;
125        case "DataTypeName":
126          dataTypeNameTextBox.Text = Content.DataTypeName;
127          break;
128        case "DataTypeTypeName":
129          dataTypeTypeNameTextBox.Text = Content.DataTypeTypeName;
130          break;
131      }
132    }
133    #endregion
134
135    #region Control Events
136    private void platformComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
137      if (Content != null) {
138        Platform selected = platformComboBox.SelectedItem as Platform;
139        if (selected != null) Content.PlatformId = selected.Id;
140      }
141    }
142    private void problemClassComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
143      if (Content != null) {
144        ProblemClass selected = problemClassComboBox.SelectedItem as ProblemClass;
145        if (selected != null) Content.ProblemClassId = selected.Id;
146      }
147    }
148
149    private void dataTypeNameTextBox_TextChanged(object sender, EventArgs e) {
150      if (dataTypeNameTextBox.Text != Content.DataTypeName)
151        Content.DataTypeName = dataTypeNameTextBox.Text;
152    }
153    private void dataTypeTypeNameTextBox_TextChanged(object sender, EventArgs e) {
154      if (dataTypeTypeNameTextBox.Text != Content.DataTypeTypeName)
155        Content.DataTypeTypeName = dataTypeTypeNameTextBox.Text;
156    }
157
158    private void storeUsersButton_Click(object sender, System.EventArgs e) {
159      try {
160        AdministrationClient.UpdateProblemUsers(Content.Id, problemUserView.GetCheckedUsers().Select(x => x.Id).ToList());
161        storeUsersButton.Enabled = false;
162      }
163      catch (Exception ex) {
164        ErrorHandling.ShowErrorDialog(this, "Store authorized users and groups failed.", ex);
165      }
166    }
167    private void problemUserView_SelectedUsersChanged(object sender, EventArgs e) {
168      storeUsersButton.Enabled = !ReadOnly;
169    }
170
171
172    private void refreshDataButton_Click(object sender, EventArgs e) {
173      CallAsync(
174        () => {
175          data = null;
176          dataViewHost.Content = null;
177          data = AdministrationClient.GetProblemData(Content.Id);
178          if (data != null) {
179            using (MemoryStream stream = new MemoryStream(data)) {
180              try {
181                dataViewHost.Content = XmlParser.Deserialize<IContent>(stream);
182              }
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        }
222        catch (Exception ex) {
223          ErrorHandling.ShowErrorDialog(this, "Create new problem 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 problem = null;
233            try {
234              problem = XmlParser.Deserialize<IContent>(openFileDialog.FileName);
235            }
236            catch (Exception) { }
237
238            if (problem != null) {
239              Content.DataTypeName = problem.GetType().Name;
240              Content.DataTypeTypeName = problem.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 = problem;
250          },
251          "Save problem 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 problem 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}
Note: See TracBrowser for help on using the repository browser.