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

Last change on this file since 3391 was 3391, checked in by mkommend, 12 years ago

added drag and drop in ViewHost (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
133      Control control = (Control)view;
134      control.Dock = DockStyle.Fill;
135      viewPanel.Controls.Add(control);
136      viewPanel.Visible = true;
137    }
138
139    private void UpdateActiveMenuItem() {
140      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
141        if (item.Key == viewType) {
142          item.Value.Checked = true;
143          item.Value.Enabled = false;
144        } else {
145          item.Value.Checked = false;
146          item.Value.Enabled = true;
147        }
148      }
149    }
150
151    private bool ViewCanShowContent(Type viewType, object content) {
152      if (content == null) // every view can display null
153        return true;
154      if (viewType == null)
155        return false;
156      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
157    }
158
159    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
160      MainFormManager.CreateView(viewType, Content, ReadOnly).Show();
161    }
162
163    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
164      Type viewType = (Type)e.ClickedItem.Tag;
165      ViewType = viewType;
166    }
167
168    private bool startDragAndDrop;
169    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
170      startDragAndDrop = true;
171      viewsLabel.Capture = false;
172    }
173
174    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
175      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
176        DataObject data = new DataObject();
177        data.SetData("Type", Content.GetType());
178        data.SetData("Value", Content);
179        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
180      } else
181        startDragAndDrop = false;
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.