Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected opening of views from the ViewHost (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            RegisterActiveViewEvents();
63            View view = activeView as View;
64            if (view != null)
65              view.OnShown(new ViewShownEventArgs(view, false));
66          }
67          ActiveViewChanged();
68          OnViewTypeChanged();
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    public new IContent Content {
86      get { return base.Content; }
87      set {
88        if (value == null || this.Content == null || value.GetType() != this.Content.GetType())
89          cachedViews.Clear();
90
91        base.Content = value;
92      }
93    }
94
95    public new bool Enabled {
96      get { return base.Enabled; }
97      set {
98        this.SuspendRepaint();
99        base.Enabled = value;
100        this.viewsLabel.Enabled = value;
101        this.ResumeRepaint(true);
102      }
103    }
104
105    protected override void OnContentChanged() {
106      viewContextMenuStrip.Item = Content;
107
108      if (Content != null) {
109        if (viewContextMenuStrip.Items.Count == 0) {
110          messageLabel.Visible = true;
111          viewsLabel.Visible = false;
112          viewPanel.Visible = false;
113        } else {
114          messageLabel.Visible = false;
115          viewsLabel.Visible = true;
116          viewPanel.Visible = true;
117        }
118
119        if (!ViewCanShowContent(viewType, Content)) {
120          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
121          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
122            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
123        }
124        UpdateActiveMenuItem();
125        foreach (IContentView view in cachedViews.Values)
126          view.Content = this.Content;
127      } else {
128        messageLabel.Visible = false;
129        viewsLabel.Visible = false;
130        viewPanel.Visible = false;
131        if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
132        viewPanel.Controls.Clear();
133        cachedViews.Clear();
134      }
135    }
136
137    private void OnViewTypeChanged() {
138      viewPanel.Controls.Clear();
139      if (viewType == null || Content == null)
140        return;
141      if (!ViewCanShowContent(viewType, Content))
142        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
143                                                          viewType, Content.GetType()));
144      if (viewPanel.Height <= 10 || viewPanel.Width <= 10) {
145        viewShown = false;
146        return;
147      }
148
149      viewShown = true;
150      UpdateActiveMenuItem();
151      IContentView view;
152      if (cachedViews.ContainsKey(ViewType))
153        view = cachedViews[ViewType];
154      else {
155        view = MainFormManager.CreateView(viewType);
156        view.ReadOnly = this.ReadOnly;
157        view.Locked = this.Locked;
158        cachedViews.Add(viewType, view);
159      }
160      this.ActiveView = view;
161      Control control = (Control)view;
162      control.Dock = DockStyle.Fill;
163      viewPanel.Controls.Add(control);
164      viewPanel.Visible = true;
165      view.Content = Content;
166    }
167
168    private void viewPanel_Resize(object sender, EventArgs e) {
169      if (!viewShown)
170        this.OnViewTypeChanged();
171    }
172
173    private void RegisterActiveViewEvents() {
174      activeView.Changed += new EventHandler(activeView_Changed);
175      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
176    }
177    private void DeregisterActiveViewEvents() {
178      activeView.Changed -= new EventHandler(activeView_Changed);
179      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
180    }
181    private void activeView_CaptionChanged(object sender, EventArgs e) {
182      this.ActiveViewChanged();
183    }
184    private void activeView_Changed(object sender, EventArgs e) {
185      this.ActiveViewChanged();
186    }
187    private void ActiveViewChanged() {
188      if (ActiveView != null) {
189        this.Caption = this.ActiveView.Caption;
190        this.Locked = this.ActiveView.Locked;
191      }
192    }
193    #region forwarding of view events
194    protected override void OnReadOnlyChanged() {
195      base.OnReadOnlyChanged();
196      foreach (IContentView view in cachedViews.Values)
197        view.ReadOnly = this.ReadOnly;
198    }
199    protected override void OnLockedChanged() {
200      base.OnLockedChanged();
201      foreach (IContentView view in cachedViews.Values)
202        view.Locked = this.Locked;
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.GetMainForm<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.