[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[7539] | 25 | using System.Reflection;
|
---|
[7448] | 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Core.Views;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.PluginInfrastructure;
|
---|
| 32 | using HeuristicLab.Problems.Instances;
|
---|
| 33 |
|
---|
| 34 | namespace 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) {
|
---|
[7482] | 64 | if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
|
---|
| 65 | var provider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
|
---|
[7538] | 66 | var genericType = provider.GetType().GetInterfaces().Single(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IProblemInstanceProvider<>)).GetGenericArguments().First();
|
---|
[7539] | 67 | this.GetType().GetMethod("SetConsumable", BindingFlags.NonPublic | BindingFlags.Instance)
|
---|
| 68 | .MakeGenericMethod(genericType).Invoke(this, new object[] { provider, Content });
|
---|
[7482] | 69 | problemInstanceProviderViewHost.Content = provider;
|
---|
| 70 | }
|
---|
[7448] | 71 | }
|
---|
| 72 |
|
---|
[7538] | 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 |
|
---|
[7448] | 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
|
---|
[7538] | 89 | && x.GetGenericTypeDefinition() == typeof(IConsumable<>));
|
---|
[7448] | 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 | }
|
---|
[7482] | 102 |
|
---|
| 103 | protected virtual string GetProviderToolTip(IProblemInstanceProvider provider) {
|
---|
[7505] | 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 | }
|
---|
[7482] | 115 | }
|
---|
| 116 |
|
---|
| 117 | private void problemInstanceProviderComboBox_ToolTipRequired(object sender, ToolTipRequiredEventArgs e) {
|
---|
| 118 | e.ToolTip = GetProviderToolTip((IProblemInstanceProvider)e.Item);
|
---|
| 119 | }
|
---|
[7448] | 120 | }
|
---|
| 121 | }
|
---|