Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/10 00:09:19 (14 years ago)
Author:
mkommend
Message:

enhanced viewHost (forwarding of view events, activeView property,...) (ticket #972)

File:
1 edited

Legend:

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

    r3398 r3403  
    2929  [Content(typeof(object))]
    3030  public sealed partial class ViewHost : AsynchronousContentView {
    31     private Dictionary<Type, IContentView> cachedViews;
    3231    public ViewHost() {
    3332      InitializeComponent();
     
    3736      startDragAndDrop = false;
    3837      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
     38      activeView = null;
    3939    }
    4040    public ViewHost(object content)
     
    4343    }
    4444
    45     public ViewHost(IContentView contentView) :this(){
     45    public ViewHost(IContentView contentView)
     46      : this() {
    4647      this.viewType = contentView.GetType();
    4748      this.Content = contentView.Content;
    4849      this.cachedViews.Add(contentView.GetType(), contentView);
    49       this.UpdateView();
    50     }
    51 
     50      this.activeView = contentView;
     51      this.RegisterActiveViewEvents();
     52      this.OnViewTypeChanged();
     53      this.ActiveViewChanged();
     54    }
     55
     56    private Dictionary<Type, IContentView> cachedViews;
     57    public IEnumerable<IContentView> Views {
     58      get { return cachedViews.Values; }
     59    }
     60
     61    private IContentView activeView;
     62    public IContentView ActiveView {
     63      get { return this.activeView; }
     64      private set {
     65        if (activeView != value) {
     66          if (activeView != null) {
     67            DeregisterActiveViewEvents();
     68            View view = activeView as View;
     69            if (view != null)
     70              view.OnHidden(EventArgs.Empty);
     71          }
     72          activeView = value;
     73          if (activeView != null) {
     74            RegisterActiveViewEvents();
     75            View view = activeView as View;
     76            if (view != null)
     77              view.OnShown(new ViewShownEventArgs(view,false));
     78          }
     79          ActiveViewChanged();
     80          OnViewTypeChanged();
     81        }
     82      }
     83    }
    5284    private Type viewType;
    5385    public Type ViewType {
     
    5991                                                      value, Content.GetType()));
    6092          viewType = value;
    61           UpdateView();
    62         }
    63       }
    64     }
    65 
     93          OnViewTypeChanged();
     94        }
     95      }
     96    }
    6697    public new object Content {
    6798      get { return base.Content; }
     
    107138            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
    108139        }
     140
    109141        foreach (IContentView view in cachedViews.Values)
    110142          view.Content = this.Content;
     
    117149
    118150
    119     private void UpdateView() {
     151    private void OnViewTypeChanged() {
    120152      viewPanel.Controls.Clear();
    121 
    122153      if (viewType == null || Content == null)
    123154        return;
    124 
    125155      if (!ViewCanShowContent(viewType, Content))
    126156        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
     
    135165        cachedViews.Add(viewType, view);
    136166      }
    137       this.Caption = view.Caption;
    138       this.SaveEnabled = view.SaveEnabled;
     167      this.ActiveView = view;
    139168
    140169      Control control = (Control)view;
     
    144173    }
    145174
     175
     176
     177    private void RegisterActiveViewEvents() {
     178      activeView.Changed += new EventHandler(activeView_Changed);
     179      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
     180    }
     181    private void DeregisterActiveViewEvents() {
     182      activeView.Changed -= new EventHandler(activeView_Changed);
     183      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
     184    }
     185    private void activeView_CaptionChanged(object sender, EventArgs e) {
     186      this.ActiveViewChanged();
     187    }
     188    private void activeView_Changed(object sender, EventArgs e) {
     189      this.ActiveViewChanged();
     190    }
     191    private void ActiveViewChanged() {
     192      if (ActiveView != null) {
     193        this.Caption = this.ActiveView.Caption;
     194        this.SaveEnabled = this.ActiveView.SaveEnabled;
     195      }
     196    }
     197
     198    #region forwarding of view events
     199    internal protected override void OnShown(ViewShownEventArgs e) {
     200      base.OnShown(e);
     201      View view = this.ActiveView as View;
     202      if (view != null)
     203        view.OnShown(e);
     204    }
     205
     206    internal protected override void OnHidden(EventArgs e) {
     207      base.OnHidden(e);
     208      View view = this.ActiveView as View;
     209      if (view != null)
     210        view.OnHidden(e);
     211    }
     212
     213    internal protected override void OnClosing(FormClosingEventArgs e) {
     214      base.OnClosing(e);
     215      foreach (View view in this.Views.OfType<View>())
     216        view.OnClosing(e);
     217    }
     218
     219    internal protected override void OnClosed(FormClosedEventArgs e) {
     220      base.OnClosed(e);
     221      foreach (View view in this.Views.OfType<View>())
     222        view.OnClosed(e);
     223    }
     224    #endregion
     225
     226    #region GUI actions
    146227    private void UpdateActiveMenuItem() {
    147228      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
     
    167248      MainFormManager.CreateView(viewType, Content, ReadOnly).Show();
    168249    }
    169 
    170250    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
    171251      Type viewType = (Type)e.ClickedItem.Tag;
     
    178258      viewsLabel.Capture = false;
    179259    }
    180 
    181260    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
    182261      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
     
    188267        startDragAndDrop = false;
    189268    }
     269    #endregion
    190270  }
    191271}
Note: See TracChangeset for help on using the changeset viewer.