Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewHost.cs @ 3281

Last change on this file since 3281 was 3281, checked in by mkommend, 13 years ago

implemented showInViewHost and moved ViewHost from Core.Views to MainForm.WindowsForm (ticket #961)

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