Free cookie consent management tool by TermsFeed Policy Generator

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

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

changed logic of showing new views (ticket #972)

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