Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7956 was 7956, checked in by sforsten, 12 years ago

#1782: improvements from mkommend's comment have been applied

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      toolTip.SetToolTip(importButton, "Open a " + GetProblemType() + " problem from file.");
72      exportButton.Text = String.Empty;
73      exportButton.Image = VSImageLibrary.SaveAs;
74      toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " problem to a file.");
75      libraryInfoButton.Text = String.Empty;
76      libraryInfoButton.Image = VSImageLibrary.Help;
77    }
78
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      if (Content == null) {
82        problemInstanceProviders = null;
83        problemInstanceProviderComboBox.DataSource = null;
84      } else {
85        problemInstanceProviderComboBox.DisplayMember = "Name";
86        problemInstanceProviders = ProblemInstanceManager.GetProviders(Content);
87        problemInstanceProviderComboBox.DataSource = ProblemInstanceProviders.OrderBy(x => x.Name).ToList();
88      }
89      SetEnabledStateOfControls();
90    }
91
92    protected override void SetEnabledStateOfControls() {
93      base.SetEnabledStateOfControls();
94      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
95      libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
96      importButton.Enabled = !ReadOnly && !Locked && Content != null && Consumer != null;
97      exportButton.Enabled = !ReadOnly && !Locked && Content != null && Exporter != null;
98      problemInstanceProviderSplitContainer.Panel2Collapsed = !exportButton.Enabled;
99    }
100
101    protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
102      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
103        SelectedProvider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
104        problemInstanceProviderViewHost.Content = SelectedProvider;
105        ProblemInstanceProviderView view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
106        consumer = Content;
107        view.Consumer = Content;
108        if (CheckForIProblemInstanceExporter(Content)) {
109          exporter = (IProblemInstanceExporter)Content;
110        }
111        SetTooltip();
112      } else {
113        SelectedProvider = null;
114      }
115      SetEnabledStateOfControls();
116    }
117
118    protected bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
119      return Content.GetType().GetInterfaces()
120                    .Any(x => x.Equals(typeof(IProblemInstanceExporter)));
121    }
122
123    private void libraryInfoButton_Click(object sender, EventArgs e) {
124      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
125        if (SelectedProvider != null && SelectedProvider.WebLink != null)
126          Process.Start(SelectedProvider.WebLink.ToString());
127      }
128    }
129
130    protected virtual void importButton_Click(object sender, EventArgs e) {
131      openFileDialog.FileName = GetProblemType() + " instance";
132      if (openFileDialog.ShowDialog() == DialogResult.OK) {
133        T instance = default(T);
134        try {
135          instance = GenericSelectedProvider.LoadData(openFileDialog.FileName);
136        }
137        catch (Exception ex) {
138          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
139          return;
140        }
141        try {
142          GenericConsumer.Load(instance);
143        }
144        catch (Exception ex) {
145          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");
146        }
147      }
148    }
149
150    protected void exportButton_Click(object sender, EventArgs e) {
151      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
152        try {
153          GenericSelectedProvider.SaveData(GenericExporter.Export(), saveFileDialog.FileName);
154        }
155        catch (Exception ex) {
156          ErrorHandling.ShowErrorDialog(this, ex);
157        }
158      }
159    }
160
161    protected string GetProblemType() {
162      string dataTypeName = typeof(T).Name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last();
163      if (dataTypeName.EndsWith("Data"))
164        return dataTypeName.Substring(0, dataTypeName.Length - "Data".Length);
165      else return dataTypeName;
166    }
167
168    #region ToolTip
169    protected void SetTooltip() {
170      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
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.