Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8031 was 8031, checked in by abeham, 12 years ago

#1782:

  • Changed tooltip generation in ProblemInstanceConsumerViewGeneric (typeof(T).Name doesn't work, because T doesn't change miraculously when the selected provider changes). The current solution might not be optimal, maybe include another property "FileFormat" in IProblemInstanceProvider that can then be TSPLIB, QAPLIB, CSV, etc.
  • Set instances combobox to not display a selected instance initially and disabled button in this case. If that still doesn't reduce the confusion, then I think we have to add problem loading on selected index change and do away with the button.
File size: 7.7 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      exportButton.Enabled = !ReadOnly && !Locked && Content != null && Exporter != null;
96      problemInstanceProviderSplitContainer.Panel2Collapsed = !exportButton.Enabled;
97    }
98
99    protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
100      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
101        SelectedProvider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
102        problemInstanceProviderViewHost.Content = SelectedProvider;
103        ProblemInstanceProviderView view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
104        consumer = Content;
105        view.Consumer = Content;
106        if (CheckForIProblemInstanceExporter(Content)) {
107          exporter = (IProblemInstanceExporter)Content;
108        }
109        SetTooltip();
110      } else {
111        SelectedProvider = null;
112      }
113      SetEnabledStateOfControls();
114    }
115
116    protected bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
117      return Content.GetType().GetInterfaces()
118                    .Any(x => x.Equals(typeof(IProblemInstanceExporter)));
119    }
120
121    private void libraryInfoButton_Click(object sender, EventArgs e) {
122      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
123        if (SelectedProvider != null && SelectedProvider.WebLink != null)
124          Process.Start(SelectedProvider.WebLink.ToString());
125      }
126    }
127
128    protected virtual void importButton_Click(object sender, EventArgs e) {
129      openFileDialog.FileName = GetProblemType() + " instance";
130      if (openFileDialog.ShowDialog() == DialogResult.OK) {
131        T instance = default(T);
132        try {
133          instance = GenericSelectedProvider.LoadData(openFileDialog.FileName);
134        }
135        catch (Exception ex) {
136          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
137          return;
138        }
139        try {
140          GenericConsumer.Load(instance);
141        }
142        catch (Exception ex) {
143          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");
144        }
145      }
146    }
147
148    protected void exportButton_Click(object sender, EventArgs e) {
149      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
150        try {
151          GenericSelectedProvider.SaveData(GenericExporter.Export(), saveFileDialog.FileName);
152        }
153        catch (Exception ex) {
154          ErrorHandling.ShowErrorDialog(this, ex);
155        }
156      }
157    }
158
159    protected string GetProblemType() {
160      return SelectedProvider.Name;
161    }
162
163    #region ToolTip
164    protected void SetTooltip() {
165      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
166      toolTip.SetToolTip(importButton, "Open a " + GetProblemType() + " problem from file.");
167      toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " problem to a file.");
168      if (SelectedProvider.WebLink != null)
169        toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
170      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
171    }
172
173    private string GetProviderToolTip() {
174      var provider = SelectedProvider;
175      string toolTip = provider.Name;
176
177      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
178        toolTip = toolTip
179            + Environment.NewLine + Environment.NewLine
180            + provider.ReferencePublication;
181      }
182      if (provider.WebLink != null) {
183        toolTip = toolTip
184            + Environment.NewLine
185            + provider.WebLink.ToString();
186      }
187
188      return toolTip;
189    }
190    #endregion
191  }
192}
Note: See TracBrowser for help on using the repository browser.