Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3491 was 3471, checked in by mkommend, 15 years ago

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