[2655] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2655] | 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;
|
---|
[2818] | 24 | using System.Linq;
|
---|
[2655] | 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
[3416] | 27 | using HeuristicLab.Common;
|
---|
[2655] | 28 |
|
---|
[3281] | 29 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
[3389] | 30 | [Content(typeof(object))]
|
---|
[3398] | 31 | public sealed partial class ViewHost : AsynchronousContentView {
|
---|
[3350] | 32 | public ViewHost() {
|
---|
| 33 | InitializeComponent();
|
---|
[3389] | 34 | cachedViews = new Dictionary<Type, IContentView>();
|
---|
[3350] | 35 | viewType = null;
|
---|
[3388] | 36 | Content = null;
|
---|
[3391] | 37 | startDragAndDrop = false;
|
---|
[3389] | 38 | viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
|
---|
[3403] | 39 | activeView = null;
|
---|
[3350] | 40 | }
|
---|
[3416] | 41 | public ViewHost(IContent content)
|
---|
[3350] | 42 | : this() {
|
---|
[3389] | 43 | this.Content = content;
|
---|
[3350] | 44 | }
|
---|
| 45 |
|
---|
[3403] | 46 | public ViewHost(IContentView contentView)
|
---|
| 47 | : this() {
|
---|
[3398] | 48 | this.viewType = contentView.GetType();
|
---|
| 49 | this.Content = contentView.Content;
|
---|
| 50 | this.cachedViews.Add(contentView.GetType(), contentView);
|
---|
[3403] | 51 | this.activeView = contentView;
|
---|
| 52 | this.RegisterActiveViewEvents();
|
---|
| 53 | this.OnViewTypeChanged();
|
---|
| 54 | this.ActiveViewChanged();
|
---|
[3398] | 55 | }
|
---|
| 56 |
|
---|
[3403] | 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)
|
---|
[3416] | 78 | view.OnShown(new ViewShownEventArgs(view, false));
|
---|
[3403] | 79 | }
|
---|
| 80 | ActiveViewChanged();
|
---|
| 81 | OnViewTypeChanged();
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
[2800] | 85 | private Type viewType;
|
---|
| 86 | public Type ViewType {
|
---|
| 87 | get { return this.viewType; }
|
---|
| 88 | set {
|
---|
[2870] | 89 | if (viewType != value) {
|
---|
[3388] | 90 | if (value != null && !ViewCanShowContent(value, Content))
|
---|
[2870] | 91 | throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
[3388] | 92 | value, Content.GetType()));
|
---|
[2870] | 93 | viewType = value;
|
---|
[3403] | 94 | OnViewTypeChanged();
|
---|
[2870] | 95 | }
|
---|
[2800] | 96 | }
|
---|
| 97 | }
|
---|
[3416] | 98 | public new IContent Content {
|
---|
[3389] | 99 | get { return base.Content; }
|
---|
[2655] | 100 | set {
|
---|
[3389] | 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;
|
---|
[2655] | 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[3177] | 109 | public new bool Enabled {
|
---|
| 110 | get { return base.Enabled; }
|
---|
| 111 | set {
|
---|
| 112 | this.SuspendRepaint();
|
---|
| 113 | base.Enabled = value;
|
---|
[3388] | 114 | this.viewsLabel.Enabled = value;
|
---|
[3177] | 115 | this.ResumeRepaint(true);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[2655] | 119 |
|
---|
[3416] | 120 |
|
---|
[3389] | 121 | protected override void OnContentChanged() {
|
---|
[3388] | 122 | messageLabel.Visible = false;
|
---|
[2687] | 123 | viewsLabel.Visible = false;
|
---|
[2870] | 124 | viewPanel.Visible = false;
|
---|
[2655] | 125 |
|
---|
[2713] | 126 | if (Content != null) {
|
---|
[3361] | 127 | if (viewContextMenuStrip.Items.Count == 0)
|
---|
[2665] | 128 | messageLabel.Visible = true;
|
---|
[3361] | 129 | else
|
---|
[2687] | 130 | viewsLabel.Visible = true;
|
---|
[2655] | 131 |
|
---|
[2800] | 132 | if (!ViewCanShowContent(viewType, Content)) {
|
---|
[3389] | 133 | ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
|
---|
[2838] | 134 | if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0)) // create first available view if default view is not available
|
---|
[3389] | 135 | ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
|
---|
[2797] | 136 | }
|
---|
[3403] | 137 |
|
---|
[3389] | 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();
|
---|
[2655] | 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[3388] | 147 |
|
---|
[3403] | 148 | private void OnViewTypeChanged() {
|
---|
[2800] | 149 | viewPanel.Controls.Clear();
|
---|
[3388] | 150 | if (viewType == null || Content == null)
|
---|
[2800] | 151 | return;
|
---|
[3388] | 152 | if (!ViewCanShowContent(viewType, Content))
|
---|
[2870] | 153 | throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
[3281] | 154 | viewType, Content.GetType()));
|
---|
[2800] | 155 |
|
---|
[2838] | 156 | UpdateActiveMenuItem();
|
---|
[3389] | 157 | IContentView view;
|
---|
[3388] | 158 | if (cachedViews.ContainsKey(ViewType))
|
---|
| 159 | view = cachedViews[ViewType];
|
---|
| 160 | else {
|
---|
[3416] | 161 | view = MainFormManager.CreateView(viewType, Content);
|
---|
| 162 | view.ReadOnly = this.ReadOnly;
|
---|
| 163 | view.Locked = this.Locked;
|
---|
[3389] | 164 | cachedViews.Add(viewType, view);
|
---|
[3388] | 165 | }
|
---|
[3403] | 166 | this.ActiveView = view;
|
---|
[3388] | 167 |
|
---|
| 168 | Control control = (Control)view;
|
---|
| 169 | control.Dock = DockStyle.Fill;
|
---|
| 170 | viewPanel.Controls.Add(control);
|
---|
[2800] | 171 | viewPanel.Visible = true;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[3403] | 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;
|
---|
[3416] | 193 | this.Locked = this.ActiveView.Locked;
|
---|
[3403] | 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | #region forwarding of view events
|
---|
[3416] | 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 | }
|
---|
[3403] | 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
|
---|
[2838] | 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 |
|
---|
[2800] | 245 | private bool ViewCanShowContent(Type viewType, object content) {
|
---|
[2797] | 246 | if (content == null) // every view can display null
|
---|
| 247 | return true;
|
---|
| 248 | if (viewType == null)
|
---|
| 249 | return false;
|
---|
[2838] | 250 | return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
|
---|
[2797] | 251 | }
|
---|
| 252 |
|
---|
[2694] | 253 | private void viewsLabel_DoubleClick(object sender, EventArgs e) {
|
---|
[3416] | 254 | IContentView view = MainFormManager.CreateView(viewType, Content);
|
---|
| 255 | view.ReadOnly = this.ReadOnly;
|
---|
| 256 | view.Locked = this.Locked;
|
---|
| 257 | view.Show();
|
---|
[2694] | 258 | }
|
---|
[2838] | 259 | private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
|
---|
[2687] | 260 | Type viewType = (Type)e.ClickedItem.Tag;
|
---|
[2797] | 261 | ViewType = viewType;
|
---|
[2655] | 262 | }
|
---|
[3391] | 263 |
|
---|
| 264 | private bool startDragAndDrop;
|
---|
| 265 | private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
|
---|
[3416] | 266 | if (!Locked) {
|
---|
| 267 | startDragAndDrop = true;
|
---|
| 268 | viewsLabel.Capture = false;
|
---|
| 269 | }
|
---|
[3391] | 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 | }
|
---|
[3403] | 280 | #endregion
|
---|
[2655] | 281 | }
|
---|
| 282 | }
|
---|