Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 3.6 KB
RevLine 
[7683]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;
[7860]23using System.Collections.Generic;
[7683]24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
[8198]28using HeuristicLab.PluginInfrastructure;
[7683]29
30namespace HeuristicLab.Problems.Instances.Views {
[7684]31  [View("ProblemInstanceProviderViewGeneric")]
[7683]32  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
[7684]33  public partial class ProblemInstanceProviderViewGeneric<T> : ProblemInstanceProviderView {
[7683]34
35    public new IProblemInstanceProvider<T> Content {
36      get { return (IProblemInstanceProvider<T>)base.Content; }
37      set { base.Content = value; }
38    }
39
[7956]40    private IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
41
42    public IProblemInstanceConsumer consumer;
[7684]43    public override IProblemInstanceConsumer Consumer {
44      get { return consumer; }
45      set {
46        consumer = value;
47        SetEnabledStateOfControls();
48      }
49    }
[7683]50
[7684]51    public ProblemInstanceProviderViewGeneric() {
[7683]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";
[7860]61        IEnumerable<IDataDescriptor> dataDescriptors = Content.GetDataDescriptors().ToList();
62        ShowInstanceLoad(dataDescriptors.Count() > 0);
63        instancesComboBox.DataSource = dataDescriptors;
[8031]64        instancesComboBox.SelectedIndex = -1;
[7683]65      }
66    }
67
[7860]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
[7683]78    protected override void SetEnabledStateOfControls() {
79      base.SetEnabledStateOfControls();
[7684]80      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
[7683]81    }
82
[7956]83    private void instancesComboBox_DataSourceChanged(object sender, EventArgs e) {
[7683]84      var comboBox = (ComboBox)sender;
85      if (comboBox.DataSource == null)
86        comboBox.Items.Clear();
87    }
[8031]88
[8196]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);
[8198]93        try {
94          GenericConsumer.Load(instance);
95        }
96        catch (Exception ex) {
[8201]97          ErrorHandling.ShowErrorDialog(String.Format("This problem does not support loading the instance {0}", descriptor.Name), ex);
[8198]98        }
[8196]99      }
[8031]100    }
[7683]101  }
102}
Note: See TracBrowser for help on using the repository browser.