Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ViewHost.cs @ 3096

Last change on this file since 3096 was 2870, checked in by swagner, 15 years ago

Added some of the changes suggested by abeham to improve UI reaction times (#887)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Common;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Core.Views {
30  public sealed partial class ViewHost : UserControl {
31    private Type viewType;
32    public Type ViewType {
33      get { return this.viewType; }
34      set {
35        if (viewType != value) {
36          if (value != null && !ViewCanShowContent(value, content))
37            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
38                                                      value.GetPrettyName(),
39                                                      content.GetType().GetPrettyName()));
40          viewType = value;
41          UpdateView();
42        }
43      }
44    }
45
46    private object content;
47    public object Content {
48      get { return content; }
49      set {
50        if (value != content) {
51          content = value;
52          viewContextMenuStrip.Item = content;
53          Initialize();
54        }
55      }
56    }
57
58    public ViewHost() {
59      InitializeComponent();
60      viewType = null;
61      content = null;
62      Initialize();
63    }
64
65    private void Initialize() {
66      viewsLabel.Visible = false;
67      viewContextMenuStrip.Enabled = false;
68      messageLabel.Visible = false;
69
70      viewPanel.Visible = false;
71      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
72      viewPanel.Controls.Clear();
73
74      if (Content != null) {
75        if (viewContextMenuStrip.Items.Count == 0) {
76          messageLabel.Visible = true;
77        } else {
78          viewsLabel.Visible = true;
79          viewContextMenuStrip.Enabled = true;
80        }
81
82        if (!ViewCanShowContent(viewType, Content)) {
83          viewType = MainFormManager.GetDefaultViewType(Content.GetType());
84          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
85            viewType = (Type)viewContextMenuStrip.Items[0].Tag;
86        }
87        UpdateView();
88      }
89    }
90
91    private void UpdateView() {
92      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
93      viewPanel.Controls.Clear();
94
95      if (viewType == null || content == null)
96        return;
97
98      if (!ViewCanShowContent(viewType, content))
99        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
100                                                          viewType.GetPrettyName(),
101                                                          Content.GetType().GetPrettyName()));
102
103      UpdateActiveMenuItem();
104      Control view = (Control)MainFormManager.CreateView(viewType, Content);
105      viewPanel.Tag = view;
106      view.Dock = DockStyle.Fill;
107      viewPanel.Controls.Add(view);
108      viewPanel.Visible = true;
109    }
110
111    private void UpdateActiveMenuItem() {
112      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
113        if (item.Key == viewType) {
114          item.Value.Checked = true;
115          item.Value.Enabled = false;
116        } else {
117          item.Value.Checked = false;
118          item.Value.Enabled = true;
119        }
120      }
121    }
122
123    private bool ViewCanShowContent(Type viewType, object content) {
124      if (content == null) // every view can display null
125        return true;
126      if (viewType == null)
127        return false;
128      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
129    }
130
131    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
132      MainFormManager.CreateView(viewType, Content).Show();
133    }
134
135    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
136      Type viewType = (Type)e.ClickedItem.Tag;
137      ViewType = viewType;
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.