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

Last change on this file since 3389 was 3389, checked in by mkommend, 12 years ago

enhanced caching of views in ViewHost and adapted MainFormManager to use IContentView (ticket #972)

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