Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1782:

  • removed loadButton
  • a new problem instance is loaded as soon as it is selected in the combobox
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;
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    public 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        IEnumerable<IDataDescriptor> dataDescriptors = Content.GetDataDescriptors().ToList();
61        ShowInstanceLoad(dataDescriptors.Count() > 0);
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    }
87
88    private void instancesComboBox_SelectionChangeCommitted(object sender, System.EventArgs e) {
89      if (instancesComboBox.SelectedIndex >= 0) {
90        var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
91        T instance = Content.LoadData(descriptor);
92        try {
93          GenericConsumer.Load(instance);
94        }
95        catch (Exception ex) {
96          MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
97        }
98      }
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.