Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.Views/3.4/ProblemInstanceConsumerView.cs @ 7823

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

#1784: changes have been applied, according to the review comments of mkommend

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 = 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    public IProblemInstanceProvider SelectedProvider {
43      get;
44      private set;
45    }
46
47    public IEnumerable<IProblemInstanceProvider> ProblemInstanceProviders {
48      get;
49      private set;
50    }
51
52    public ProblemInstanceConsumerView() {
53      InitializeComponent();
54      libraryInfoButton.Text = String.Empty;
55      libraryInfoButton.Image = VSImageLibrary.Internet;
56    }
57
58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      if (Content == null) {
61        ProblemInstanceProviders = null;
62        problemInstanceProviderComboBox.DataSource = null;
63      } else {
64        problemInstanceProviderComboBox.DisplayMember = "Name";
65        ProblemInstanceProviders = GetProblemInstanceProviders();
66        problemInstanceProviderComboBox.DataSource = ProblemInstanceProviders.OrderBy(x => x.Name).ToList();
67      }
68      SetEnabledStateOfControls();
69    }
70
71    private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
72      var consumerTypes = Content.GetType().GetInterfaces()
73        .Where(x => x.IsGenericType
74          && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
75
76      if (consumerTypes.Any()) {
77        var instanceTypes = consumerTypes
78          .Select(x => x.GetGenericArguments().First())
79          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
80
81        foreach (var type in instanceTypes) {
82          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
83            yield return (IProblemInstanceProvider)provider;
84        }
85      }
86    }
87
88    public bool ContainsProviders() {
89      return problemInstanceProviderComboBox.Items.Count > 0;
90    }
91
92    protected override void SetEnabledStateOfControls() {
93      base.SetEnabledStateOfControls();
94      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
95      libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
96    }
97
98    private void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
99      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
100        SelectedProvider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
101        problemInstanceProviderViewHost.Content = SelectedProvider;
102        ProblemInstanceProviderView view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
103        view.Consumer = Content;
104        if (CheckForIProblemInstanceExporter(Content)) {
105          view.Exporter = (IProblemInstanceExporter)Content;
106        }
107        SetTooltip();
108      } else {
109        SelectedProvider = null;
110      }
111
112      SetEnabledStateOfControls();
113    }
114
115    private bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
116      return Content.GetType().GetInterfaces()
117                    .Any(x => x.Equals(typeof(IProblemInstanceExporter)));
118    }
119
120    private void libraryInfoButton_Click(object sender, EventArgs e) {
121      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
122        if (SelectedProvider != null && SelectedProvider.WebLink != null)
123          Process.Start(SelectedProvider.WebLink.ToString());
124      }
125    }
126
127    #region ToolTip
128    private void SetTooltip() {
129      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
130      if (SelectedProvider.WebLink != null)
131        toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
132      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
133    }
134
135    private string GetProviderToolTip() {
136      var provider = SelectedProvider;
137      string toolTip = provider.Name;
138
139      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
140        toolTip = toolTip
141            + Environment.NewLine + Environment.NewLine
142            + provider.ReferencePublication;
143      }
144      if (provider.WebLink != null) {
145        toolTip = toolTip
146            + Environment.NewLine
147            + provider.WebLink.ToString();
148      }
149
150      return toolTip;
151    }
152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.