Free cookie consent management tool by TermsFeed Policy Generator

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

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

added dispose call before removing views from the view collection (ticket #972)

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