Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7665 was 7665, checked in by sforsten, 13 years ago

#1784:

  • minor bug fixes in several views
File size: 5.2 KB
RevLine 
[7664]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();
[7665]70      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && Consumer != null;
71      loadButton.Enabled = !ReadOnly && !Locked && Content != null && Consumer != null;
72      exportButton.Enabled = !ReadOnly && !Locked && Content != null && Exporter != null;
73      problemInstanceProviderSplitContainer.Panel2Collapsed = !exportButton.Enabled;
[7664]74    }
75
76    protected virtual void loadButton_Click(object sender, EventArgs e) {
77      var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
78      var instance = Content.LoadData(descriptor);
79      try {
[7665]80        Consumer.Load(instance);
[7664]81      }
82      catch (Exception ex) {
83        MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
84      }
85    }
86
87    protected virtual void importButton_Click(object sender, EventArgs e) {
88      openFileDialog.FileName = GetProblemType() + " instance";
89      if (openFileDialog.ShowDialog() == DialogResult.OK) {
90        T instance = default(T);
91        try {
92          instance = Content.LoadData(openFileDialog.FileName);
93        }
94        catch (Exception ex) {
95          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
96          return;
97        }
98        try {
[7665]99          Consumer.Load(instance);
[7664]100        }
101        catch (Exception ex) {
102          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");
103        }
104      }
105    }
106
107    protected virtual void exportButton_Click(object sender, EventArgs e) {
108      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
109        try {
[7665]110          Content.SaveData(Exporter.Export(), saveFileDialog.FileName);
[7664]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.