Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views/3.3/ProblemView.cs @ 7548

Last change on this file since 7548 was 7548, checked in by abeham, 12 years ago

#1614: changed according to architects review

File size: 5.7 KB
RevLine 
[7448]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;
[7548]24using System.Diagnostics;
[7448]25using System.Linq;
[7539]26using System.Reflection;
[7448]27using System.Windows.Forms;
[7548]28using HeuristicLab.Common.Resources;
[7448]29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32using HeuristicLab.Optimization;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Problems.Instances;
35
36namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views {
37  [View("ProblemView")]
38  [Content(typeof(Problem), IsDefaultView = true)]
39  public partial class ProblemView : ParameterizedNamedItemView {
40
41    public new Problem Content {
42      get { return (Problem)base.Content; }
43      set { base.Content = value; }
44    }
45
[7548]46    private IProblemInstanceProvider SelectedProvider {
47      get { return (problemInstanceProviderComboBox.SelectedIndex >= 0 ? (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem : null); }
48    }
49
[7448]50    public ProblemView() {
51      InitializeComponent();
[7548]52      libraryInfoButton.Text = String.Empty;
53      libraryInfoButton.Image = VSImageLibrary.Information;
[7448]54    }
55
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      if (Content == null) {
59        problemInstanceProviderComboBox.DataSource = null;
60      } else {
61        problemInstanceProviderComboBox.DisplayMember = "Name";
62        problemInstanceProviderComboBox.DataSource = GetProblemInstanceProviders().ToList();
63      }
[7548]64      SetEnabledStateOfControls();
[7448]65    }
66
67    protected override void SetEnabledStateOfControls() {
68      base.SetEnabledStateOfControls();
[7548]69      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
70      libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
[7448]71    }
72
73    private void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
[7482]74      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
[7548]75        var genericType = SelectedProvider.GetType().GetInterfaces().Single(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IProblemInstanceProvider<>)).GetGenericArguments().First();
[7539]76        this.GetType().GetMethod("SetConsumable", BindingFlags.NonPublic | BindingFlags.Instance)
[7548]77          .MakeGenericMethod(genericType).Invoke(this, new object[] { SelectedProvider, Content });
78        problemInstanceProviderViewHost.Content = SelectedProvider;
79        SetTooltip();
[7482]80      }
[7548]81      SetEnabledStateOfControls();
[7448]82    }
83
[7548]84    // Do not change method without changing the reflection call above
85    [Obsolete]
86    private void SetConsumable<T>(IProblemInstanceProvider<T> producer, IProblemInstanceConsumer<T> consumer) {
[7538]87      producer.Consumer = consumer;
88    }
89
[7448]90    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
91      var comboBox = (ComboBox)sender;
92      if (comboBox.DataSource == null)
93        comboBox.Items.Clear();
94    }
95
[7548]96    private void libraryInfoButton_Click(object sender, EventArgs e) {
97      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
98        if (SelectedProvider != null && SelectedProvider.WebLink != null)
99          Process.Start(SelectedProvider.WebLink.ToString());
100      }
101    }
102
[7448]103    protected virtual IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
104      var consumerTypes = Content.GetType().GetInterfaces()
105        .Where(x => x.IsGenericType
[7548]106          && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
[7448]107
108      if (consumerTypes.Any()) {
109        var instanceTypes = consumerTypes
110          .Select(x => x.GetGenericArguments().First())
111          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
112
113        foreach (var type in instanceTypes) {
114          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
115            yield return (IProblemInstanceProvider)provider;
116        }
117      }
118    }
[7482]119
[7548]120    private void SetTooltip() {
121      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
122      if (SelectedProvider.WebLink != null)
123        toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
124      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
125    }
126
127    protected virtual string GetProviderToolTip() {
128      var provider = SelectedProvider;
129      string toolTip = provider.Name;
130
131      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
132        toolTip = toolTip
[7505]133            + Environment.NewLine + Environment.NewLine
134            + provider.ReferencePublication;
135      }
[7548]136      if (provider.WebLink != null) {
137        toolTip = toolTip
138            + Environment.NewLine
139            + provider.WebLink.ToString();
140      }
[7482]141
[7548]142      return toolTip;
[7482]143    }
[7448]144  }
145}
Note: See TracBrowser for help on using the repository browser.