source: tags/3.3.0/HeuristicLab.MainForm.WindowsForms/3.3/ViewHost.cs @ 3866

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

corrected critical bug in release tag (ticket #972)

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(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 ViewType = defaultViewType;
117        } else {
118          foreach (Type type in cachedViews.Keys.ToList()) {
119            if (!ViewCanShowContent(type, Content))
120              cachedViews.Remove(type);
121          }
122        }
123        UpdateActiveMenuItem();
124        foreach (IContentView view in cachedViews.Values)
125          view.Content = this.Content;
126      } else {
127        messageLabel.Visible = false;
128        viewsLabel.Visible = false;
129        viewPanel.Visible = false;
130      }
131    }
132
133    private void OnViewTypeChanged() {
134      viewPanel.Controls.Clear();
135      if (viewType == null || Content == null)
136        return;
137      if (!ViewCanShowContent(viewType, Content))
138        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
139                                                          viewType, Content.GetType()));
140      if (viewPanel.Height <= 10 || viewPanel.Width <= 10) {
141        viewShown = false;
142        return;
143      }
144
145      viewShown = true;
146      UpdateActiveMenuItem();
147      IContentView view;
148      if (cachedViews.ContainsKey(ViewType))
149        view = cachedViews[ViewType];
150      else {
151        view = MainFormManager.CreateView(viewType);
152        view.ReadOnly = this.ReadOnly;
153        view.Locked = this.Locked;
154        cachedViews.Add(viewType, view);
155      }
156      this.ActiveView = view;
157      Control control = (Control)view;
158      control.Dock = DockStyle.Fill;
159      viewPanel.Controls.Add(control);
160      viewPanel.Visible = true;
161      view.Content = Content;
162    }
163
164    private void viewPanel_Resize(object sender, EventArgs e) {
165      if (!viewShown)
166        this.OnViewTypeChanged();
167    }
168
169    private void RegisterActiveViewEvents() {
170      activeView.Changed += new EventHandler(activeView_Changed);
171      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
172    }
173    private void DeregisterActiveViewEvents() {
174      activeView.Changed -= new EventHandler(activeView_Changed);
175      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
176    }
177    private void activeView_CaptionChanged(object sender, EventArgs e) {
178      this.ActiveViewChanged();
179    }
180    private void activeView_Changed(object sender, EventArgs e) {
181      this.ActiveViewChanged();
182    }
183    private void ActiveViewChanged() {
184      if (ActiveView != null) {
185        this.Caption = this.ActiveView.Caption;
186        this.Locked = this.ActiveView.Locked;
187      }
188    }
189    #region forwarding of view events
190    protected override void OnReadOnlyChanged() {
191      this.SuspendRepaint();
192      base.OnReadOnlyChanged();
193      foreach (IContentView view in cachedViews.Values)
194        view.ReadOnly = this.ReadOnly;
195      this.ResumeRepaint(true);
196    }
197    protected override void OnLockedChanged() {
198      this.SuspendRepaint();
199      base.OnLockedChanged();
200      foreach (IContentView view in cachedViews.Values)
201        view.Locked = this.Locked;
202      this.ResumeRepaint(true);
203    }
204    internal protected override void OnShown(ViewShownEventArgs e) {
205      base.OnShown(e);
206      View view = this.ActiveView as View;
207      if (view != null)
208        view.OnShown(e);
209    }
210    internal protected override void OnHidden(EventArgs e) {
211      base.OnHidden(e);
212      View view = this.ActiveView as View;
213      if (view != null)
214        view.OnHidden(e);
215    }
216    internal protected override void OnClosing(FormClosingEventArgs e) {
217      base.OnClosing(e);
218      foreach (View view in this.Views.OfType<View>())
219        view.OnClosing(e);
220    }
221    internal protected override void OnClosed(FormClosedEventArgs e) {
222      base.OnClosed(e);
223      foreach (View view in this.Views.OfType<View>())
224        view.OnClosed(e);
225    }
226    #endregion
227
228    #region GUI actions
229    private void UpdateActiveMenuItem() {
230      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
231        if (item.Key == viewType) {
232          item.Value.Checked = true;
233          item.Value.Enabled = false;
234        } else {
235          item.Value.Checked = false;
236          item.Value.Enabled = true;
237        }
238      }
239    }
240
241    private bool ViewCanShowContent(Type viewType, object content) {
242      if (content == null) // every view can display null
243        return true;
244      if (viewType == null)
245        return false;
246      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
247    }
248
249    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
250      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
251      if (view != null) {
252        view.ReadOnly = this.ReadOnly;
253        view.Locked = this.Locked;
254      }
255    }
256    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
257      Type viewType = (Type)e.ClickedItem.Tag;
258      ViewType = viewType;
259    }
260
261    private bool startDragAndDrop;
262    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
263      if (!Locked) {
264        startDragAndDrop = true;
265        viewsLabel.Capture = false;
266        viewsLabel.Focus();
267      }
268    }
269    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
270      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
271        DataObject data = new DataObject();
272        data.SetData("Type", Content.GetType());
273        data.SetData("Value", Content);
274        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
275      } else
276        startDragAndDrop = false;
277    }
278    #endregion
279  }
280}
Note: See TracBrowser for help on using the repository browser.