Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewHost.cs @ 3394

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

adapted mainforms to have a ShowInViewHost property (ticket #972)

File size: 6.3 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;
27
28namespace HeuristicLab.MainForm.WindowsForms {
29  [Content(typeof(object))]
30  public sealed partial class ViewHost : ContentView {
31    private Dictionary<Type, IContentView> cachedViews;
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    }
40    public ViewHost(object content)
41      : this() {
42      this.Content = content;
43    }
44
45    private Type viewType;
46    public Type ViewType {
47      get { return this.viewType; }
48      set {
49        if (viewType != value) {
50          if (value != null && !ViewCanShowContent(value, Content))
51            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
52                                                      value, Content.GetType()));
53          viewType = value;
54          UpdateView();
55        }
56      }
57    }
58
59    public new object Content {
60      get { return base.Content; }
61      set {
62        if (value == null || this.Content == null || value.GetType() != this.Content.GetType())
63          cachedViews.Clear();
64        viewContextMenuStrip.Item = value;
65        this.viewsLabel.Enabled = value != null;
66        base.Content = value;
67      }
68    }
69
70    public new bool Enabled {
71      get { return base.Enabled; }
72      set {
73        this.SuspendRepaint();
74        base.Enabled = value;
75        this.viewsLabel.Enabled = value;
76        this.ResumeRepaint(true);
77      }
78    }
79
80    protected override void OnReadOnlyChanged() {
81      base.OnReadOnlyChanged();
82      foreach (IContentView view in cachedViews.Values)
83        view.ReadOnly = this.ReadOnly;
84    }
85
86    protected override void OnContentChanged() {
87      messageLabel.Visible = false;
88      viewsLabel.Visible = false;
89      viewPanel.Visible = false;
90      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
91      viewPanel.Controls.Clear();
92
93      if (Content != null) {
94        if (viewContextMenuStrip.Items.Count == 0)
95          messageLabel.Visible = true;
96        else
97          viewsLabel.Visible = true;
98
99        if (!ViewCanShowContent(viewType, Content)) {
100          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
101          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
102            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
103        }
104        foreach (IContentView view in cachedViews.Values)
105          view.Content = this.Content;
106      } else {
107        if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
108        viewPanel.Controls.Clear();
109        cachedViews.Clear();
110      }
111    }
112
113
114    private void UpdateView() {
115      viewPanel.Controls.Clear();
116
117      if (viewType == null || Content == null)
118        return;
119
120      if (!ViewCanShowContent(viewType, Content))
121        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
122                                                          viewType, Content.GetType()));
123
124      UpdateActiveMenuItem();
125      IContentView view;
126      if (cachedViews.ContainsKey(ViewType))
127        view = cachedViews[ViewType];
128      else {
129        view = MainFormManager.CreateView(viewType, Content, ReadOnly);
130        cachedViews.Add(viewType, view);
131      }
132      this.Caption = view.Caption;
133      this.SaveEnabled = view.SaveEnabled;
134
135      Control control = (Control)view;
136      control.Dock = DockStyle.Fill;
137      viewPanel.Controls.Add(control);
138      viewPanel.Visible = true;
139    }
140
141    private void UpdateActiveMenuItem() {
142      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
143        if (item.Key == viewType) {
144          item.Value.Checked = true;
145          item.Value.Enabled = false;
146        } else {
147          item.Value.Checked = false;
148          item.Value.Enabled = true;
149        }
150      }
151    }
152
153    private bool ViewCanShowContent(Type viewType, object content) {
154      if (content == null) // every view can display null
155        return true;
156      if (viewType == null)
157        return false;
158      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
159    }
160
161    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
162      MainFormManager.CreateView(viewType, Content, ReadOnly).Show();
163    }
164
165    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
166      Type viewType = (Type)e.ClickedItem.Tag;
167      ViewType = viewType;
168    }
169
170    private bool startDragAndDrop;
171    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
172      startDragAndDrop = true;
173      viewsLabel.Capture = false;
174    }
175
176    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
177      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
178        DataObject data = new DataObject();
179        data.SetData("Type", Content.GetType());
180        data.SetData("Value", Content);
181        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
182      } else
183        startDragAndDrop = false;
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.