Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1784:

  • added some regions for readability
  • added import and export methods in DataAnalysisProblem and SymbolicDataAnalysisProblem to reduce code duplication
  • added a recursive and an iterative approach without many linq expression to generate all combinations of list elements in ValueGenerator
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    private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
64      var consumerTypes = Content.GetType().GetInterfaces()
65        .Where(x => x.IsGenericType
66          && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
67
68      if (consumerTypes.Any()) {
69        var instanceTypes = consumerTypes
70          .Select(x => x.GetGenericArguments().First())
71          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
72
73        foreach (var type in instanceTypes) {
74          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
75            yield return (IProblemInstanceProvider)provider;
76        }
77      }
78    }
79
80    public bool ContainsProviders() {
81      return problemInstanceProviderComboBox.Items.Count > 0;
82    }
83
84    protected override void SetEnabledStateOfControls() {
85      base.SetEnabledStateOfControls();
86      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
87      libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
88    }
89
90    private void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
91      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
92        problemInstanceProviderViewHost.Content = SelectedProvider;
93        ProblemInstanceProviderView view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
94        view.Consumer = Content;
95        if (CheckForIProblemInstanceExporter(Content)) {
96          view.Exporter = (IProblemInstanceExporter)Content;
97        }
98        SetTooltip();
99      }
100      SetEnabledStateOfControls();
101    }
102
103    private bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
104      return Content.GetType().GetInterfaces()
105                    .Any(x => x.Equals(typeof(IProblemInstanceExporter)));
106    }
107
108    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
109      var comboBox = (ComboBox)sender;
110      if (comboBox.DataSource == null)
111        comboBox.Items.Clear();
112    }
113
114    private void libraryInfoButton_Click(object sender, EventArgs e) {
115      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
116        if (SelectedProvider != null && SelectedProvider.WebLink != null)
117          Process.Start(SelectedProvider.WebLink.ToString());
118      }
119    }
120
121    #region ToolTip
122    private 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    private 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    #endregion
147  }
148}
Note: See TracBrowser for help on using the repository browser.