Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3539 was 3497, checked in by mkommend, 15 years ago

corrected small bug fixes 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(object))]
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    public ViewHost(IContent content)
43      : this() {
44      this.Content = content;
45    }
46
47    public ViewHost(IContentView contentView)
48      : this() {
49      this.viewType = contentView.GetType();
50      this.Content = contentView.Content;
51      this.cachedViews.Add(contentView.GetType(), contentView);
52      this.activeView = contentView;
53      this.RegisterActiveViewEvents();
54      this.OnViewTypeChanged();
55      this.ActiveViewChanged();
56    }
57
58    private Dictionary<Type, IContentView> cachedViews;
59    public IEnumerable<IContentView> Views {
60      get { return cachedViews.Values; }
61    }
62
63    private IContentView activeView;
64    public IContentView ActiveView {
65      get { return this.activeView; }
66      private set {
67        if (activeView != value) {
68          if (activeView != null) {
69            DeregisterActiveViewEvents();
70            View view = activeView as View;
71            if (view != null)
72              view.OnHidden(EventArgs.Empty);
73          }
74          activeView = value;
75          if (activeView != null) {
76            RegisterActiveViewEvents();
77            View view = activeView as View;
78            if (view != null)
79              view.OnShown(new ViewShownEventArgs(view, false));
80          }
81          ActiveViewChanged();
82          OnViewTypeChanged();
83        }
84      }
85    }
86    private Type viewType;
87    public Type ViewType {
88      get { return this.viewType; }
89      set {
90        if (viewType != value) {
91          if (value != null && !ViewCanShowContent(value, Content))
92            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
93                                                      value, Content.GetType()));
94          viewType = value;
95          OnViewTypeChanged();
96        }
97      }
98    }
99    public new IContent Content {
100      get { return base.Content; }
101      set {
102        if (value == null || this.Content == null || value.GetType() != this.Content.GetType())
103          cachedViews.Clear();
104
105        base.Content = value;
106      }
107    }
108
109    public new bool Enabled {
110      get { return base.Enabled; }
111      set {
112        this.SuspendRepaint();
113        base.Enabled = value;
114        this.viewsLabel.Enabled = value;
115        this.ResumeRepaint(true);
116      }
117    }
118
119
120
121    protected override void OnContentChanged() {
122      messageLabel.Visible = false;
123      viewsLabel.Visible = false;
124      viewPanel.Visible = false;
125      viewContextMenuStrip.Item = Content;
126     
127      if (Content != null) {
128        if (viewContextMenuStrip.Items.Count == 0)
129          messageLabel.Visible = true;
130        else {
131          viewsLabel.Visible = true;
132          viewPanel.Visible = true;
133        }
134
135
136        if (!ViewCanShowContent(viewType, Content)) {
137          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
138          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
139            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
140        }
141        UpdateActiveMenuItem();
142        foreach (IContentView view in cachedViews.Values)
143          view.Content = this.Content;
144      } else {
145        if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
146        viewPanel.Controls.Clear();
147        cachedViews.Clear();
148      }
149    }
150
151
152    private void OnViewTypeChanged() {
153      viewPanel.Controls.Clear();
154      if (viewType == null || Content == null)
155        return;
156      if (!ViewCanShowContent(viewType, Content))
157        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
158                                                          viewType, Content.GetType()));
159
160      UpdateActiveMenuItem();
161      IContentView view;
162      if (cachedViews.ContainsKey(ViewType))
163        view = cachedViews[ViewType];
164      else {
165        view = MainFormManager.CreateView(viewType, Content);
166        view.ReadOnly = this.ReadOnly;
167        view.Locked = this.Locked;
168        cachedViews.Add(viewType, view);
169      }
170      this.ActiveView = view;
171      Control control = (Control)view;
172      control.Dock = DockStyle.Fill;
173      viewPanel.Controls.Add(control);
174      viewPanel.Visible = true;
175    }
176
177
178
179    private void RegisterActiveViewEvents() {
180      activeView.Changed += new EventHandler(activeView_Changed);
181      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
182    }
183    private void DeregisterActiveViewEvents() {
184      activeView.Changed -= new EventHandler(activeView_Changed);
185      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
186    }
187    private void activeView_CaptionChanged(object sender, EventArgs e) {
188      this.ActiveViewChanged();
189    }
190    private void activeView_Changed(object sender, EventArgs e) {
191      this.ActiveViewChanged();
192    }
193    private void ActiveViewChanged() {
194      if (ActiveView != null) {
195        this.Caption = this.ActiveView.Caption;
196        this.Locked = this.ActiveView.Locked;
197      }
198    }
199
200    #region forwarding of view events
201    protected override void OnReadOnlyChanged() {
202      base.OnReadOnlyChanged();
203      foreach (IContentView view in cachedViews.Values)
204        view.ReadOnly = this.ReadOnly;
205    }
206    protected override void OnLockedChanged() {
207      base.OnLockedChanged();
208      foreach (IContentView view in cachedViews.Values)
209        view.Locked = this.Locked;
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.CreateView(viewType, Content);
258      view.ReadOnly = this.ReadOnly;
259      view.Locked = this.Locked;
260      view.Show();
261    }
262    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
263      Type viewType = (Type)e.ClickedItem.Tag;
264      ViewType = viewType;
265    }
266
267    private bool startDragAndDrop;
268    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
269      if (!Locked) {
270        startDragAndDrop = true;
271        viewsLabel.Capture = false;
272        viewsLabel.Focus();
273      }
274    }
275    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
276      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
277        DataObject data = new DataObject();
278        data.SetData("Type", Content.GetType());
279        data.SetData("Value", Content);
280        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
281      } else
282        startDragAndDrop = false;
283    }
284    #endregion
285  }
286}
Note: See TracBrowser for help on using the repository browser.