Changeset 3388


Ignore:
Timestamp:
04/18/10 01:14:00 (12 years ago)
Author:
mkommend
Message:

added caching in ViewHost (ticket #972)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewHost.cs

    r3361 r3388  
    2727
    2828namespace HeuristicLab.MainForm.WindowsForms {
    29   public sealed partial class ViewHost : UserControl {
     29  public sealed partial class ViewHost : View {
    3030    private Dictionary<Type, IView> cachedViews;
    3131    public ViewHost() {
    3232      InitializeComponent();
     33      cachedViews = new Dictionary<Type, IView>();
    3334      viewType = null;
    34       content = null;
    35       cachedViews = new Dictionary<Type, IView>();
    36       Initialize();
     35      Content = null;
    3736    }
    3837    public ViewHost(bool readOnly)
     
    4645      set {
    4746        if (viewType != value) {
    48           if (value != null && !ViewCanShowContent(value, content))
     47          if (value != null && !ViewCanShowContent(value, Content))
    4948            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
    50                                                       value, content.GetType()));
     49                                                      value, Content.GetType()));
    5150          viewType = value;
    5251          UpdateView();
     
    5756    private object content;
    5857    public object Content {
    59       get { return content; }
     58      get { return this.content; }
    6059      set {
    61         if (value != content) {
    62           content = value;
    63           viewContextMenuStrip.Item = content;
    64           cachedViews.Clear();
     60        if (value != this.content) {
     61          if (value == null || this.content == null || value.GetType() != this.content.GetType())
     62            cachedViews.Clear();
     63          viewContextMenuStrip.Item = value;
    6564          this.viewsLabel.Enabled = value != null;
    66           Initialize();
     65          this.content = value;
     66          this.OnContentChanged();
    6767        }
     68
    6869      }
    6970    }
     
    7475        this.SuspendRepaint();
    7576        base.Enabled = value;
    76         if (Content != null && value)
    77           this.viewsLabel.Enabled = value;
     77        this.viewsLabel.Enabled = value;
    7878        this.ResumeRepaint(true);
    7979      }
    8080    }
    81     private bool readOnly;
    82     public bool ReadOnly {
    83       get { return this.readOnly; }
    84       set {
    85         if (InvokeRequired) {
    86           Action<bool> action = delegate(bool b) { this.ReadOnly = b; };
    87           Invoke(action, value);
    88         } else {
    89           if (value != readOnly) {
    90             readOnly = value;
    91             OnReadOnlyChanged();
    92           }
    93         }
    94       }
     81
     82    protected override void OnReadOnlyChanged() {
     83      base.OnReadOnlyChanged();
     84      foreach (IView view in cachedViews.Values)
     85        view.ReadOnly = this.ReadOnly;
    9586    }
    9687
    97     public event EventHandler ReadOnlyChanged;
    98     private void OnReadOnlyChanged() {
    99       if (InvokeRequired)
    100         Invoke((MethodInvoker)OnReadOnlyChanged);
    101       else {
    102         EventHandler handler = ReadOnlyChanged;
    103         if (handler != null)
    104           handler(this, EventArgs.Empty);
    105         foreach (IView view in cachedViews.Values)
    106           view.ReadOnly = this.readOnly;
    107       }
    108     }
    109 
    110     private void Initialize() {
     88    private void OnContentChanged() {
     89      messageLabel.Visible = false;
    11190      viewsLabel.Visible = false;
    11291      viewPanel.Visible = false;
     
    129108    }
    130109
     110
    131111    private void UpdateView() {
    132       if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
    133112      viewPanel.Controls.Clear();
    134113
    135       if (viewType == null || content == null)
     114      if (viewType == null || Content == null)
    136115        return;
    137116
    138       if (!ViewCanShowContent(viewType, content))
     117      if (!ViewCanShowContent(viewType, Content))
    139118        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
    140119                                                          viewType, Content.GetType()));
    141120
    142121      UpdateActiveMenuItem();
    143       cachedViews.Clear();
    144       Control view = (Control)MainFormManager.CreateView(viewType, Content, ReadOnly);
    145       cachedViews.Add(viewType, ((IView)view));
    146       view.Dock = DockStyle.Fill;
    147       viewPanel.Controls.Add(view);
     122      IView view;
     123      if (cachedViews.ContainsKey(ViewType))
     124        view = cachedViews[ViewType];
     125      else {
     126        view = MainFormManager.CreateView(viewType, Content, ReadOnly);
     127        cachedViews.Add(viewType, ((IView)view));
     128      }
     129
     130      Control control = (Control)view;
     131      control.Dock = DockStyle.Fill;
     132      viewPanel.Controls.Add(control);
    148133      viewPanel.Visible = true;
    149134    }
Note: See TracChangeset for help on using the changeset viewer.