Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2797


Ignore:
Timestamp:
02/15/10 14:09:49 (15 years ago)
Author:
mkommend
Message:

changed ViewHost to cach the currently display ViewType (ticket #95)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core.Views/3.3/ViewHost.cs

    r2792 r2797  
    3131using HeuristicLab.MainForm;
    3232
    33 namespace HeuristicLab.Core.Views { 
     33namespace HeuristicLab.Core.Views {
    3434  public partial class ViewHost : UserControl {
    3535    private Dictionary<Type, ToolStripMenuItem> typeMenuItemTable;
    36 
    3736    public IEnumerable<Type> AvailableViewTypes {
    3837      get { return typeMenuItemTable.Keys; }
     
    5049    }
    5150
     51    private Type viewType;
     52    public Type ViewType {
     53      get { return this.viewType; }
     54      set {
     55        if (!ViewTypeCanShowContent(value, content))
     56          throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
     57                                                    value.GetPrettyName(),
     58                                                    content.GetType().GetPrettyName()));
     59        viewType = value;
     60        ShowView();
     61      }
     62    }
     63
    5264    public ViewHost() {
    5365      typeMenuItemTable = new Dictionary<Type, ToolStripMenuItem>();
     66      viewType = null;
     67      content = null;
    5468      InitializeComponent();
    5569      Initialize();
    5670    }
    5771
    58     public void ShowView(Type viewType) {
    59       if (!typeMenuItemTable.ContainsKey(viewType))
     72    private void ShowView() {
     73      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
     74      viewPanel.Controls.Clear();
     75
     76      if (viewType == null || content == null)
     77        return;
     78
     79      if (!ViewTypeCanShowContent(viewType, content) || !typeMenuItemTable.ContainsKey(viewType))
    6080        throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
    6181                                                  viewType.GetPrettyName(),
    62                                                   content.GetType().GetPrettyName()));
     82                                                  Content.GetType().GetPrettyName()));
    6383
    6484      foreach (ToolStripMenuItem item in typeMenuItemTable.Values) {
     
    6989      typeMenuItemTable[viewType].Enabled = false;
    7090
    71       if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
    72       viewPanel.Controls.Clear();
    7391      Control view = (Control)MainFormManager.CreateView(viewType, Content);
    7492      viewPanel.Controls.Add(view);
     
    112130        }
    113131
    114         Type viewType = MainFormManager.GetDefaultViewType(Content.GetType());
    115         if ((viewType == null) && (contextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
    116           viewType = (Type)contextMenuStrip.Items[0].Tag;
    117         if (viewType != null)
    118           ShowView(viewType);
     132        if (!ViewTypeCanShowContent(viewType, Content)) {
     133          viewType = MainFormManager.GetDefaultViewType(Content.GetType());
     134          if ((viewType == null) && (contextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
     135            viewType = (Type)contextMenuStrip.Items[0].Tag;
     136        }
     137        ShowView();
    119138      }
     139    }
     140
     141    private bool ViewTypeCanShowContent(Type viewType, object content) {
     142      if (content == null) // every view can display null
     143        return true;
     144      if (viewType == null)
     145        return false;
     146      return ContentAttribute.CanViewType(viewType, Content.GetType());
    120147    }
    121148
     
    125152    protected void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
    126153      Type viewType = (Type)e.ClickedItem.Tag;
    127       ShowView(viewType);
     154      ViewType = viewType;
     155      ShowView();
    128156    }
    129157  }
Note: See TracChangeset for help on using the changeset viewer.