Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceConsumerViewGeneric.cs @ 8199

Last change on this file since 8199 was 8199, checked in by mkommend, 12 years ago

#1784: Renamed CSV instance providers and corrected tooltips.

File size: 7.9 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.Diagnostics;
24using System.IO;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Problems.Instances.Views {
33  [View("ProblemInstanceConsumerView")]
34  [Content(typeof(IProblemInstanceConsumer<>), IsDefaultView = true)]
35  public partial class ProblemInstanceConsumerViewGeneric<T> : ProblemInstanceConsumerView {
36
37    public new IProblemInstanceConsumer<T> Content {
38      get { return (IProblemInstanceConsumer<T>)base.Content; }
39      set { base.Content = value; }
40    }
41
42    protected IProblemInstanceProvider<T> GenericSelectedProvider { get { return SelectedProvider as IProblemInstanceProvider<T>; } }
43    public IProblemInstanceProvider SelectedProvider { get; protected set; }
44
45    #region Importer & Exporter
46    protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
47    protected IProblemInstanceConsumer consumer;
48    public IProblemInstanceConsumer Consumer {
49      get { return consumer; }
50      set {
51        consumer = value;
52        SetEnabledStateOfControls();
53      }
54    }
55
56    protected IProblemInstanceExporter<T> GenericExporter { get { return Exporter as IProblemInstanceExporter<T>; } }
57    protected IProblemInstanceExporter exporter;
58    public IProblemInstanceExporter Exporter {
59      get { return exporter; }
60      set {
61        exporter = value;
62        SetEnabledStateOfControls();
63      }
64    }
65    #endregion
66
67    public ProblemInstanceConsumerViewGeneric() {
68      InitializeComponent();
69      importButton.Text = String.Empty;
70      importButton.Image = VSImageLibrary.Open;
71      exportButton.Text = String.Empty;
72      exportButton.Image = VSImageLibrary.SaveAs;
73      libraryInfoButton.Text = String.Empty;
74      libraryInfoButton.Image = VSImageLibrary.Help;
75    }
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        problemInstanceProviders = null;
81        problemInstanceProviderComboBox.DataSource = null;
82      } else {
83        problemInstanceProviderComboBox.DisplayMember = "Name";
84        problemInstanceProviders = ProblemInstanceManager.GetProviders(Content);
85        problemInstanceProviderComboBox.DataSource = ProblemInstanceProviders.OrderBy(x => x.Name).ToList();
86      }
87      SetEnabledStateOfControls();
88    }
89
90    protected override void SetEnabledStateOfControls() {
91      base.SetEnabledStateOfControls();
92      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
93      libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
94      importButton.Enabled = !ReadOnly && !Locked && Content != null && Consumer != null &&
95                             GenericSelectedProvider != null && GenericSelectedProvider.CanImportData;
96      ProviderImportSplitContainer.Panel2Collapsed = !importButton.Enabled;
97      exportButton.Enabled = !ReadOnly && !Locked && Content != null && Exporter != null &&
98                             GenericSelectedProvider != null && GenericSelectedProvider.CanExportData;
99      ProviderExportSplitContainer.Panel2Collapsed = !exportButton.Enabled;
100    }
101
102    protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
103      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
104        SelectedProvider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
105        problemInstanceProviderViewHost.Content = SelectedProvider;
106        ProblemInstanceProviderView view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
107        consumer = Content;
108        view.Consumer = Content;
109        if (CheckForIProblemInstanceExporter(Content)) {
110          exporter = (IProblemInstanceExporter)Content;
111        }
112        SetTooltip();
113      } else {
114        SelectedProvider = null;
115      }
116      SetEnabledStateOfControls();
117    }
118
119    protected bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
120      return Content.GetType().GetInterfaces()
121                    .Any(x => x.Equals(typeof(IProblemInstanceExporter)));
122    }
123
124    private void libraryInfoButton_Click(object sender, EventArgs e) {
125      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
126        if (SelectedProvider != null && SelectedProvider.WebLink != null)
127          Process.Start(SelectedProvider.WebLink.ToString());
128      }
129    }
130
131    protected virtual void importButton_Click(object sender, EventArgs e) {
132      openFileDialog.FileName = GetProblemType() + " instance";
133      if (openFileDialog.ShowDialog() == DialogResult.OK) {
134        T instance = default(T);
135        try {
136          instance = GenericSelectedProvider.ImportData(openFileDialog.FileName);
137        }
138        catch (Exception ex) {
139          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
140          return;
141        }
142        try {
143          GenericConsumer.Load(instance);
144        }
145        catch (Exception ex) {
146          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");
147        }
148      }
149    }
150
151    protected virtual void exportButton_Click(object sender, EventArgs e) {
152      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
153        try {
154          GenericSelectedProvider.ExportData(GenericExporter.Export(), saveFileDialog.FileName);
155        }
156        catch (Exception ex) {
157          ErrorHandling.ShowErrorDialog(this, ex);
158        }
159      }
160    }
161
162    protected string GetProblemType() {
163      return SelectedProvider.Name;
164    }
165
166    #region ToolTip
167    protected void SetTooltip() {
168      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
169      toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " problem from file.");
170      toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " problem to a file.");
171      if (SelectedProvider.WebLink != null)
172        toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
173      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
174    }
175
176    private string GetProviderToolTip() {
177      var provider = SelectedProvider;
178      string toolTip = provider.Name;
179
180      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
181        toolTip = toolTip
182            + Environment.NewLine + Environment.NewLine
183            + provider.ReferencePublication;
184      }
185      if (provider.WebLink != null) {
186        toolTip = toolTip
187            + Environment.NewLine
188            + provider.WebLink.ToString();
189      }
190
191      return toolTip;
192    }
193    #endregion
194  }
195}
Note: See TracBrowser for help on using the repository browser.