Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Views/3.4/ProblemInstanceConsumerView.cs @ 7759

Last change on this file since 7759 was 7698, checked in by sforsten, 13 years ago

#1784:

  • ProblemInstanceProvider are sorted now
  • the return values of ValueGenerator have been changed to !IEnumerable
  • changes have been applied to classes which are using the ValueGenerator
  • change of the cast in ProblemInstanceProviderViewGeneric and !importButton.Enable is set now in SetEnabledStateOfControls
File size: 5.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.Diagnostics;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Problems.Instances.Views {
33  [View("ProblemInstanceConsumerView")]
34  [Content(typeof(IProblemInstanceConsumer), IsDefaultView = false)]
35  public sealed partial class ProblemInstanceConsumerView : AsynchronousContentView {
36
37    public new IProblemInstanceConsumer Content {
38      get { return (IProblemInstanceConsumer)base.Content; }
39      set { base.Content = value; }
40    }
41
42    private IProblemInstanceProvider SelectedProvider {
43      get { return (problemInstanceProviderComboBox.SelectedIndex >= 0 ? (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem : null); }
44    }
45
46    public ProblemInstanceConsumerView() {
47      InitializeComponent();
48      libraryInfoButton.Text = String.Empty;
49      libraryInfoButton.Image = VSImageLibrary.Internet;
50    }
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54      if (Content == null) {
55        problemInstanceProviderComboBox.DataSource = null;
56      } else {
57        problemInstanceProviderComboBox.DisplayMember = "Name";
58        problemInstanceProviderComboBox.DataSource = GetProblemInstanceProviders().OrderBy(x => x.Name).ToList();
59      }
60      SetEnabledStateOfControls();
61    }
62
63    public bool ContainsProviders() {
64      return problemInstanceProviderComboBox.Items.Count > 0;
65    }
66
67    protected override void SetEnabledStateOfControls() {
68      base.SetEnabledStateOfControls();
69      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
70      libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
71    }
72
73    private void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
74      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
75        problemInstanceProviderViewHost.Content = SelectedProvider;
76        ProblemInstanceProviderView view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
77        view.Consumer = Content;
78        if (CheckForIProblemInstanceExporter(Content)) {
79          view.Exporter = (IProblemInstanceExporter)Content;
80        }
81        SetTooltip();
82      }
83      SetEnabledStateOfControls();
84    }
85
86    private bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
87      return Content.GetType().GetInterfaces()
88                    .Any(x => x.Equals(typeof(IProblemInstanceExporter)));
89    }
90
91    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
92      var comboBox = (ComboBox)sender;
93      if (comboBox.DataSource == null)
94        comboBox.Items.Clear();
95    }
96
97    private void libraryInfoButton_Click(object sender, EventArgs e) {
98      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
99        if (SelectedProvider != null && SelectedProvider.WebLink != null)
100          Process.Start(SelectedProvider.WebLink.ToString());
101      }
102    }
103
104    private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
105      var consumerTypes = Content.GetType().GetInterfaces()
106        .Where(x => x.IsGenericType
107          && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
108
109      if (consumerTypes.Any()) {
110        var instanceTypes = consumerTypes
111          .Select(x => x.GetGenericArguments().First())
112          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
113
114        foreach (var type in instanceTypes) {
115          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
116            yield return (IProblemInstanceProvider)provider;
117        }
118      }
119    }
120
121    private void SetTooltip() {
122      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
123      if (SelectedProvider.WebLink != null)
124        toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
125      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
126    }
127
128    private string GetProviderToolTip() {
129      var provider = SelectedProvider;
130      string toolTip = provider.Name;
131
132      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
133        toolTip = toolTip
134            + Environment.NewLine + Environment.NewLine
135            + provider.ReferencePublication;
136      }
137      if (provider.WebLink != null) {
138        toolTip = toolTip
139            + Environment.NewLine
140            + provider.WebLink.ToString();
141      }
142
143      return toolTip;
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.