Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewHost.cs @ 3403

Last change on this file since 3403 was 3403, checked in by mkommend, 14 years ago

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

File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.MainForm.WindowsForms {
29  [Content(typeof(object))]
30  public sealed partial class ViewHost : AsynchronousContentView {
31    public ViewHost() {
32      InitializeComponent();
33      cachedViews = new Dictionary<Type, IContentView>();
34      viewType = null;
35      Content = null;
36      startDragAndDrop = false;
37      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
38      activeView = null;
39    }
40    public ViewHost(object content)
41      : this() {
42      this.Content = content;
43    }
44
45    public ViewHost(IContentView contentView)
46      : this() {
47      this.viewType = contentView.GetType();
48      this.Content = contentView.Content;
49      this.cachedViews.Add(contentView.GetType(), contentView);
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    }
84    private Type viewType;
85    public Type ViewType {
86      get { return this.viewType; }
87      set {
88        if (viewType != value) {
89          if (value != null && !ViewCanShowContent(value, Content))
90            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
91                                                      value, Content.GetType()));
92          viewType = value;
93          OnViewTypeChanged();
94        }
95      }
96    }
97    public new object Content {
98      get { return base.Content; }
99      set {
100        if (value == null || this.Content == null || value.GetType() != this.Content.GetType())
101          cachedViews.Clear();
102        viewContextMenuStrip.Item = value;
103        this.viewsLabel.Enabled = value != null;
104        base.Content = value;
105      }
106    }
107
108    public new bool Enabled {
109      get { return base.Enabled; }
110      set {
111        this.SuspendRepaint();
112        base.Enabled = value;
113        this.viewsLabel.Enabled = value;
114        this.ResumeRepaint(true);
115      }
116    }
117
118    protected override void OnReadOnlyChanged() {
119      base.OnReadOnlyChanged();
120      foreach (IContentView view in cachedViews.Values)
121        view.ReadOnly = this.ReadOnly;
122    }
123
124    protected override void OnContentChanged() {
125      messageLabel.Visible = false;
126      viewsLabel.Visible = false;
127      viewPanel.Visible = false;
128
129      if (Content != null) {
130        if (viewContextMenuStrip.Items.Count == 0)
131          messageLabel.Visible = true;
132        else
133          viewsLabel.Visible = true;
134
135        if (!ViewCanShowContent(viewType, Content)) {
136          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
137          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
138            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
139        }
140
141        foreach (IContentView view in cachedViews.Values)
142          view.Content = this.Content;
143      } else {
144        if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
145        viewPanel.Controls.Clear();
146        cachedViews.Clear();
147      }
148    }
149
150
151    private void OnViewTypeChanged() {
152      viewPanel.Controls.Clear();
153      if (viewType == null || Content == null)
154        return;
155      if (!ViewCanShowContent(viewType, Content))
156        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
157                                                          viewType, Content.GetType()));
158
159      UpdateActiveMenuItem();
160      IContentView view;
161      if (cachedViews.ContainsKey(ViewType))
162        view = cachedViews[ViewType];
163      else {
164        view = MainFormManager.CreateView(viewType, Content, ReadOnly);
165        cachedViews.Add(viewType, view);
166      }
167      this.ActiveView = view;
168
169      Control control = (Control)view;
170      control.Dock = DockStyle.Fill;
171      viewPanel.Controls.Add(control);
172      viewPanel.Visible = true;
173    }
174
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
227    private void UpdateActiveMenuItem() {
228      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
229        if (item.Key == viewType) {
230          item.Value.Checked = true;
231          item.Value.Enabled = false;
232        } else {
233          item.Value.Checked = false;
234          item.Value.Enabled = true;
235        }
236      }
237    }
238
239    private bool ViewCanShowContent(Type viewType, object content) {
240      if (content == null) // every view can display null
241        return true;
242      if (viewType == null)
243        return false;
244      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
245    }
246
247    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
248      MainFormManager.CreateView(viewType, Content, ReadOnly).Show();
249    }
250    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
251      Type viewType = (Type)e.ClickedItem.Tag;
252      ViewType = viewType;
253    }
254
255    private bool startDragAndDrop;
256    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
257      startDragAndDrop = true;
258      viewsLabel.Capture = false;
259    }
260    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
261      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
262        DataObject data = new DataObject();
263        data.SetData("Type", Content.GetType());
264        data.SetData("Value", Content);
265        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
266      } else
267        startDragAndDrop = false;
268    }
269    #endregion
270  }
271}
Note: See TracBrowser for help on using the repository browser.