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

Last change on this file since 7538 was 7538, checked in by abeham, 8 years ago

#1614

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