Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • Added Transpose() extension method for double[,] matrices
  • Added IProblemInstanceConsumer<T> interface
  • Implemented general ProblemView which auto-detects all instances a problem can consume
  • Added ability of IProblemInstanceProvider to directly feed a consumer
  • Implemented general view for problem instance providers
  • Fixed a few bugs
File size: 3.3 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      var provider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
64      provider.SetConsumer((IProblemInstanceConsumer)Content);
65      problemInstanceProviderViewHost.Content = provider;
66    }
67
68    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
69      var comboBox = (ComboBox)sender;
70      if (comboBox.DataSource == null)
71        comboBox.Items.Clear();
72    }
73
74    protected virtual IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
75      var consumerTypes = Content.GetType().GetInterfaces()
76        .Where(x => x.IsGenericType
77          && typeof(IProblemInstanceConsumer).IsAssignableFrom(x));
78
79      if (consumerTypes.Any()) {
80        var instanceTypes = consumerTypes
81          .Select(x => x.GetGenericArguments().First())
82          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
83
84        foreach (var type in instanceTypes) {
85          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
86            yield return (IProblemInstanceProvider)provider;
87        }
88      }
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.