Free cookie consent management tool by TermsFeed Policy Generator

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

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

incremented plugin version HeuristicLab.MainForm and HeuristicLab.MainForm.WindowsForms to 3.3 (ticket #983)

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