Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1782:

  • added CanSave property to IProblemInstanceProvider to know if it can save its data
  • ProblemInstanceConsumerViewGeneric only shows the export button if an IProblemInstanceExporter is available and the selected SelectedProvider has the CanSave property set to true
  • added a default implementation for CanSave and SaveData to ProblemInstanceProvider, so classes which inherit from it, don't have to implement it.
  • Classes in Problems.Instances.DataAnalysis which implemented the IProblemInstanceProvider now inherit from the class ProblemInstanceProvider, so some code is obsolete now and had been deleted
File size: 7.8 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                             GenericSelectedProvider != null && GenericSelectedProvider.CanSaveData;
97      problemInstanceProviderSplitContainer.Panel2Collapsed = !exportButton.Enabled;
98    }
99
100    protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
101      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
102        SelectedProvider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
103        problemInstanceProviderViewHost.Content = SelectedProvider;
104        ProblemInstanceProviderView view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
105        consumer = Content;
106        view.Consumer = Content;
107        if (CheckForIProblemInstanceExporter(Content)) {
108          exporter = (IProblemInstanceExporter)Content;
109        }
110        SetTooltip();
111      } else {
112        SelectedProvider = null;
113      }
114      SetEnabledStateOfControls();
115    }
116
117    protected bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
118      return Content.GetType().GetInterfaces()
119                    .Any(x => x.Equals(typeof(IProblemInstanceExporter)));
120    }
121
122    private void libraryInfoButton_Click(object sender, EventArgs e) {
123      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
124        if (SelectedProvider != null && SelectedProvider.WebLink != null)
125          Process.Start(SelectedProvider.WebLink.ToString());
126      }
127    }
128
129    protected virtual void importButton_Click(object sender, EventArgs e) {
130      openFileDialog.FileName = GetProblemType() + " instance";
131      if (openFileDialog.ShowDialog() == DialogResult.OK) {
132        T instance = default(T);
133        try {
134          instance = GenericSelectedProvider.LoadData(openFileDialog.FileName);
135        }
136        catch (Exception ex) {
137          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
138          return;
139        }
140        try {
141          GenericConsumer.Load(instance);
142        }
143        catch (Exception ex) {
144          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");
145        }
146      }
147    }
148
149    protected void exportButton_Click(object sender, EventArgs e) {
150      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
151        try {
152          GenericSelectedProvider.SaveData(GenericExporter.Export(), saveFileDialog.FileName);
153        }
154        catch (Exception ex) {
155          ErrorHandling.ShowErrorDialog(this, ex);
156        }
157      }
158    }
159
160    protected string GetProblemType() {
161      return SelectedProvider.Name;
162    }
163
164    #region ToolTip
165    protected void SetTooltip() {
166      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
167      toolTip.SetToolTip(importButton, "Open a " + GetProblemType() + " problem from file.");
168      toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " problem to a file.");
169      if (SelectedProvider.WebLink != null)
170        toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
171      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
172    }
173
174    private string GetProviderToolTip() {
175      var provider = SelectedProvider;
176      string toolTip = provider.Name;
177
178      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
179        toolTip = toolTip
180            + Environment.NewLine + Environment.NewLine
181            + provider.ReferencePublication;
182      }
183      if (provider.WebLink != null) {
184        toolTip = toolTip
185            + Environment.NewLine
186            + provider.WebLink.ToString();
187      }
188
189      return toolTip;
190    }
191    #endregion
192  }
193}
Note: See TracBrowser for help on using the repository browser.