[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;
|
---|
[2739] | 26 | using HeuristicLab.Common;
|
---|
[2655] | 27 | using HeuristicLab.MainForm;
|
---|
| 28 |
|
---|
[2797] | 29 | namespace HeuristicLab.Core.Views {
|
---|
[2800] | 30 | public sealed partial class ViewHost : UserControl {
|
---|
| 31 | private Type viewType;
|
---|
| 32 | public Type ViewType {
|
---|
| 33 | get { return this.viewType; }
|
---|
| 34 | set {
|
---|
[2870] | 35 | if (viewType != value) {
|
---|
| 36 | if (value != null && !ViewCanShowContent(value, content))
|
---|
| 37 | throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
| 38 | value.GetPrettyName(),
|
---|
| 39 | content.GetType().GetPrettyName()));
|
---|
| 40 | viewType = value;
|
---|
| 41 | UpdateView();
|
---|
| 42 | }
|
---|
[2800] | 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[2713] | 46 | private object content;
|
---|
| 47 | public object Content {
|
---|
| 48 | get { return content; }
|
---|
[2655] | 49 | set {
|
---|
[2713] | 50 | if (value != content) {
|
---|
| 51 | content = value;
|
---|
[2838] | 52 | viewContextMenuStrip.Item = content;
|
---|
[2655] | 53 | Initialize();
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public ViewHost() {
|
---|
[2838] | 59 | InitializeComponent();
|
---|
[2797] | 60 | viewType = null;
|
---|
| 61 | content = null;
|
---|
[2655] | 62 | Initialize();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[2800] | 65 | private void Initialize() {
|
---|
[2687] | 66 | viewsLabel.Visible = false;
|
---|
[2838] | 67 | viewContextMenuStrip.Enabled = false;
|
---|
[2665] | 68 | messageLabel.Visible = false;
|
---|
[2838] | 69 |
|
---|
[2870] | 70 | viewPanel.Visible = false;
|
---|
[2655] | 71 | if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
|
---|
| 72 | viewPanel.Controls.Clear();
|
---|
| 73 |
|
---|
[2713] | 74 | if (Content != null) {
|
---|
[2838] | 75 | if (viewContextMenuStrip.Items.Count == 0) {
|
---|
[2665] | 76 | messageLabel.Visible = true;
|
---|
| 77 | } else {
|
---|
[2687] | 78 | viewsLabel.Visible = true;
|
---|
[2838] | 79 | viewContextMenuStrip.Enabled = true;
|
---|
[2655] | 80 | }
|
---|
| 81 |
|
---|
[2800] | 82 | if (!ViewCanShowContent(viewType, Content)) {
|
---|
[2797] | 83 | viewType = MainFormManager.GetDefaultViewType(Content.GetType());
|
---|
[2838] | 84 | if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0)) // create first available view if default view is not available
|
---|
| 85 | viewType = (Type)viewContextMenuStrip.Items[0].Tag;
|
---|
[2797] | 86 | }
|
---|
[2800] | 87 | UpdateView();
|
---|
[2655] | 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[2800] | 91 | private void UpdateView() {
|
---|
| 92 | if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
|
---|
| 93 | viewPanel.Controls.Clear();
|
---|
| 94 |
|
---|
| 95 | if (viewType == null || content == null)
|
---|
| 96 | return;
|
---|
| 97 |
|
---|
[2817] | 98 | if (!ViewCanShowContent(viewType, content))
|
---|
[2870] | 99 | throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
| 100 | viewType.GetPrettyName(),
|
---|
| 101 | Content.GetType().GetPrettyName()));
|
---|
[2800] | 102 |
|
---|
[2838] | 103 | UpdateActiveMenuItem();
|
---|
[2800] | 104 | Control view = (Control)MainFormManager.CreateView(viewType, Content);
|
---|
| 105 | viewPanel.Tag = view;
|
---|
| 106 | view.Dock = DockStyle.Fill;
|
---|
[2870] | 107 | viewPanel.Controls.Add(view);
|
---|
[2800] | 108 | viewPanel.Visible = true;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[2838] | 111 | private void UpdateActiveMenuItem() {
|
---|
| 112 | foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
|
---|
| 113 | if (item.Key == viewType) {
|
---|
| 114 | item.Value.Checked = true;
|
---|
| 115 | item.Value.Enabled = false;
|
---|
| 116 | } else {
|
---|
| 117 | item.Value.Checked = false;
|
---|
| 118 | item.Value.Enabled = true;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[2800] | 123 | private bool ViewCanShowContent(Type viewType, object content) {
|
---|
[2797] | 124 | if (content == null) // every view can display null
|
---|
| 125 | return true;
|
---|
| 126 | if (viewType == null)
|
---|
| 127 | return false;
|
---|
[2838] | 128 | return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
|
---|
[2797] | 129 | }
|
---|
| 130 |
|
---|
[2694] | 131 | private void viewsLabel_DoubleClick(object sender, EventArgs e) {
|
---|
[2838] | 132 | MainFormManager.CreateView(viewType, Content).Show();
|
---|
[2694] | 133 | }
|
---|
[2838] | 134 |
|
---|
| 135 | private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
|
---|
[2687] | 136 | Type viewType = (Type)e.ClickedItem.Tag;
|
---|
[2797] | 137 | ViewType = viewType;
|
---|
[2655] | 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|