Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1782: improvements from mkommend's comment have been applied

File size: 3.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.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
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      loadButton.Text = String.Empty;
54      loadButton.Image = VSImageLibrary.RefreshDocument;
55      toolTip.SetToolTip(loadButton, "Load the selected problem.");
56    }
57
58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      if (Content == null) {
61        instancesComboBox.DataSource = null;
62      } else {
63        instancesComboBox.DisplayMember = "Name";
64        IEnumerable<IDataDescriptor> dataDescriptors = Content.GetDataDescriptors().ToList();
65        ShowInstanceLoad(dataDescriptors.Count() > 0);
66        instancesComboBox.DataSource = dataDescriptors;
67      }
68    }
69
70    protected void ShowInstanceLoad(bool show) {
71      if (show) {
72        instanceLabel.Show();
73        instancesComboBox.Show();
74        loadButton.Show();
75      } else {
76        instanceLabel.Hide();
77        instancesComboBox.Hide();
78        loadButton.Hide();
79      }
80    }
81
82    protected override void SetEnabledStateOfControls() {
83      base.SetEnabledStateOfControls();
84      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
85      loadButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
86    }
87
88    protected virtual void loadButton_Click(object sender, EventArgs e) {
89      var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
90      T instance = Content.LoadData(descriptor);
91      try {
92        GenericConsumer.Load(instance);
93      }
94      catch (Exception ex) {
95        MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
96      }
97    }
98
99    private void instancesComboBox_DataSourceChanged(object sender, EventArgs e) {
100      var comboBox = (ComboBox)sender;
101      if (comboBox.DataSource == null)
102        comboBox.Items.Clear();
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.