Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Optimization.Views/3.3/ProblemView.cs @ 7669

Last change on this file since 7669 was 7669, checked in by spimming, 12 years ago

#1680: merged changes from trunk into branch

File size: 5.4 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.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Problems.Instances;
32
33namespace HeuristicLab.Optimization.Views {
34  /// <summary>
35  /// The base class for visual representations of items.
36  /// </summary>
37  [View("Problem View")]
38  [Content(typeof(IProblem), true)]
39  public partial class ProblemView : ParameterizedNamedItemView {
40    public new IProblem Content {
41      get { return (IProblem)base.Content; }
42      set { base.Content = value; }
43    }
44
45    protected IProblemInstanceProvider SelectedProvider {
46      get { return (problemInstanceProviderComboBox.SelectedIndex >= 0 ? (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem : null); }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="ItemBaseView"/>.
51    /// </summary>
52    public ProblemView() {
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        problemInstanceProviderComboBox.DataSource = null;
62      } else {
63        problemInstanceProviderComboBox.DisplayMember = "Name";
64        problemInstanceProviderComboBox.DataSource = GetProblemInstanceProviders().ToList();
65        problemInstanceSplitContainer.Panel1Collapsed = problemInstanceProviderComboBox.Items.Count <= 0;
66
67      }
68      SetEnabledStateOfControls();
69    }
70
71    protected override void SetEnabledStateOfControls() {
72      base.SetEnabledStateOfControls();
73      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
74      libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
75    }
76
77    protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
78      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
79        dynamic provider = SelectedProvider;
80        dynamic consumer = Content;
81        provider.Consumer = consumer;
82        problemInstanceProviderViewHost.Content = SelectedProvider;
83        SetTooltip();
84      }
85      SetEnabledStateOfControls();
86    }
87
88    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
89      var comboBox = (ComboBox)sender;
90      if (comboBox.DataSource == null)
91        comboBox.Items.Clear();
92    }
93
94    protected virtual void libraryInfoButton_Click(object sender, EventArgs e) {
95      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
96        if (SelectedProvider != null && SelectedProvider.WebLink != null)
97          Process.Start(SelectedProvider.WebLink.ToString());
98      }
99    }
100
101    protected virtual IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
102      var consumerTypes = Content.GetType().GetInterfaces()
103        .Where(x => x.IsGenericType
104          && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
105
106      if (consumerTypes.Any()) {
107        var instanceTypes = consumerTypes
108          .Select(x => x.GetGenericArguments().First())
109          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
110
111        foreach (var type in instanceTypes) {
112          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
113            yield return (IProblemInstanceProvider)provider;
114        }
115      }
116    }
117
118    protected virtual void SetTooltip() {
119      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
120      if (SelectedProvider.WebLink != null)
121        toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
122      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
123    }
124
125    protected virtual string GetProviderToolTip() {
126      var provider = SelectedProvider;
127      string toolTip = provider.Name;
128
129      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
130        toolTip = toolTip
131            + Environment.NewLine + Environment.NewLine
132            + provider.ReferencePublication;
133      }
134      if (provider.WebLink != null) {
135        toolTip = toolTip
136            + Environment.NewLine
137            + provider.WebLink.ToString();
138      }
139
140      return toolTip;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.