Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3389


Ignore:
Timestamp:
04/18/10 02:08:29 (15 years ago)
Author:
mkommend
Message:

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

Location:
trunk/sources
Files:
5 edited

Legend:

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

    r2961 r3389  
    3434    public object Content {
    3535      get { return content; }
    36       protected set {
     36      set {
    3737        if ((value != null) && (!MainFormManager.ViewCanViewObject(this, value)))
    3838          throw new ArgumentException(string.Format("View \"{0}\" cannot view object \"{1}\".", this.GetType().Name, value.GetType().Name));
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewContextMenuStrip.cs

    r2992 r3389  
    1212      InitializeComponent();
    1313      this.menuItems = new Dictionary<Type, ToolStripMenuItem>();
     14      this.ignoredViewTypes = new List<Type>();
    1415    }
    1516
     
    3031    }
    3132
     33    private List<Type> ignoredViewTypes;
     34    public IEnumerable<Type> IgnoredViewTypes {
     35      get { return this.ignoredViewTypes; }
     36      set { this.ignoredViewTypes = new List<Type>(value); RefreshMenuItems(); }
     37    }
     38
    3239    private Dictionary<Type, ToolStripMenuItem> menuItems;
    3340    public IEnumerable<KeyValuePair<Type, ToolStripMenuItem>> MenuItems {
     
    4249        ToolStripMenuItem menuItem;
    4350        IEnumerable<Type> types = MainFormManager.GetViewTypes(item.GetType(),true);
    44         foreach (Type t in types) {
     51        foreach (Type t in types.Except(IgnoredViewTypes)) {
    4552          menuItem = new ToolStripMenuItem();
    4653          menuItem.Tag = t;
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewHost.cs

    r3388 r3389  
    2727
    2828namespace HeuristicLab.MainForm.WindowsForms {
    29   public sealed partial class ViewHost : View {
    30     private Dictionary<Type, IView> cachedViews;
     29  [Content(typeof(object))]
     30  public sealed partial class ViewHost : ContentView {
     31    private Dictionary<Type, IContentView> cachedViews;
    3132    public ViewHost() {
    3233      InitializeComponent();
    33       cachedViews = new Dictionary<Type, IView>();
     34      cachedViews = new Dictionary<Type, IContentView>();
    3435      viewType = null;
    3536      Content = null;
     37      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
    3638    }
    37     public ViewHost(bool readOnly)
     39    public ViewHost(object content)
    3840      : this() {
    39       this.ReadOnly = readOnly;
     41      this.Content = content;
    4042    }
    4143
     
    5456    }
    5557
    56     private object content;
    57     public object Content {
    58       get { return this.content; }
     58    public new object Content {
     59      get { return base.Content; }
    5960      set {
    60         if (value != this.content) {
    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           this.content = value;
    66           this.OnContentChanged();
    67         }
    68 
     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;
    6966      }
    7067    }
     
    8279    protected override void OnReadOnlyChanged() {
    8380      base.OnReadOnlyChanged();
    84       foreach (IView view in cachedViews.Values)
     81      foreach (IContentView view in cachedViews.Values)
    8582        view.ReadOnly = this.ReadOnly;
    8683    }
    8784
    88     private void OnContentChanged() {
     85    protected override void OnContentChanged() {
    8986      messageLabel.Visible = false;
    9087      viewsLabel.Visible = false;
     
    10097
    10198        if (!ViewCanShowContent(viewType, Content)) {
    102           viewType = MainFormManager.GetDefaultViewType(Content.GetType());
     99          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
    103100          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
    104             viewType = (Type)viewContextMenuStrip.Items[0].Tag;
     101            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
    105102        }
    106         UpdateView();
     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();
    107109      }
    108110    }
     
    120122
    121123      UpdateActiveMenuItem();
    122       IView view;
     124      IContentView view;
    123125      if (cachedViews.ContainsKey(ViewType))
    124126        view = cachedViews[ViewType];
    125127      else {
    126128        view = MainFormManager.CreateView(viewType, Content, ReadOnly);
    127         cachedViews.Add(viewType, ((IView)view));
     129        cachedViews.Add(viewType, view);
    128130      }
    129131
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IContentView.cs

    r2961 r3389  
    2727namespace HeuristicLab.MainForm {
    2828  public interface IContentView : IView {
    29     object Content { get; }
     29    object Content { get; set; }
    3030    bool SaveEnabled { get; }
    3131  }
  • trunk/sources/HeuristicLab.MainForm/3.2/MainFormManager.cs

    r3375 r3389  
    5050        MainFormManager.mainform = mainForm;
    5151        IEnumerable<Type> types =
    52           from t in ApplicationManager.Manager.GetTypes(typeof(IView))
     52          from t in ApplicationManager.Manager.GetTypes(typeof(IContentView))
    5353          where !t.IsAbstract && !t.IsInterface && ContentAttribute.HasContentAttribute(t)
    5454          select t;
     
    102102    }
    103103
    104     public static bool ViewCanViewObject(IView view, object content) {
     104    public static bool ViewCanViewObject(IContentView view, object content) {
    105105      return ContentAttribute.CanViewType(view.GetType(), content.GetType());
    106106    }
     
    141141    }
    142142
    143     public static IView CreateDefaultView(object content) {
     143    public static IContentView CreateDefaultView(object content) {
    144144      Type t = GetDefaultViewType(content.GetType());
    145145      if (t == null)
    146146        return null;
    147147
    148       return (IView)Activator.CreateInstance(t, content);
    149     }
    150     public static IView CreateDefaultView(object content, bool readOnly) {
    151       IView view = CreateDefaultView(content);
     148      return (IContentView)Activator.CreateInstance(t, content);
     149    }
     150    public static IContentView CreateDefaultView(object content, bool readOnly) {
     151      IContentView view = CreateDefaultView(content);
    152152      if (view != null)
    153153        view.ReadOnly = readOnly;
     
    155155    }
    156156
    157     public static IView CreateView(Type viewType) {
    158       if (!typeof(IView).IsAssignableFrom(viewType))
     157    public static IContentView CreateView(Type viewType) {
     158      if (!typeof(IContentView).IsAssignableFrom(viewType))
    159159        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
    160160      if (viewType.IsGenericTypeDefinition)
    161161        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
    162162
    163       return (IView)Activator.CreateInstance(viewType);
    164     }
    165     public static IView CreateView(Type viewType, bool readOnly) {
    166       IView view = CreateView(viewType);
     163      return (IContentView)Activator.CreateInstance(viewType);
     164    }
     165    public static IContentView CreateView(Type viewType, bool readOnly) {
     166      IContentView view = CreateView(viewType);
    167167      view.ReadOnly = readOnly;
    168168      return view;
    169169    }
    170170
    171     public static IView CreateView(Type viewType, object content) {
    172       if (!typeof(IView).IsAssignableFrom(viewType))
     171    public static IContentView CreateView(Type viewType, object content) {
     172      if (!typeof(IContentView).IsAssignableFrom(viewType))
    173173        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
    174174      Type view = viewType;
     
    176176        view = TransformGenericTypeDefinition(view, content.GetType());
    177177
    178       return (IView)Activator.CreateInstance(view, content);
    179     }
    180     public static IView CreateView(Type viewType, object content, bool readOnly) {
    181       IView view = CreateView(viewType, content);
     178      return (IContentView)Activator.CreateInstance(view, content);
     179    }
     180    public static IContentView CreateView(Type viewType, object content, bool readOnly) {
     181      IContentView view = CreateView(viewType, content);
    182182      view.ReadOnly = readOnly;
    183183      return view;
Note: See TracChangeset for help on using the changeset viewer.