Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceProviderViewGeneric.cs @ 9585

Last change on this file since 9585 was 9363, checked in by spimming, 12 years ago

#1888:

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