Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11150 was 10009, checked in by abeham, 11 years ago

#2110: renamed generic view class

File size: 7.6 KB
RevLine 
[7683]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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;
[10000]23using System.Diagnostics;
24using System.IO;
[7683]25using System.Linq;
[9991]26using System.Threading.Tasks;
[7683]27using System.Windows.Forms;
[10000]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)]
[10009]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
[10000]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();
[10000]51        SetTooltip();
[7684]52      }
53    }
[7683]54
[10000]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
[10009]66    public ProblemInstanceProviderView() {
[7683]67      InitializeComponent();
[10000]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      }
[10000]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;
[10000]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
[10000]117    protected virtual void instancesComboBox_SelectionChangeCommitted(object sender, EventArgs e) {
[9991]118      toolTip.SetToolTip(instancesComboBox, String.Empty);
[8196]119      if (instancesComboBox.SelectedIndex >= 0) {
120        var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
[9991]121
122        IContentView activeView = (IContentView)MainFormManager.MainForm.ActiveView;
123        var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
124        // lock active view and show progress bar
125        mainForm.AddOperationProgressToContent(activeView.Content, "Loading problem instance.");
126
[10000]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);
133            mainForm.RemoveOperationProgressFromContent(activeView.Content);
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 {
141            mainForm.RemoveOperationProgressFromContent(activeView.Content);
142          }
143        });
144      }
145    }
[9991]146
[10000]147    private void libraryInfoButton_Click(object sender, EventArgs e) {
148      Process.Start(Content.WebLink.ToString());
149    }
[9991]150
[10000]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    }
[9991]169
[10000]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        }
[9991]177      }
[8031]178    }
[10000]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.