Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/HeuristicOptimizationProblemView.cs @ 7603

Last change on this file since 7603 was 7558, checked in by abeham, 13 years ago

#1782: trunk integration of problem instance development

  • Adapted TSP and QAP to use the new feature
  • Moved the TSPLIB importer dialog from the TSP plugin to the TSPLIB instances plugin (created a view for that provider)
  • Created it as a default view for IHeuristicOptimizationProblem in order not to interfere with other problems do not yet work with this
File size: 5.2 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.Diagnostics;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Problems.Instances;
33
34namespace HeuristicLab.Optimization.Views {
35  [View("HeuristicOptimizationProblemView")]
36  [Content(typeof(IHeuristicOptimizationProblem), IsDefaultView = true)]
37  public partial class HeuristicOptimizationProblemView : ParameterizedNamedItemView {
38
39    public new IProblem Content {
40      get { return (IProblem)base.Content; }
41      set { base.Content = value; }
42    }
43
44    protected IProblemInstanceProvider SelectedProvider {
45      get { return (problemInstanceProviderComboBox.SelectedIndex >= 0 ? (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem : null); }
46    }
47
48    public HeuristicOptimizationProblemView() {
49      InitializeComponent();
50      libraryInfoButton.Text = String.Empty;
51      libraryInfoButton.Image = VSImageLibrary.Information;
52    }
53
54    protected override void OnContentChanged() {
55      base.OnContentChanged();
56      if (Content == null) {
57        problemInstanceProviderComboBox.DataSource = null;
58      } else {
59        problemInstanceProviderComboBox.DisplayMember = "Name";
60        problemInstanceProviderComboBox.DataSource = GetProblemInstanceProviders().ToList();
61      }
62      SetEnabledStateOfControls();
63    }
64
65    protected override void SetEnabledStateOfControls() {
66      base.SetEnabledStateOfControls();
67      problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
68      libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
69    }
70
71    protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
72      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
73        dynamic provider = SelectedProvider;
74        dynamic consumer = Content;
75        provider.Consumer = consumer;
76        problemInstanceProviderViewHost.Content = SelectedProvider;
77        SetTooltip();
78      }
79      SetEnabledStateOfControls();
80    }
81
82    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
83      var comboBox = (ComboBox)sender;
84      if (comboBox.DataSource == null)
85        comboBox.Items.Clear();
86    }
87
88    protected virtual void libraryInfoButton_Click(object sender, EventArgs e) {
89      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
90        if (SelectedProvider != null && SelectedProvider.WebLink != null)
91          Process.Start(SelectedProvider.WebLink.ToString());
92      }
93    }
94
95    protected virtual IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
96      var consumerTypes = Content.GetType().GetInterfaces()
97        .Where(x => x.IsGenericType
98          && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
99
100      if (consumerTypes.Any()) {
101        var instanceTypes = consumerTypes
102          .Select(x => x.GetGenericArguments().First())
103          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
104
105        foreach (var type in instanceTypes) {
106          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
107            yield return (IProblemInstanceProvider)provider;
108        }
109      }
110    }
111
112    protected virtual void SetTooltip() {
113      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
114      if (SelectedProvider.WebLink != null)
115        toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
116      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
117    }
118
119    protected virtual string GetProviderToolTip() {
120      var provider = SelectedProvider;
121      string toolTip = provider.Name;
122
123      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
124        toolTip = toolTip
125            + Environment.NewLine + Environment.NewLine
126            + provider.ReferencePublication;
127      }
128      if (provider.WebLink != null) {
129        toolTip = toolTip
130            + Environment.NewLine
131            + provider.WebLink.ToString();
132      }
133
134      return toolTip;
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.