Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8031 was 8031, checked in by abeham, 12 years ago

#1782:

  • Changed tooltip generation in ProblemInstanceConsumerViewGeneric (typeof(T).Name doesn't work, because T doesn't change miraculously when the selected provider changes). The current solution might not be optimal, maybe include another property "FileFormat" in IProblemInstanceProvider that can then be TSPLIB, QAPLIB, CSV, etc.
  • Set instances combobox to not display a selected instance initially and disabled button in this case. If that still doesn't reduce the confusion, then I think we have to add problem loading on selected index change and do away with the button.
File size: 4.0 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        instancesComboBox.SelectedIndex = -1;
68      }
69    }
70
71    protected void ShowInstanceLoad(bool show) {
72      if (show) {
73        instanceLabel.Show();
74        instancesComboBox.Show();
75        loadButton.Show();
76      } else {
77        instanceLabel.Hide();
78        instancesComboBox.Hide();
79        loadButton.Hide();
80      }
81    }
82
83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
85      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
86      loadButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null && instancesComboBox.SelectedIndex >= 0;
87    }
88
89    protected virtual void loadButton_Click(object sender, EventArgs e) {
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    private void instancesComboBox_DataSourceChanged(object sender, EventArgs e) {
101      var comboBox = (ComboBox)sender;
102      if (comboBox.DataSource == null)
103        comboBox.Items.Clear();
104    }
105
106    private void instancesComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
107      SetEnabledStateOfControls();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.