Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceProviderViewGeneric.cs @ 11586

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

#2110: renamed generic view class

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Diagnostics;
24using System.IO;
25using System.Linq;
26using System.Threading.Tasks;
27using System.Windows.Forms;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Problems.Instances.Views {
34  [View("ProblemInstanceProviderViewGeneric")]
35  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
36  public partial class ProblemInstanceProviderView<T> : ProblemInstanceProviderView {
37
38    public new IProblemInstanceProvider<T> Content {
39      get { return (IProblemInstanceProvider<T>)base.Content; }
40      set { base.Content = value; }
41    }
42
43    #region Importer & Exporter
44    protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
45    protected IProblemInstanceConsumer consumer;
46    public override IProblemInstanceConsumer Consumer {
47      get { return consumer; }
48      set {
49        consumer = value;
50        SetEnabledStateOfControls();
51        SetTooltip();
52      }
53    }
54
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() {
67      InitializeComponent();
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;
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        var dataDescriptors = Content.GetDataDescriptors().ToList();
83        ShowInstanceLoad(dataDescriptors.Any());
84        instancesComboBox.DataSource = dataDescriptors;
85        instancesComboBox.SelectedIndex = -1;
86      }
87      SetTooltip();
88    }
89
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
100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
102      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
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;
108    }
109
110    private void instancesComboBox_DataSourceChanged(object sender, EventArgs e) {
111      var comboBox = (ComboBox)sender;
112      if (comboBox.DataSource == null)
113        comboBox.Items.Clear();
114      toolTip.SetToolTip(comboBox, String.Empty);
115    }
116
117    protected virtual void instancesComboBox_SelectionChangeCommitted(object sender, EventArgs e) {
118      toolTip.SetToolTip(instancesComboBox, String.Empty);
119      if (instancesComboBox.SelectedIndex >= 0) {
120        var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
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
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    }
146
147    private void libraryInfoButton_Click(object sender, EventArgs e) {
148      Process.Start(Content.WebLink.ToString());
149    }
150
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    }
169
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        }
177      }
178    }
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    }
187  }
188}
Note: See TracBrowser for help on using the repository browser.