Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2838 was 2838, checked in by mkommend, 14 years ago

changed ViewHost to use ViewContextMenuStrip (ticket #95)

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