Free cookie consent management tool by TermsFeed Policy Generator

source: branches/TimeLimitRunOptimizer/HeuristicLab.Optimization.Views/3.3/ProblemInstanceProviderView.cs @ 10662

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

#1782:

  • HeuristicOptimizationView has been integrated in ProblemView and was deleted afterwards.
  • the icons for the load and the info button have been changed
  • QuadraticAssignmentProblem now also loads name and description of problem instance
  • A unused OpenFileDialog has been removed in QuadraticAssignmentProblemView
File size: 4.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.IO;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Problems.Instances;
30
31namespace HeuristicLab.Optimization.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 ProblemInstanceProviderView() {
42      InitializeComponent();
43      importButton.Text = String.Empty;
44      importButton.Image = VSImageLibrary.Open;
45      toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " instance from file.");
46      loadButton.Text = String.Empty;
47      loadButton.Image = VSImageLibrary.RefreshDocument;
48      toolTip.SetToolTip(loadButton, "Load the selected instance.");
49    }
50
51    protected override void OnContentChanged() {
52      base.OnContentChanged();
53      if (Content == null) {
54        instancesComboBox.DataSource = null;
55      } else {
56        instancesComboBox.DisplayMember = "Name";
57        instancesComboBox.DataSource = Content.GetDataDescriptors().ToList();
58      }
59    }
60
61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
63      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
64      loadButton.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
65    }
66
67    protected virtual void loadButton_Click(object sender, EventArgs e) {
68      var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
69      var instance = Content.LoadData(descriptor);
70      try {
71        Content.Consumer.Load(instance);
72      } catch (Exception ex) {
73        MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
74      }
75    }
76
77    protected virtual void importButton_Click(object sender, EventArgs e) {
78      openFileDialog.FileName = GetProblemType() + " instance";
79      if (openFileDialog.ShowDialog() == DialogResult.OK) {
80        T instance = default(T);
81        try {
82          instance = Content.LoadData(openFileDialog.FileName);
83        } catch (Exception ex) {
84          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
85          return;
86        }
87        try {
88          Content.Consumer.Load(instance);
89        } catch (Exception ex) {
90          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");
91        }
92      }
93    }
94
95    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
96      var comboBox = (ComboBox)sender;
97      if (comboBox.DataSource == null)
98        comboBox.Items.Clear();
99    }
100
101    protected virtual string GetProblemType() {
102      string dataTypeName = typeof(T).Name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last();
103      if (dataTypeName.EndsWith("Data"))
104        return dataTypeName.Substring(0, dataTypeName.Length - "Data".Length);
105      else return dataTypeName;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.