Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceProviderViewGeneric.cs @ 17740

Last change on this file since 17740 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 7.4 KB
RevLine 
[7683]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7683]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;
[10021]23using System.Diagnostics;
24using System.IO;
[7683]25using System.Linq;
[10020]26using System.Threading.Tasks;
[7683]27using System.Windows.Forms;
[10021]28using HeuristicLab.Common.Resources;
[7683]29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
[8198]31using HeuristicLab.PluginInfrastructure;
[7683]32
33namespace HeuristicLab.Problems.Instances.Views {
[7684]34  [View("ProblemInstanceProviderViewGeneric")]
[7683]35  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
[10021]36  public partial class ProblemInstanceProviderView<T> : ProblemInstanceProviderView {
[7683]37
38    public new IProblemInstanceProvider<T> Content {
39      get { return (IProblemInstanceProvider<T>)base.Content; }
40      set { base.Content = value; }
41    }
42
[10021]43    #region Importer & Exporter
44    protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
45    protected IProblemInstanceConsumer consumer;
[7684]46    public override IProblemInstanceConsumer Consumer {
47      get { return consumer; }
48      set {
49        consumer = value;
50        SetEnabledStateOfControls();
[10021]51        SetTooltip();
[7684]52      }
53    }
[7683]54
[10021]55    protected IProblemInstanceExporter<T> GenericExporter { get { return Exporter as IProblemInstanceExporter<T>; } }
56    protected IProblemInstanceExporter exporter;
57    public override IProblemInstanceExporter Exporter {
58      get { return exporter; }
59      set {
60        exporter = value;
61        SetEnabledStateOfControls();
62      }
63    }
64    #endregion
65
66    public ProblemInstanceProviderView() {
[7683]67      InitializeComponent();
[10021]68      importButton.Text = String.Empty;
69      importButton.Image = VSImageLibrary.Open;
70      exportButton.Text = String.Empty;
71      exportButton.Image = VSImageLibrary.SaveAs;
72      libraryInfoButton.Text = String.Empty;
73      libraryInfoButton.Image = VSImageLibrary.Help;
[7683]74    }
75
76    protected override void OnContentChanged() {
77      base.OnContentChanged();
78      if (Content == null) {
79        instancesComboBox.DataSource = null;
80      } else {
81        instancesComboBox.DisplayMember = "Name";
[8912]82        var dataDescriptors = Content.GetDataDescriptors().ToList();
83        ShowInstanceLoad(dataDescriptors.Any());
[7860]84        instancesComboBox.DataSource = dataDescriptors;
[8031]85        instancesComboBox.SelectedIndex = -1;
[7683]86      }
[10021]87      SetTooltip();
[7683]88    }
89
[7860]90    protected void ShowInstanceLoad(bool show) {
91      if (show) {
92        instanceLabel.Show();
93        instancesComboBox.Show();
94      } else {
95        instanceLabel.Hide();
96        instancesComboBox.Hide();
97      }
98    }
99
[7683]100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
[7684]102      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
[10021]103      libraryInfoButton.Enabled = Content != null && Content.WebLink != null;
104      importButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null && Content.CanImportData;
105      splitContainer1.Panel1Collapsed = !importButton.Enabled;
106      exportButton.Enabled = !ReadOnly && !Locked && Content != null && GenericExporter != null && Content.CanExportData;
107      splitContainer2.Panel1Collapsed = !exportButton.Enabled;
[7683]108    }
109
[7956]110    private void instancesComboBox_DataSourceChanged(object sender, EventArgs e) {
[7683]111      var comboBox = (ComboBox)sender;
112      if (comboBox.DataSource == null)
113        comboBox.Items.Clear();
[8912]114      toolTip.SetToolTip(comboBox, String.Empty);
[7683]115    }
[8031]116
[10021]117    protected virtual void instancesComboBox_SelectionChangeCommitted(object sender, EventArgs e) {
[10020]118      toolTip.SetToolTip(instancesComboBox, String.Empty);
[8196]119      if (instancesComboBox.SelectedIndex >= 0) {
120        var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
[10020]121
122        IContentView activeView = (IContentView)MainFormManager.MainForm.ActiveView;
[17062]123        var content = activeView.Content;
[10020]124        // lock active view and show progress bar
[17062]125        Progress.Show(content, "Loading problem instance.", ProgressMode.Indeterminate);
[10020]126
[10021]127        Task.Factory.StartNew(() => {
128          T data;
129          try {
130            data = Content.LoadData(descriptor);
131          } catch (Exception ex) {
132            ErrorHandling.ShowErrorDialog(String.Format("Could not load the problem instance {0}", descriptor.Name), ex);
[17062]133            Progress.Hide(content);
[10021]134            return;
135          }
136          try {
137            GenericConsumer.Load(data);
138          } catch (Exception ex) {
139            ErrorHandling.ShowErrorDialog(String.Format("This problem does not support loading the instance {0}", descriptor.Name), ex);
140          } finally {
[17062]141            Progress.Hide(content);
[10021]142          }
143        });
144      }
145    }
[10020]146
[10021]147    private void libraryInfoButton_Click(object sender, EventArgs e) {
148      Process.Start(Content.WebLink.ToString());
149    }
[10020]150
[10021]151    protected virtual void importButton_Click(object sender, EventArgs e) {
152      openFileDialog.FileName = Content.Name + " instance";
153      if (openFileDialog.ShowDialog() == DialogResult.OK) {
154        T instance = default(T);
155        try {
156          instance = Content.ImportData(openFileDialog.FileName);
157        } catch (Exception ex) {
158          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
159          return;
160        }
161        try {
162          GenericConsumer.Load(instance);
163          instancesComboBox.SelectedIndex = -1;
164        } catch (Exception ex) {
165          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");
166        }
167      }
168    }
[10020]169
[10021]170    protected virtual void exportButton_Click(object sender, EventArgs e) {
171      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
172        try {
173          Content.ExportData(GenericExporter.Export(), saveFileDialog.FileName);
174        } catch (Exception ex) {
175          ErrorHandling.ShowErrorDialog(this, ex);
176        }
[10020]177      }
[8031]178    }
[10021]179
180    protected override void SetTooltip() {
181      toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " from a file in the " + GetProviderFormatInfo() + " format.");
182      toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " to a file in the " + GetProviderFormatInfo() + " format.");
183      if (Content != null && Content.WebLink != null)
184        toolTip.SetToolTip(libraryInfoButton, "Browse to " + Content.WebLink);
185      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
186    }
[7683]187  }
188}
Note: See TracBrowser for help on using the repository browser.