Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/Views/ProblemDataView.cs @ 4587

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

Worked on OKB (#1174)

File size: 9.1 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.ComponentModel;
25using System.IO;
26using System.Linq;
27using System.Windows.Forms;
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 {
37  [View("ProblemData View")]
38  [Content(typeof(ProblemData), true)]
39  public partial class ProblemDataView : AsynchronousContentView {
40    private List<DataType> dataTypeComboBoxValues;
41
42    private long problemId;
43    public long ProblemId {
44      get { return problemId; }
45      set {
46        problemId = value;
47        if (Content != null) {
48          Content.ProblemId = value;
49          SetEnabledStateOfControls();
50        }
51      }
52    }
53
54    public new ProblemData Content {
55      get { return (ProblemData)base.Content; }
56      set { base.Content = value; }
57    }
58
59    public ProblemDataView() {
60      InitializeComponent();
61    }
62
63    protected override void OnInitialized(System.EventArgs e) {
64      base.OnInitialized(e);
65      dataTypeComboBoxValues = OKBClient.Instance.DataTypes.ToList();
66      dataTypeComboBox.DataSource = dataTypeComboBoxValues;
67      dataTypeComboBox.SelectedIndex = -1;
68    }
69
70    protected override void DeregisterContentEvents() {
71      Content.PropertyChanged -= new PropertyChangedEventHandler(Content_PropertyChanged);
72      base.DeregisterContentEvents();
73    }
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.PropertyChanged += new PropertyChangedEventHandler(Content_PropertyChanged);
77    }
78
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      if (Content == null) {
82        dataTypeComboBox.SelectedIndex = -1;
83        viewHost.Content = null;
84        noViewAvailableLabel.Visible = false;
85      } else {
86        dataTypeComboBox.SelectedItem = dataTypeComboBoxValues.FirstOrDefault(d => d.Id == Content.DataTypeId);
87
88        using (MemoryStream stream = new MemoryStream(Content.Data)) {
89          try {
90            viewHost.Content = XmlParser.Deserialize<IContent>(stream);
91            noViewAvailableLabel.Visible = false;
92          }
93          catch (Exception) {
94            viewHost.Content = null;
95            noViewAvailableLabel.Visible = true;
96          }
97          stream.Close();
98        }
99      }
100      fileTextBox.Text = "-";
101    }
102
103    protected override void SetEnabledStateOfControls() {
104      base.SetEnabledStateOfControls();
105      newDataButton.Enabled = !ReadOnly;
106      openFileButton.Enabled = !ReadOnly;
107      saveFileButton.Enabled = (Content != null) && (((Content.Data != null) && (Content.Data.Length != 0)) || (viewHost.Content != null));
108      storeDataButton.Enabled = saveFileButton.Enabled && (Content.ProblemId != 0) && !ReadOnly;
109      dataTypeComboBox.Enabled = Content != null && viewHost.Content == null && !ReadOnly;
110      fileTextBox.Enabled = Content != null;
111      groupBox.Enabled = Content != null;
112    }
113
114    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
115      switch (e.PropertyName) {
116        case "DataTypeId":
117          dataTypeComboBox.SelectedItem = dataTypeComboBoxValues.FirstOrDefault(d => d.Id == Content.DataTypeId);
118          break;
119      }
120    }
121
122    private void refreshDataButton_Click(object sender, EventArgs e) {
123      BeginAsyncCall();
124      var call = new Action(delegate() {
125        ProblemData data = OKBClient.Instance.GetProblemData(ProblemId);
126        Content = data;
127      });
128      call.BeginInvoke(delegate(IAsyncResult result) {
129        call.EndInvoke(result);
130        EndAsyncCall();
131      }, null);
132    }
133    private void storeDataButton_Click(object sender, EventArgs e) {
134      if (viewHost.Content != null) {
135        try {
136          using (MemoryStream stream = new MemoryStream()) {
137            IProblem problem = viewHost.Content as IProblem;
138            XmlGenerator.Serialize(problem, stream);
139            stream.Close();
140            Content.Data = stream.ToArray();
141          }
142        }
143        catch (Exception ex) {
144          ErrorHandling.ShowErrorDialog(this, ex);
145        }
146      }
147      OKBClient.Instance.UpdateProblemData(Content);
148    }
149    private void newDataButton_Click(object sender, EventArgs e) {
150      using (TypeSelectorDialog dialog = new TypeSelectorDialog()) {
151        dialog.Caption = "Select Problem";
152        dialog.TypeSelector.Caption = "Available Problems";
153        dialog.TypeSelector.Configure(typeof(IProblem), false, true);
154        if (dialog.ShowDialog(this) == DialogResult.OK) {
155          try {
156            if (Content == null) Content = new ProblemData { ProblemId = ProblemId };
157            viewHost.Content = (IContent)dialog.TypeSelector.CreateInstanceOfSelectedType();
158            DataType dataType = OKBClient.Instance.ConvertToDataType(viewHost.Content.GetType());
159            dataTypeComboBoxValues = OKBClient.Instance.DataTypes.OrderBy(d => d.Name).ToList();
160            dataTypeComboBox.DataSource = dataTypeComboBoxValues;
161            dataTypeComboBox.SelectedItem = dataType;
162            fileTextBox.Text = "-";
163            noViewAvailableLabel.Visible = false;
164            SetEnabledStateOfControls();
165          }
166          catch (Exception ex) {
167            ErrorHandling.ShowErrorDialog(this, ex);
168          }
169        }
170      }
171    }
172    private void openFileButton_Click(object sender, EventArgs e) {
173      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
174        try {
175          if (Content == null) Content = new ProblemData { ProblemId = ProblemId };
176          IProblem problem = null;
177          try {
178            problem = XmlParser.Deserialize<IProblem>(openFileDialog.FileName);
179          }
180          catch (Exception) { }
181
182          if (problem != null) {
183            viewHost.Content = problem;
184            noViewAvailableLabel.Visible = false;
185            DataType dataType = OKBClient.Instance.ConvertToDataType(viewHost.Content.GetType());
186            dataTypeComboBoxValues = OKBClient.Instance.DataTypes.OrderBy(d => d.Name).ToList();
187            dataTypeComboBox.DataSource = dataTypeComboBoxValues;
188            dataTypeComboBox.SelectedItem = dataType;
189          } else {
190            viewHost.Content = null;
191            noViewAvailableLabel.Visible = true;
192            using (FileStream stream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) {
193              byte[] bytes = new byte[stream.Length];
194              stream.Read(bytes, 0, bytes.Length);
195              stream.Close();
196              Content.Data = bytes;
197            }
198          }
199          fileTextBox.Text = openFileDialog.FileName;
200          SetEnabledStateOfControls();
201        }
202        catch (Exception ex) {
203          ErrorHandling.ShowErrorDialog(this, ex);
204        }
205      }
206    }
207    private void saveFileButton_Click(object sender, EventArgs e) {
208      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
209        try {
210          if (viewHost.Content != null) {
211            XmlGenerator.Serialize(viewHost.Content, saveFileDialog.FileName);
212          } else {
213            using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) {
214              stream.Write(Content.Data, 0, Content.Data.Length);
215              stream.Close();
216            }
217          }
218          fileTextBox.Text = saveFileDialog.FileName;
219        }
220        catch (Exception ex) {
221          ErrorHandling.ShowErrorDialog(this, ex);
222        }
223      }
224    }
225    private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
226      if (Content != null) Content.DataTypeId = ((DataType)dataTypeComboBox.SelectedItem).Id;
227    }
228
229    private void BeginAsyncCall() {
230      if (InvokeRequired)
231        Invoke(new Action(BeginAsyncCall));
232      else {
233        Cursor = Cursors.AppStarting;
234        Enabled = false;
235      }
236    }
237    private void EndAsyncCall() {
238      if (InvokeRequired)
239        Invoke(new Action(EndAsyncCall));
240      else {
241        Cursor = Cursors.Default;
242        Enabled = true;
243      }
244    }
245  }
246}
Note: See TracBrowser for help on using the repository browser.