Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/ProblemView.cs @ 10000

Last change on this file since 10000 was 10000, checked in by jkarder, 11 years ago

#2110: refactored ProblemInstanceProviderViews

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        }
74      }
75      SetEnabledStateOfControls();
76    }
77
78    protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
79      if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
80        SelectedProvider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
81        problemInstanceProviderViewHost.Content = SelectedProvider;
82        var view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
83        var consumer = (IProblemInstanceConsumer)Content;
84        view.Consumer = consumer;
85        if (CheckForIProblemInstanceExporter(consumer))
86          view.Exporter = (IProblemInstanceExporter)Content;
87        else view.Exporter = null;
88        SetTooltip();
89      } else {
90        SelectedProvider = null;
91      }
92      SetEnabledStateOfControls();
93    }
94
95    protected bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
96      return Content.GetType().GetInterfaces()
97                    .Any(x => x == typeof(IProblemInstanceExporter));
98    }
99
100    #region ToolTip
101    protected void SetTooltip() {
102      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
103    }
104
105    private string GetProviderToolTip() {
106      var provider = SelectedProvider;
107      string toolTip = provider.Name;
108
109      if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
110        toolTip = toolTip
111            + Environment.NewLine + Environment.NewLine
112            + provider.ReferencePublication;
113      }
114      if (provider.WebLink != null) {
115        toolTip = toolTip
116            + Environment.NewLine
117            + provider.WebLink.ToString();
118      }
119
120      return toolTip;
121    }
122    #endregion
123  }
124}
Note: See TracBrowser for help on using the repository browser.