Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

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