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

Last change on this file since 2797 was 2797, checked in by mkommend, 11 years ago

changed ViewHost to cach the currently display ViewType (ticket #95)

File size: 5.5 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using System.Linq;
30using HeuristicLab.Common;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Core.Views {
34  public partial class ViewHost : UserControl {
35    private Dictionary<Type, ToolStripMenuItem> typeMenuItemTable;
36    public IEnumerable<Type> AvailableViewTypes {
37      get { return typeMenuItemTable.Keys; }
38    }
39
40    private object content;
41    public object Content {
42      get { return content; }
43      set {
44        if (value != content) {
45          content = value;
46          Initialize();
47        }
48      }
49    }
50
51    private Type viewType;
52    public Type ViewType {
53      get { return this.viewType; }
54      set {
55        if (!ViewTypeCanShowContent(value, content))
56          throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
57                                                    value.GetPrettyName(),
58                                                    content.GetType().GetPrettyName()));
59        viewType = value;
60        ShowView();
61      }
62    }
63
64    public ViewHost() {
65      typeMenuItemTable = new Dictionary<Type, ToolStripMenuItem>();
66      viewType = null;
67      content = null;
68      InitializeComponent();
69      Initialize();
70    }
71
72    private void ShowView() {
73      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
74      viewPanel.Controls.Clear();
75
76      if (viewType == null || content == null)
77        return;
78
79      if (!ViewTypeCanShowContent(viewType, content) || !typeMenuItemTable.ContainsKey(viewType))
80        throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
81                                                  viewType.GetPrettyName(),
82                                                  Content.GetType().GetPrettyName()));
83
84      foreach (ToolStripMenuItem item in typeMenuItemTable.Values) {
85        item.Checked = false;
86        item.Enabled = true;
87      }
88      typeMenuItemTable[viewType].Checked = true;
89      typeMenuItemTable[viewType].Enabled = false;
90
91      Control view = (Control)MainFormManager.CreateView(viewType, Content);
92      viewPanel.Controls.Add(view);
93      viewPanel.Tag = view;
94      view.Dock = DockStyle.Fill;
95      viewPanel.Enabled = true;
96      viewPanel.Visible = true;
97    }
98
99    protected virtual void Initialize() {
100      viewsLabel.Enabled = false;
101      viewsLabel.Visible = false;
102      typeMenuItemTable.Clear();
103      contextMenuStrip.Items.Clear();
104      contextMenuStrip.Enabled = false;
105      messageLabel.Visible = false;
106      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
107      viewPanel.Controls.Clear();
108      viewPanel.Enabled = false;
109      viewPanel.Visible = false;
110
111      if (Content != null) {
112        var viewTypes = from t in MainFormManager.GetViewTypes(Content.GetType())
113                        orderby t.Name ascending
114                        select t;
115        foreach (Type v in viewTypes) {
116          ToolStripMenuItem item = new ToolStripMenuItem(v.GetPrettyName());
117          item.Name = v.FullName;
118          item.ToolTipText = v.GetPrettyName(true);
119          item.Tag = v;
120          contextMenuStrip.Items.Add(item);
121          typeMenuItemTable.Add(v, item);
122        }
123        if (contextMenuStrip.Items.Count == 0) {
124          messageLabel.Visible = true;
125        } else {
126          viewsLabel.Enabled = true;
127          viewsLabel.Visible = true;
128          contextMenuStrip.Enabled = true;
129          messageLabel.Visible = false;
130        }
131
132        if (!ViewTypeCanShowContent(viewType, Content)) {
133          viewType = MainFormManager.GetDefaultViewType(Content.GetType());
134          if ((viewType == null) && (contextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
135            viewType = (Type)contextMenuStrip.Items[0].Tag;
136        }
137        ShowView();
138      }
139    }
140
141    private bool ViewTypeCanShowContent(Type viewType, object content) {
142      if (content == null) // every view can display null
143        return true;
144      if (viewType == null)
145        return false;
146      return ContentAttribute.CanViewType(viewType, Content.GetType());
147    }
148
149    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
150      MainFormManager.CreateView(viewPanel.Tag.GetType(), Content).Show();
151    }
152    protected void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
153      Type viewType = (Type)e.ClickedItem.Tag;
154      ViewType = viewType;
155      ShowView();
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.