Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceProviderViewGeneric.cs @ 7860

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

#1784:

  • added additional Keijzer problem instances
  • capitalized names real world problem instances
  • added Friedman I and II
  • added link to VariousInstanceProvider
  • changed symbol of info button for ProblemInstanceProvider in ProblemInstanceConsumerView
  • added CSVProvider for classification and regression problems
  • ProblemInstanceProviderViewGeneric only shows controls to load problem instances, if the selected ProblemInstanceProvider contains IDataDescriptor
File size: 6.4 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.Resources;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Problems.Instances.Views {
33  [View("ProblemInstanceProviderViewGeneric")]
34  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
35  public partial class ProblemInstanceProviderViewGeneric<T> : ProblemInstanceProviderView {
36
37    public new IProblemInstanceProvider<T> Content {
38      get { return (IProblemInstanceProvider<T>)base.Content; }
39      set { base.Content = value; }
40    }
41
42    #region Importer & Exporter
43    protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
44    protected IProblemInstanceConsumer consumer;
45    public override IProblemInstanceConsumer Consumer {
46      get { return consumer; }
47      set {
48        consumer = value;
49        SetEnabledStateOfControls();
50      }
51    }
52
53    protected IProblemInstanceExporter<T> GenericExporter { get { return Exporter as IProblemInstanceExporter<T>; } }
54    protected IProblemInstanceExporter exporter;
55    public override IProblemInstanceExporter Exporter {
56      get { return exporter; }
57      set {
58        exporter = value;
59        SetEnabledStateOfControls();
60      }
61    }
62    #endregion
63
64    public ProblemInstanceProviderViewGeneric() {
65      InitializeComponent();
66      importButton.Text = String.Empty;
67      importButton.Image = VSImageLibrary.Open;
68      toolTip.SetToolTip(importButton, "Open a " + GetProblemType() + " problem from file.");
69      exportButton.Text = String.Empty;
70      exportButton.Image = VSImageLibrary.Save;
71      toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " problem to a file.");
72      loadButton.Text = String.Empty;
73      loadButton.Image = VSImageLibrary.RefreshDocument;
74      toolTip.SetToolTip(loadButton, "Load the selected problem.");
75    }
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        instancesComboBox.DataSource = null;
81      } else {
82        instancesComboBox.DisplayMember = "Name";
83        IEnumerable<IDataDescriptor> dataDescriptors = Content.GetDataDescriptors().ToList();
84        ShowInstanceLoad(dataDescriptors.Count() > 0);
85        instancesComboBox.DataSource = dataDescriptors;
86      }
87    }
88
89    protected void ShowInstanceLoad(bool show) {
90      if (show) {
91        instanceLabel.Show();
92        instancesComboBox.Show();
93        loadButton.Show();
94      } else {
95        instanceLabel.Hide();
96        instancesComboBox.Hide();
97        loadButton.Hide();
98      }
99    }
100
101    protected override void SetEnabledStateOfControls() {
102      base.SetEnabledStateOfControls();
103      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
104      loadButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
105      importButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
106      exportButton.Enabled = !ReadOnly && !Locked && Content != null && GenericExporter != null;
107      problemInstanceProviderSplitContainer.Panel2Collapsed = !exportButton.Enabled;
108    }
109
110    protected virtual void loadButton_Click(object sender, EventArgs e) {
111      var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
112      T instance = Content.LoadData(descriptor);
113      try {
114        GenericConsumer.Load(instance);
115      }
116      catch (Exception ex) {
117        MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
118      }
119    }
120
121    protected virtual void importButton_Click(object sender, EventArgs e) {
122      openFileDialog.FileName = GetProblemType() + " instance";
123      if (openFileDialog.ShowDialog() == DialogResult.OK) {
124        T instance = default(T);
125        try {
126          instance = Content.LoadData(openFileDialog.FileName);
127        }
128        catch (Exception ex) {
129          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
130          return;
131        }
132        try {
133          GenericConsumer.Load(instance);
134        }
135        catch (Exception ex) {
136          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");
137        }
138      }
139    }
140
141    protected virtual void exportButton_Click(object sender, EventArgs e) {
142      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
143        try {
144          Content.SaveData(GenericExporter.Export(), saveFileDialog.FileName);
145        }
146        catch (Exception ex) {
147          ErrorHandling.ShowErrorDialog(this, ex);
148        }
149      }
150    }
151
152    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
153      var comboBox = (ComboBox)sender;
154      if (comboBox.DataSource == null)
155        comboBox.Items.Clear();
156    }
157
158    protected virtual string GetProblemType() {
159      string dataTypeName = typeof(T).Name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last();
160      if (dataTypeName.EndsWith("Data"))
161        return dataTypeName.Substring(0, dataTypeName.Length - "Data".Length);
162      else return dataTypeName;
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.