Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/ViewHost.cs @ 3552

Last change on this file since 3552 was 3552, checked in by swagner, 14 years ago

Fixed exceptions (#893)

File size: 9.6 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;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.MainForm.WindowsForms {
30  [Content(typeof(object))]
31  public sealed partial class ViewHost : AsynchronousContentView {
32    public ViewHost() {
33      InitializeComponent();
34      cachedViews = new Dictionary<Type, IContentView>();
35      viewType = null;
36      startDragAndDrop = false;
37      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
38      activeView = null;
39      Content = null;
40      OnContentChanged();
41    }
42    public ViewHost(IContent content)
43      : this() {
44      this.Content = content;
45    }
46
47    public ViewHost(IContentView contentView)
48      : this() {
49      this.viewType = contentView.GetType();
50      this.Content = contentView.Content;
51      this.cachedViews.Add(contentView.GetType(), contentView);
52      this.activeView = contentView;
53      this.RegisterActiveViewEvents();
54      this.OnViewTypeChanged();
55      this.ActiveViewChanged();
56    }
57
58    private Dictionary<Type, IContentView> cachedViews;
59    public IEnumerable<IContentView> Views {
60      get { return cachedViews.Values; }
61    }
62
63    private IContentView activeView;
64    public IContentView ActiveView {
65      get { return this.activeView; }
66      private set {
67        if (activeView != value) {
68          if (activeView != null) {
69            DeregisterActiveViewEvents();
70            View view = activeView as View;
71            if (view != null)
72              view.OnHidden(EventArgs.Empty);
73          }
74          activeView = value;
75          if (activeView != null) {
76            RegisterActiveViewEvents();
77            View view = activeView as View;
78            if (view != null)
79              view.OnShown(new ViewShownEventArgs(view, false));
80          }
81          ActiveViewChanged();
82          OnViewTypeChanged();
83        }
84      }
85    }
86    private Type viewType;
87    public Type ViewType {
88      get { return this.viewType; }
89      set {
90        if (viewType != value) {
91          if (value != null && !ViewCanShowContent(value, Content))
92            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
93                                                      value, Content.GetType()));
94          viewType = value;
95          OnViewTypeChanged();
96        }
97      }
98    }
99    public new IContent Content {
100      get { return base.Content; }
101      set {
102        if (value == null || this.Content == null || value.GetType() != this.Content.GetType())
103          cachedViews.Clear();
104
105        base.Content = value;
106      }
107    }
108
109    public new bool Enabled {
110      get { return base.Enabled; }
111      set {
112        this.SuspendRepaint();
113        base.Enabled = value;
114        this.viewsLabel.Enabled = value;
115        this.ResumeRepaint(true);
116      }
117    }
118
119
120
121    protected override void OnContentChanged() {
122      messageLabel.Visible = false;
123      viewsLabel.Visible = false;
124      viewPanel.Visible = false;
125      viewContextMenuStrip.Item = Content;
126     
127      if (Content != null) {
128        if (viewContextMenuStrip.Items.Count == 0)
129          messageLabel.Visible = true;
130        else {
131          viewsLabel.Visible = true;
132          viewPanel.Visible = true;
133        }
134
135
136        if (!ViewCanShowContent(viewType, Content)) {
137          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
138          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
139            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
140        }
141        UpdateActiveMenuItem();
142        foreach (IContentView view in cachedViews.Values)
143          view.Content = this.Content;
144      } else {
145        if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
146        viewPanel.Controls.Clear();
147        cachedViews.Clear();
148      }
149    }
150
151
152    private void OnViewTypeChanged() {
153      viewPanel.Controls.Clear();
154      if (viewType == null || Content == null)
155        return;
156      if (!ViewCanShowContent(viewType, Content))
157        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
158                                                          viewType, Content.GetType()));
159
160      UpdateActiveMenuItem();
161      IContentView view;
162      if (cachedViews.ContainsKey(ViewType))
163        view = cachedViews[ViewType];
164      else {
165        view = MainFormManager.CreateView(viewType);
166        view.ReadOnly = this.ReadOnly;
167        view.Locked = this.Locked;
168        cachedViews.Add(viewType, view);
169      }
170      this.ActiveView = view;
171      Control control = (Control)view;
172      control.Dock = DockStyle.Fill;
173      viewPanel.Controls.Add(control);
174      viewPanel.Visible = true;
175      view.Content = Content;
176    }
177
178
179
180    private void RegisterActiveViewEvents() {
181      activeView.Changed += new EventHandler(activeView_Changed);
182      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
183    }
184    private void DeregisterActiveViewEvents() {
185      activeView.Changed -= new EventHandler(activeView_Changed);
186      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
187    }
188    private void activeView_CaptionChanged(object sender, EventArgs e) {
189      this.ActiveViewChanged();
190    }
191    private void activeView_Changed(object sender, EventArgs e) {
192      this.ActiveViewChanged();
193    }
194    private void ActiveViewChanged() {
195      if (ActiveView != null) {
196        this.Caption = this.ActiveView.Caption;
197        this.Locked = this.ActiveView.Locked;
198      }
199    }
200
201    #region forwarding of view events
202    protected override void OnReadOnlyChanged() {
203      base.OnReadOnlyChanged();
204      foreach (IContentView view in cachedViews.Values)
205        view.ReadOnly = this.ReadOnly;
206    }
207    protected override void OnLockedChanged() {
208      base.OnLockedChanged();
209      foreach (IContentView view in cachedViews.Values)
210        view.Locked = this.Locked;
211    }
212    internal protected override void OnShown(ViewShownEventArgs e) {
213      base.OnShown(e);
214      View view = this.ActiveView as View;
215      if (view != null)
216        view.OnShown(e);
217    }
218    internal protected override void OnHidden(EventArgs e) {
219      base.OnHidden(e);
220      View view = this.ActiveView as View;
221      if (view != null)
222        view.OnHidden(e);
223    }
224    internal protected override void OnClosing(FormClosingEventArgs e) {
225      base.OnClosing(e);
226      foreach (View view in this.Views.OfType<View>())
227        view.OnClosing(e);
228    }
229    internal protected override void OnClosed(FormClosedEventArgs e) {
230      base.OnClosed(e);
231      foreach (View view in this.Views.OfType<View>())
232        view.OnClosed(e);
233    }
234    #endregion
235
236    #region GUI actions
237    private void UpdateActiveMenuItem() {
238      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
239        if (item.Key == viewType) {
240          item.Value.Checked = true;
241          item.Value.Enabled = false;
242        } else {
243          item.Value.Checked = false;
244          item.Value.Enabled = true;
245        }
246      }
247    }
248
249    private bool ViewCanShowContent(Type viewType, object content) {
250      if (content == null) // every view can display null
251        return true;
252      if (viewType == null)
253        return false;
254      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
255    }
256
257    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
258      IContentView view = MainFormManager.CreateView(viewType, Content);
259      view.ReadOnly = this.ReadOnly;
260      view.Locked = this.Locked;
261      view.Show();
262    }
263    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
264      Type viewType = (Type)e.ClickedItem.Tag;
265      ViewType = viewType;
266    }
267
268    private bool startDragAndDrop;
269    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
270      if (!Locked) {
271        startDragAndDrop = true;
272        viewsLabel.Capture = false;
273        viewsLabel.Focus();
274      }
275    }
276    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
277      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
278        DataObject data = new DataObject();
279        data.SetData("Type", Content.GetType());
280        data.SetData("Value", Content);
281        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
282      } else
283        startDragAndDrop = false;
284    }
285    #endregion
286  }
287}
Note: See TracBrowser for help on using the repository browser.