Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9472 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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