Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • fixed a discovery bug
File size: 4.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.Linq;
25using System.Reflection;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Optimization;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Problems.Instances;
33
34namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views {
35  [View("ProblemView")]
36  [Content(typeof(Problem), IsDefaultView = true)]
37  public partial class ProblemView : ParameterizedNamedItemView {
38
39    public new Problem Content {
40      get { return (Problem)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public ProblemView() {
45      InitializeComponent();
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content == null) {
51        problemInstanceProviderComboBox.DataSource = null;
52      } else {
53        problemInstanceProviderComboBox.DisplayMember = "Name";
54        problemInstanceProviderComboBox.DataSource = GetProblemInstanceProviders().ToList();
55      }
56    }
57
58    protected override void SetEnabledStateOfControls() {
59      base.SetEnabledStateOfControls();
60      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null;
61    }
62
63    private void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
64      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
65        var provider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
66        var genericType = provider.GetType().GetInterfaces().Single(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IProblemInstanceProvider<>)).GetGenericArguments().First();
67        this.GetType().GetMethod("SetConsumable", BindingFlags.NonPublic | BindingFlags.Instance)
68          .MakeGenericMethod(genericType).Invoke(this, new object[] { provider, Content });
69        problemInstanceProviderViewHost.Content = provider;
70      }
71    }
72
73    /// <summary>
74    /// Do not change method name without changing the string reference above
75    /// </summary>
76    private void SetConsumable<T>(IProblemInstanceProvider<T> producer, IConsumable<T> consumer) {
77      producer.Consumer = consumer;
78    }
79
80    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
81      var comboBox = (ComboBox)sender;
82      if (comboBox.DataSource == null)
83        comboBox.Items.Clear();
84    }
85
86    protected virtual IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
87      var consumerTypes = Content.GetType().GetInterfaces()
88        .Where(x => x.IsGenericType
89          && x.GetGenericTypeDefinition() == typeof(IConsumable<>));
90
91      if (consumerTypes.Any()) {
92        var instanceTypes = consumerTypes
93          .Select(x => x.GetGenericArguments().First())
94          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
95
96        foreach (var type in instanceTypes) {
97          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
98            yield return (IProblemInstanceProvider)provider;
99        }
100      }
101    }
102
103    protected virtual string GetProviderToolTip(IProblemInstanceProvider provider) {
104      if (provider.WebLink != null) {
105        return provider.Name
106            + Environment.NewLine
107            + provider.WebLink.ToString()
108            + Environment.NewLine + Environment.NewLine
109            + provider.ReferencePublication;
110      } else {
111        return provider.Name
112            + Environment.NewLine + Environment.NewLine
113            + provider.ReferencePublication;
114      }
115    }
116
117    private void problemInstanceProviderComboBox_ToolTipRequired(object sender, ToolTipRequiredEventArgs e) {
118      e.ToolTip = GetProviderToolTip((IProblemInstanceProvider)e.Item);
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.