Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected opening of views from the RunCollectionView (ticket #893)

File size: 9.4 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        }
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
85    public new bool Enabled {
86      get { return base.Enabled; }
87      set {
88        this.SuspendRepaint();
89        base.Enabled = value;
90        this.viewsLabel.Enabled = value;
91        this.ResumeRepaint(true);
92      }
93    }
94
95    protected override void OnContentChanged() {
96      viewContextMenuStrip.Item = Content;
97
98      if (Content != null) {
99        if (viewContextMenuStrip.Items.Count == 0) {
100          messageLabel.Visible = true;
101          viewsLabel.Visible = false;
102          viewPanel.Visible = false;
103        } else {
104          messageLabel.Visible = false;
105          viewsLabel.Visible = true;
106          viewPanel.Visible = true;
107        }
108
109        Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
110        if (!ViewCanShowContent(viewType, Content)) {
111          cachedViews.Clear();
112          if (defaultViewType == null) {
113            if (viewContextMenuStrip.Items.Count > 0)  // create first available view if default view is not available
114              ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
115          } else if (defaultViewType != this.ViewType)
116            ViewType = defaultViewType;
117        }
118        UpdateActiveMenuItem();
119        foreach (IContentView view in cachedViews.Values)
120          view.Content = this.Content;
121      } else {
122        messageLabel.Visible = false;
123        viewsLabel.Visible = false;
124        viewPanel.Visible = false;
125      }
126    }
127
128    private void OnViewTypeChanged() {
129      viewPanel.Controls.Clear();
130      if (viewType == null || Content == null)
131        return;
132      if (!ViewCanShowContent(viewType, Content))
133        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
134                                                          viewType, Content.GetType()));
135      if (viewPanel.Height <= 10 || viewPanel.Width <= 10) {
136        viewShown = false;
137        return;
138      }
139
140      viewShown = true;
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 viewPanel_Resize(object sender, EventArgs e) {
160      if (!viewShown)
161        this.OnViewTypeChanged();
162    }
163
164    private void RegisterActiveViewEvents() {
165      activeView.Changed += new EventHandler(activeView_Changed);
166      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
167    }
168    private void DeregisterActiveViewEvents() {
169      activeView.Changed -= new EventHandler(activeView_Changed);
170      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
171    }
172    private void activeView_CaptionChanged(object sender, EventArgs e) {
173      this.ActiveViewChanged();
174    }
175    private void activeView_Changed(object sender, EventArgs e) {
176      this.ActiveViewChanged();
177    }
178    private void ActiveViewChanged() {
179      if (ActiveView != null) {
180        this.Caption = this.ActiveView.Caption;
181        this.Locked = this.ActiveView.Locked;
182      }
183    }
184    #region forwarding of view events
185    protected override void OnReadOnlyChanged() {
186      this.SuspendRepaint();
187      base.OnReadOnlyChanged();
188      foreach (IContentView view in cachedViews.Values)
189        view.ReadOnly = this.ReadOnly;
190      this.ResumeRepaint(true);
191    }
192    protected override void OnLockedChanged() {
193      this.SuspendRepaint();
194      base.OnLockedChanged();
195      foreach (IContentView view in cachedViews.Values)
196        view.Locked = this.Locked;
197      this.ResumeRepaint(true);
198    }
199    internal protected override void OnShown(ViewShownEventArgs e) {
200      base.OnShown(e);
201      View view = this.ActiveView as View;
202      if (view != null)
203        view.OnShown(e);
204    }
205    internal protected override void OnHidden(EventArgs e) {
206      base.OnHidden(e);
207      View view = this.ActiveView as View;
208      if (view != null)
209        view.OnHidden(e);
210    }
211    internal protected override void OnClosing(FormClosingEventArgs e) {
212      base.OnClosing(e);
213      foreach (View view in this.Views.OfType<View>())
214        view.OnClosing(e);
215    }
216    internal protected override void OnClosed(FormClosedEventArgs e) {
217      base.OnClosed(e);
218      foreach (View view in this.Views.OfType<View>())
219        view.OnClosed(e);
220    }
221    #endregion
222
223    #region GUI actions
224    private void UpdateActiveMenuItem() {
225      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
226        if (item.Key == viewType) {
227          item.Value.Checked = true;
228          item.Value.Enabled = false;
229        } else {
230          item.Value.Checked = false;
231          item.Value.Enabled = true;
232        }
233      }
234    }
235
236    private bool ViewCanShowContent(Type viewType, object content) {
237      if (content == null) // every view can display null
238        return true;
239      if (viewType == null)
240        return false;
241      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
242    }
243
244    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
245      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
246      if (view != null) {
247        view.ReadOnly = this.ReadOnly;
248        view.Locked = this.Locked;
249      }
250    }
251    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
252      Type viewType = (Type)e.ClickedItem.Tag;
253      ViewType = viewType;
254    }
255
256    private bool startDragAndDrop;
257    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
258      if (!Locked) {
259        startDragAndDrop = true;
260        viewsLabel.Capture = false;
261        viewsLabel.Focus();
262      }
263    }
264    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
265      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
266        DataObject data = new DataObject();
267        data.SetData("Type", Content.GetType());
268        data.SetData("Value", Content);
269        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
270      } else
271        startDragAndDrop = false;
272    }
273    #endregion
274  }
275}
Note: See TracBrowser for help on using the repository browser.