Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 5.6 KB
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2655]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;
[2818]24using System.Linq;
[2655]25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27
[3281]28namespace HeuristicLab.MainForm.WindowsForms {
[3389]29  [Content(typeof(object))]
30  public sealed partial class ViewHost : ContentView {
31    private Dictionary<Type, IContentView> cachedViews;
[3350]32    public ViewHost() {
33      InitializeComponent();
[3389]34      cachedViews = new Dictionary<Type, IContentView>();
[3350]35      viewType = null;
[3388]36      Content = null;
[3389]37      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
[3350]38    }
[3389]39    public ViewHost(object content)
[3350]40      : this() {
[3389]41      this.Content = content;
[3350]42    }
43
[2800]44    private Type viewType;
45    public Type ViewType {
46      get { return this.viewType; }
47      set {
[2870]48        if (viewType != value) {
[3388]49          if (value != null && !ViewCanShowContent(value, Content))
[2870]50            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
[3388]51                                                      value, Content.GetType()));
[2870]52          viewType = value;
53          UpdateView();
54        }
[2800]55      }
56    }
57
[3389]58    public new object Content {
59      get { return base.Content; }
[2655]60      set {
[3389]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;
[2655]66      }
67    }
68
[3177]69    public new bool Enabled {
70      get { return base.Enabled; }
71      set {
72        this.SuspendRepaint();
73        base.Enabled = value;
[3388]74        this.viewsLabel.Enabled = value;
[3177]75        this.ResumeRepaint(true);
76      }
77    }
78
[3388]79    protected override void OnReadOnlyChanged() {
80      base.OnReadOnlyChanged();
[3389]81      foreach (IContentView view in cachedViews.Values)
[3388]82        view.ReadOnly = this.ReadOnly;
[2655]83    }
84
[3389]85    protected override void OnContentChanged() {
[3388]86      messageLabel.Visible = false;
[2687]87      viewsLabel.Visible = false;
[2870]88      viewPanel.Visible = false;
[2655]89      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
90      viewPanel.Controls.Clear();
91
[2713]92      if (Content != null) {
[3361]93        if (viewContextMenuStrip.Items.Count == 0)
[2665]94          messageLabel.Visible = true;
[3361]95        else
[2687]96          viewsLabel.Visible = true;
[2655]97
[2800]98        if (!ViewCanShowContent(viewType, Content)) {
[3389]99          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
[2838]100          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
[3389]101            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
[2797]102        }
[3389]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();
[2655]109      }
110    }
111
[3388]112
[2800]113    private void UpdateView() {
114      viewPanel.Controls.Clear();
115
[3388]116      if (viewType == null || Content == null)
[2800]117        return;
118
[3388]119      if (!ViewCanShowContent(viewType, Content))
[2870]120        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
[3281]121                                                          viewType, Content.GetType()));
[2800]122
[2838]123      UpdateActiveMenuItem();
[3389]124      IContentView view;
[3388]125      if (cachedViews.ContainsKey(ViewType))
126        view = cachedViews[ViewType];
127      else {
128        view = MainFormManager.CreateView(viewType, Content, ReadOnly);
[3389]129        cachedViews.Add(viewType, view);
[3388]130      }
131
132      Control control = (Control)view;
133      control.Dock = DockStyle.Fill;
134      viewPanel.Controls.Add(control);
[2800]135      viewPanel.Visible = true;
136    }
137
[2838]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
[2800]150    private bool ViewCanShowContent(Type viewType, object content) {
[2797]151      if (content == null) // every view can display null
152        return true;
153      if (viewType == null)
154        return false;
[2838]155      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
[2797]156    }
157
[2694]158    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
[3361]159      MainFormManager.CreateView(viewType, Content, ReadOnly).Show();
[2694]160    }
[2838]161
162    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
[2687]163      Type viewType = (Type)e.ClickedItem.Tag;
[2797]164      ViewType = viewType;
[2655]165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.