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

Last change on this file since 3805 was 3805, checked in by mkommend, 11 years ago

corrected issue in ViewHost (ticket #972)

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