Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1784:

  • minor bug fixes in several views
File size: 5.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.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 = true)]
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().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        dynamic view = problemInstanceProviderViewHost.ActiveView;
77        dynamic content = Content;
78        view.Consumer = content;
79        if (CheckForIProblemInstanceExporter(Content))
80          view.Exporter = content;
81        //SetTooltip();
82      }
83      SetEnabledStateOfControls();
84    }
85
86    private bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
87      // no check if IProblemInstanceConsumer<T> and IProblemInstanceExporter<T> have the same generic type T
88      return Content.GetType().GetInterfaces()
89                    .Any(x => x.IsGenericType && x.GetGenericTypeDefinition().Equals(typeof(IProblemInstanceExporter<>)));
90    }
91
92    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
93      var comboBox = (ComboBox)sender;
94      if (comboBox.DataSource == null)
95        comboBox.Items.Clear();
96    }
97
98    private void libraryInfoButton_Click(object sender, EventArgs e) {
99      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
100        if (SelectedProvider != null && SelectedProvider.WebLink != null)
101          Process.Start(SelectedProvider.WebLink.ToString());
102      }
103    }
104
105    private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
106      var consumerTypes = Content.GetType().GetInterfaces()
107        .Where(x => x.IsGenericType
108          && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
109
110      if (consumerTypes.Any()) {
111        var instanceTypes = consumerTypes
112          .Select(x => x.GetGenericArguments().First())
113          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
114
115        foreach (var type in instanceTypes) {
116          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
117            yield return (IProblemInstanceProvider)provider;
118        }
119      }
120    }
121
122    //protected void SetTooltip() {
123    //  toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
124    //  if (SelectedProvider.WebLink != null)
125    //    toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
126    //  else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
127    //}
128
129    //protected string GetProviderToolTip() {
130    //  var provider = SelectedProvider;
131    //  string toolTip = provider.Name;
132
133    //  if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
134    //    toolTip = toolTip
135    //        + Environment.NewLine + Environment.NewLine
136    //        + provider.ReferencePublication;
137    //  }
138    //  if (provider.WebLink != null) {
139    //    toolTip = toolTip
140    //        + Environment.NewLine
141    //        + provider.WebLink.ToString();
142    //  }
143
144    //  return toolTip;
145    //}
146  }
147}
Note: See TracBrowser for help on using the repository browser.