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