Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3188 was 3177, checked in by swagner, 15 years ago

Moved extension methods of Control from HeuristicLab.Core.Views to HeuristicLab.MainForm.WindowsForms and suspended/resumed repainting in some controls to improve UI response times (#887).

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