source: trunk/sources/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceProviderViewGeneric.cs @ 8201

Last change on this file since 8201 was 8201, checked in by sforsten, 9 years ago

#1782: removed duplication of exception message, when giving the exception to the ErrorDialog

File size: 3.6 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.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Problems.Instances.Views {
31  [View("ProblemInstanceProviderViewGeneric")]
32  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
33  public partial class ProblemInstanceProviderViewGeneric<T> : ProblemInstanceProviderView {
34
35    public new IProblemInstanceProvider<T> Content {
36      get { return (IProblemInstanceProvider<T>)base.Content; }
37      set { base.Content = value; }
38    }
39
40    private IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
41
42    public IProblemInstanceConsumer consumer;
43    public override IProblemInstanceConsumer Consumer {
44      get { return consumer; }
45      set {
46        consumer = value;
47        SetEnabledStateOfControls();
48      }
49    }
50
51    public ProblemInstanceProviderViewGeneric() {
52      InitializeComponent();
53    }
54
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57      if (Content == null) {
58        instancesComboBox.DataSource = null;
59      } else {
60        instancesComboBox.DisplayMember = "Name";
61        IEnumerable<IDataDescriptor> dataDescriptors = Content.GetDataDescriptors().ToList();
62        ShowInstanceLoad(dataDescriptors.Count() > 0);
63        instancesComboBox.DataSource = dataDescriptors;
64        instancesComboBox.SelectedIndex = -1;
65      }
66    }
67
68    protected void ShowInstanceLoad(bool show) {
69      if (show) {
70        instanceLabel.Show();
71        instancesComboBox.Show();
72      } else {
73        instanceLabel.Hide();
74        instancesComboBox.Hide();
75      }
76    }
77
78    protected override void SetEnabledStateOfControls() {
79      base.SetEnabledStateOfControls();
80      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
81    }
82
83    private void instancesComboBox_DataSourceChanged(object sender, EventArgs e) {
84      var comboBox = (ComboBox)sender;
85      if (comboBox.DataSource == null)
86        comboBox.Items.Clear();
87    }
88
89    private void instancesComboBox_SelectionChangeCommitted(object sender, System.EventArgs e) {
90      if (instancesComboBox.SelectedIndex >= 0) {
91        var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
92        T instance = Content.LoadData(descriptor);
93        try {
94          GenericConsumer.Load(instance);
95        }
96        catch (Exception ex) {
97          ErrorHandling.ShowErrorDialog(String.Format("This problem does not support loading the instance {0}", descriptor.Name), ex);
98        }
99      }
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.