Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimization.Views/3.3/ProblemInstanceProviderView.cs @ 17622

Last change on this file since 17622 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.4 KB
RevLine 
[7448]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7448]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;
[7548]23using System.IO;
[7448]24using System.Linq;
25using System.Windows.Forms;
[7538]26using HeuristicLab.Common.Resources;
[7448]27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Problems.Instances;
30
[7558]31namespace HeuristicLab.Optimization.Views {
[7505]32  [View("ProblemInstanceProviderView")]
[7538]33  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
34  public partial class ProblemInstanceProviderView<T> : AsynchronousContentView {
[7448]35
[7538]36    public new IProblemInstanceProvider<T> Content {
37      get { return (IProblemInstanceProvider<T>)base.Content; }
[7448]38      set { base.Content = value; }
39    }
40
41    public ProblemInstanceProviderView() {
42      InitializeComponent();
[7548]43      importButton.Text = String.Empty;
[7538]44      importButton.Image = VSImageLibrary.Open;
[7548]45      toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " instance from file.");
46      loadButton.Text = String.Empty;
[7641]47      loadButton.Image = VSImageLibrary.RefreshDocument;
[7548]48      toolTip.SetToolTip(loadButton, "Load the selected instance.");
[7448]49    }
50
51    protected override void OnContentChanged() {
52      base.OnContentChanged();
53      if (Content == null) {
54        instancesComboBox.DataSource = null;
55      } else {
56        instancesComboBox.DisplayMember = "Name";
[7548]57        instancesComboBox.DataSource = Content.GetDataDescriptors().ToList();
[7448]58      }
59    }
60
61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
[7538]63      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
64      loadButton.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
[7448]65    }
66
[7558]67    protected virtual void loadButton_Click(object sender, EventArgs e) {
[7548]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");
[7466]74      }
[7448]75    }
76
[7558]77    protected virtual void importButton_Click(object sender, EventArgs e) {
[7548]78      openFileDialog.FileName = GetProblemType() + " instance";
[7538]79      if (openFileDialog.ShowDialog() == DialogResult.OK) {
80        T instance = default(T);
81        try {
[7548]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);
[7538]85          return;
86        }
87        try {
[7548]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");
[7538]91        }
92      }
93    }
94
[7448]95    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
96      var comboBox = (ComboBox)sender;
97      if (comboBox.DataSource == null)
98        comboBox.Items.Clear();
99    }
[7548]100
[7558]101    protected virtual string GetProblemType() {
[7548]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    }
[7448]107  }
108}
Note: See TracBrowser for help on using the repository browser.