Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1784:

  • added some regions for readability
  • added import and export methods in DataAnalysisProblem and SymbolicDataAnalysisProblem to reduce code duplication
  • added a recursive and an iterative approach without many linq expression to generate all combinations of list elements in ValueGenerator
File size: 6.0 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("ProblemInstanceProviderViewGeneric")]
33  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
34  public partial class ProblemInstanceProviderViewGeneric<T> : ProblemInstanceProviderView {
35
36    public new IProblemInstanceProvider<T> Content {
37      get { return (IProblemInstanceProvider<T>)base.Content; }
38      set { base.Content = value; }
39    }
40
41    #region Importer & Exporter
42    protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
43    protected IProblemInstanceConsumer consumer;
44    public override IProblemInstanceConsumer Consumer {
45      get { return consumer; }
46      set {
47        consumer = value;
48        SetEnabledStateOfControls();
49      }
50    }
51
52    protected IProblemInstanceExporter<T> GenericExporter { get { return Exporter as IProblemInstanceExporter<T>; } }
53    protected IProblemInstanceExporter exporter;
54    public override IProblemInstanceExporter Exporter {
55      get { return exporter; }
56      set {
57        exporter = value;
58        SetEnabledStateOfControls();
59      }
60    }
61    #endregion
62
63    public ProblemInstanceProviderViewGeneric() {
64      InitializeComponent();
65      importButton.Text = String.Empty;
66      importButton.Image = VSImageLibrary.Open;
67      toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " problem from file.");
68      exportButton.Text = String.Empty;
69      exportButton.Image = VSImageLibrary.Save;
70      toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " problem to a file.");
71      loadButton.Text = String.Empty;
72      loadButton.Image = VSImageLibrary.RefreshDocument;
73      toolTip.SetToolTip(loadButton, "Load the selected problem.");
74    }
75
76    protected override void OnContentChanged() {
77      base.OnContentChanged();
78      if (Content == null) {
79        instancesComboBox.DataSource = null;
80      } else {
81        instancesComboBox.DisplayMember = "Name";
82        instancesComboBox.DataSource = Content.GetDataDescriptors().ToList();
83      }
84    }
85
86    protected override void SetEnabledStateOfControls() {
87      base.SetEnabledStateOfControls();
88      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
89      loadButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
90      importButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
91      exportButton.Enabled = !ReadOnly && !Locked && Content != null && GenericExporter != null;
92      problemInstanceProviderSplitContainer.Panel2Collapsed = !exportButton.Enabled;
93    }
94
95    protected virtual void loadButton_Click(object sender, EventArgs e) {
96      var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
97      T instance = Content.LoadData(descriptor);
98      try {
99        GenericConsumer.Load(instance);
100      }
101      catch (Exception ex) {
102        MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
103      }
104    }
105
106    protected virtual void importButton_Click(object sender, EventArgs e) {
107      openFileDialog.FileName = GetProblemType() + " instance";
108      if (openFileDialog.ShowDialog() == DialogResult.OK) {
109        T instance = default(T);
110        try {
111          instance = Content.LoadData(openFileDialog.FileName);
112        }
113        catch (Exception ex) {
114          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
115          return;
116        }
117        try {
118          GenericConsumer.Load(instance);
119        }
120        catch (Exception ex) {
121          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");
122        }
123      }
124    }
125
126    protected virtual void exportButton_Click(object sender, EventArgs e) {
127      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
128        try {
129          Content.SaveData(GenericExporter.Export(), saveFileDialog.FileName);
130        }
131        catch (Exception ex) {
132          ErrorHandling.ShowErrorDialog(this, ex);
133        }
134      }
135    }
136
137    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
138      var comboBox = (ComboBox)sender;
139      if (comboBox.DataSource == null)
140        comboBox.Items.Clear();
141    }
142
143    protected virtual string GetProblemType() {
144      string dataTypeName = typeof(T).Name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last();
145      if (dataTypeName.EndsWith("Data"))
146        return dataTypeName.Substring(0, dataTypeName.Length - "Data".Length);
147      else return dataTypeName;
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.