Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3714 was 3709, checked in by mkommend, 15 years ago

corrected collection views and ViewHost (ticket #972)

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