Changeset 4299


Ignore:
Timestamp:
08/24/10 14:24:30 (12 years ago)
Author:
mkommend
Message:

Removed caching from the ViewHost (ticket #1133).

File:
1 edited

Legend:

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

    r4158 r4299  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Reflection;
    2625using System.Windows.Forms;
    2726using HeuristicLab.Common;
     
    3231    public ViewHost() {
    3332      InitializeComponent();
    34       cachedViews = new Dictionary<Type, IContentView>();
    3533      startDragAndDrop = false;
    3634      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
     
    4139      messageLabel.Visible = false;
    4240      viewsLabel.Visible = false;
    43     }
    44 
    45     private Dictionary<Type, IContentView> cachedViews;
    46     public IEnumerable<IContentView> Views {
    47       get { return cachedViews.Values; }
    4841    }
    4942
     
    5649            DeregisterActiveViewEvents();
    5750            View view = activeView as View;
    58             if (view != null)
     51            if (view != null) {
    5952              view.OnHidden(EventArgs.Empty);
    60             if (ActiveViewControl != null)
    61               ActiveViewControl.Visible = false;
     53              Controls.Remove(view);
     54              view.Dispose();
     55            }
    6256          }
    6357          activeView = value;
     
    6660            RegisterActiveViewEvents();
    6761            View view = activeView as View;
    68             if (view != null)
     62            if (view != null) {
    6963              view.OnShown(new ViewShownEventArgs(view, false));
    70             if (ActiveViewControl != null) {
    71               ActiveViewControl.Visible = true;
    72               ActiveViewControl.BringToFront();
     64              Controls.Add(view);
    7365            }
    7466          } else viewType = null;
     
    9890      get { return base.Enabled; }
    9991      set {
    100         this.SuspendRepaint();
    10192        base.Enabled = value;
    10293        this.viewsLabel.Enabled = value;
    103         this.ResumeRepaint(true);
    104       }
    105     }
    106 
    107     public void ClearCache() {
    108       foreach (var cachedView in cachedViews.ToArray()) {
    109         if (cachedView.Value != activeView) {
    110           Control c = cachedView.Value as Control;
    111           if (c != null) {
    112             this.Controls.Remove(c);
    113             c.Dispose();
    114           }
    115           cachedViews.Remove(cachedView.Key);
    116         }
    11794      }
    11895    }
     
    12097    protected override void OnContentChanged() {
    12198      viewContextMenuStrip.Item = Content;
    122       //remove cached views which cannot show the content
    123       foreach (Type type in cachedViews.Keys.ToList()) {
    124         if (!ViewCanShowContent(type, Content)) {
    125           Control c = cachedViews[type] as Control;
    126           if (c != null) {
    127             this.Controls.Remove(c);
    128             c.Dispose();
    129           }
    130           cachedViews.Remove(type);
    131         }
    132       }
    133 
    13499      //change ViewType if view of ViewType can not show content or is null
    135       if (Content != null && !ViewCanShowContent(viewType, Content)) {
    136         Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
    137         if (defaultViewType != null)
    138           ViewType = defaultViewType;
    139         else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
    140           ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
    141         else
    142           ViewType = null;
    143       }
    144 
    145       foreach (IContentView view in cachedViews.Values)
    146         view.Content = this.Content;
    147 
    148       if (Content != null && viewType != null)
    149         ActiveView = cachedViews[viewType];
    150       else
    151         ActiveView = null;
    152 
     100      if (Content != null) {
     101        if (!ViewCanShowContent(viewType, Content)) {
     102          Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
     103          if (defaultViewType != null)
     104            ViewType = defaultViewType;
     105          else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
     106            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
     107          else
     108            ViewType = null;
     109        }
     110        if (ActiveView != null) {
     111          ActiveView.Content = Content;
     112          if (ActiveViewControl != null) ActiveViewControl.Visible = true;
     113        }
     114      } else if (ActiveViewControl != null)
     115        ActiveViewControl.Visible = false;
     116
     117      UpdateLabels();
     118    }
     119
     120    private void UpdateLabels() {
    153121      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
    154122        messageLabel.Visible = false;
     
    169137                                                            viewType, Content.GetType()));
    170138        IContentView view;
    171         if (!cachedViews.ContainsKey(ViewType)) {
    172           view = MainFormManager.CreateView(viewType);
    173           view.ReadOnly = this.ReadOnly;
    174           view.Locked = this.Locked;
    175           ActiveView = view; //necessary to allow the views to change the status of the viewhost
    176           view.Content = Content;
    177           cachedViews.Add(viewType, view);
    178           Control c = view as Control;
    179           if (c != null)
    180             this.Controls.Add(c);
    181         } else
    182           ActiveView = cachedViews[viewType];
     139        view = MainFormManager.CreateView(viewType);
     140        view.ReadOnly = this.ReadOnly;
     141        view.Locked = this.Locked;
     142        ActiveView = view; //necessary to allow the views to change the status of the viewhost
     143        view.Content = Content;
     144
    183145        UpdateActiveMenuItem();
    184146      }
     
    231193
    232194    #region forwarding of view events
    233     protected override void PropagateStateChanges(Control control, Type type, System.Reflection.PropertyInfo propertyInfo) {
    234       if (!type.GetProperties().Contains(propertyInfo))
    235         throw new ArgumentException("The specified type " + type + "implement the property " + propertyInfo.Name + ".");
    236       if (!type.IsAssignableFrom(this.GetType()))
    237         throw new ArgumentException("The specified type " + type + "must be the same or a base class / interface of this object.");
    238       if (!propertyInfo.CanWrite)
    239         throw new ArgumentException("The specified property " + propertyInfo.Name + " must have a setter.");
    240 
    241       if (activeView != null) {
    242         Type controlType = activeView.GetType();
    243         PropertyInfo controlPropertyInfo = controlType.GetProperty(propertyInfo.Name, propertyInfo.PropertyType);
    244         if (type.IsAssignableFrom(controlType) && controlPropertyInfo != null) {
    245           var thisValue = propertyInfo.GetValue(this, null);
    246           controlPropertyInfo.SetValue(activeView, thisValue, null);
    247         }
    248       }
    249     }
    250 
    251195    internal protected override void OnShown(ViewShownEventArgs e) {
    252196      base.OnShown(e);
     
    263207    internal protected override void OnClosing(FormClosingEventArgs e) {
    264208      base.OnClosing(e);
    265       foreach (View view in this.Views.OfType<View>())
     209      View view = ActiveView as View;
     210      if (view != null)
    266211        view.OnClosing(e);
    267212    }
    268213    internal protected override void OnClosed(FormClosedEventArgs e) {
    269214      base.OnClosed(e);
    270       foreach (View view in this.Views.OfType<View>())
     215      View view = ActiveView as View;
     216      if (view != null)
    271217        view.OnClosed(e);
    272218    }
Note: See TracChangeset for help on using the changeset viewer.