Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Views/3.3/ProblemView.cs @ 11065

Last change on this file since 11065 was 10021, checked in by abeham, 11 years ago

#2110: merged to stable

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.Problems.Instances;
28using HeuristicLab.Problems.Instances.Views;
29
30namespace HeuristicLab.Optimization.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [View("Problem View")]
35  [Content(typeof(IProblem), true)]
36  public partial class ProblemView : ParameterizedNamedItemView {
37
38    public new IProblem Content {
39      get { return (IProblem)base.Content; }
40      set { base.Content = value; }
41    }
42
43    protected IEnumerable<IProblemInstanceProvider> problemInstanceProviders;
44    public IEnumerable<IProblemInstanceProvider> ProblemInstanceProviders {
45      get { return new List<IProblemInstanceProvider>(problemInstanceProviders); }
46    }
47
48    public IProblemInstanceProvider SelectedProvider { get; protected set; }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="ItemBaseView"/>.
52    /// </summary>
53    public ProblemView() {
54      InitializeComponent();
55    }
56
57    protected override void OnContentChanged() {
58      base.OnContentChanged();
59      if (Content == null) {
60        problemInstanceProviders = null;
61        problemInstanceProviderComboBox.DataSource = null;
62        problemInstanceSplitContainer.Panel1Collapsed = true;
63      } else {
64        var consumer = Content as IProblemInstanceConsumer;
65        if (consumer != null) {
66          problemInstanceProviders = ProblemInstanceManager.GetProviders(Content);
67          bool expand = problemInstanceProviders.Any();
68          if (expand) {
69            problemInstanceProviderComboBox.DisplayMember = "Name";
70            problemInstanceProviderComboBox.DataSource = ProblemInstanceProviders.OrderBy(x => x.Name).ToList();
71          }
72          problemInstanceSplitContainer.Panel1Collapsed = !expand;
73        } else
74          problemInstanceSplitContainer.Panel1Collapsed = true;
75      }
76      SetEnabledStateOfControls();
77    }
78
79    protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
80      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
81        SelectedProvider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
82        problemInstanceProviderViewHost.Content = SelectedProvider;
83        var view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
84        var consumer = (IProblemInstanceConsumer)Content;
85        view.Consumer = consumer;
86        if (CheckForIProblemInstanceExporter(consumer))
87          view.Exporter = (IProblemInstanceExporter)Content;
88        else view.Exporter = null;
89        SetTooltip();
90      } else {
91        SelectedProvider = null;
92      }
93      SetEnabledStateOfControls();
94    }
95
96    protected bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
97      return Content.GetType().GetInterfaces()
98                    .Any(x => x == typeof(IProblemInstanceExporter));
99    }
100
101    #region ToolTip
102    protected void SetTooltip() {
103      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
104    }
105
106    private string GetProviderToolTip() {
107      var provider = SelectedProvider;
108      string toolTip = provider.Name;
109
110      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
111        toolTip = toolTip
112            + Environment.NewLine + Environment.NewLine
113            + provider.ReferencePublication;
114      }
115      if (provider.WebLink != null) {
116        toolTip = toolTip
117            + Environment.NewLine
118            + provider.WebLink.ToString();
119      }
120
121      return toolTip;
122    }
123    #endregion
124  }
125}
Note: See TracBrowser for help on using the repository browser.