Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Views/3.4/ProblemInstanceProviderView.cs @ 7664

Last change on this file since 7664 was 7664, checked in by sforsten, 12 years ago

#1784:

  • added Keijzer, Korns, Vladislavleva und Nguyen regression problem instances
  • changes have been made in the ProblemView. Some parts have been replaced with views from Problems.Instances.Views
File size: 5.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.IO;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Problems.Instances.Views {
32  [View("ProblemInstanceProviderView")]
33  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
34  public partial class ProblemInstanceProviderView<T> : AsynchronousContentView {
35
36    public new IProblemInstanceProvider<T> Content {
37      get { return (IProblemInstanceProvider<T>)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public IProblemInstanceConsumer<T> Consumer { get; set; }
42
43    public IProblemInstanceExporter<T> Exporter { get; set; }
44
45    public ProblemInstanceProviderView() {
46      InitializeComponent();
47      importButton.Text = String.Empty;
48      importButton.Image = VSImageLibrary.Open;
49      toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " instance from file.");
50      exportButton.Text = String.Empty;
51      exportButton.Image = VSImageLibrary.Save;
52      toolTip.SetToolTip(exportButton, "Export a " + GetProblemType() + " instance to a file.");
53      loadButton.Text = String.Empty;
54      loadButton.Image = VSImageLibrary.RefreshDocument;
55      toolTip.SetToolTip(loadButton, "Load the selected instance.");
56    }
57
58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      if (Content == null) {
61        instancesComboBox.DataSource = null;
62      } else {
63        instancesComboBox.DisplayMember = "Name";
64        instancesComboBox.DataSource = Content.GetDataDescriptors().ToList();
65      }
66    }
67
68    protected override void SetEnabledStateOfControls() {
69      base.SetEnabledStateOfControls();
70      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
71      loadButton.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
72    }
73
74    protected virtual void loadButton_Click(object sender, EventArgs e) {
75      var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
76      var instance = Content.LoadData(descriptor);
77      try {
78        Content.Consumer.Load(instance);
79      }
80      catch (Exception ex) {
81        MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
82      }
83    }
84
85    protected virtual void importButton_Click(object sender, EventArgs e) {
86      openFileDialog.FileName = GetProblemType() + " instance";
87      if (openFileDialog.ShowDialog() == DialogResult.OK) {
88        T instance = default(T);
89        try {
90          instance = Content.LoadData(openFileDialog.FileName);
91        }
92        catch (Exception ex) {
93          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
94          return;
95        }
96        try {
97          Content.Consumer.Load(instance);
98        }
99        catch (Exception ex) {
100          MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", Path.GetFileName(openFileDialog.FileName), Environment.NewLine + ex.Message), "Cannot load instance");
101        }
102      }
103    }
104
105    protected virtual void exportButton_Click(object sender, EventArgs e) {
106      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
107        try {
108          var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
109          var instance = Content.LoadData(descriptor);
110          Content.SaveData(instance, saveFileDialog.FileName);
111        }
112        catch (Exception ex) {
113          ErrorHandling.ShowErrorDialog(this, ex);
114        }
115      }
116    }
117
118    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
119      var comboBox = (ComboBox)sender;
120      if (comboBox.DataSource == null)
121        comboBox.Items.Clear();
122    }
123
124    protected virtual string GetProblemType() {
125      string dataTypeName = typeof(T).Name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last();
126      if (dataTypeName.EndsWith("Data"))
127        return dataTypeName.Substring(0, dataTypeName.Length - "Data".Length);
128      else return dataTypeName;
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.