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.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using System.Linq;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Core.Views {
|
---|
34 | public sealed partial class ViewHost : UserControl {
|
---|
35 | private Dictionary<Type, ToolStripMenuItem> typeMenuItemTable;
|
---|
36 |
|
---|
37 | public IEnumerable<Type> AvailableViewTypes {
|
---|
38 | get { return typeMenuItemTable.Keys; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | private Type viewType;
|
---|
42 | public Type ViewType {
|
---|
43 | get { return this.viewType; }
|
---|
44 | set {
|
---|
45 | if (!ViewCanShowContent(value, content))
|
---|
46 | throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
47 | value.GetPrettyName(),
|
---|
48 | content.GetType().GetPrettyName()));
|
---|
49 | viewType = value;
|
---|
50 | UpdateView();
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private object content;
|
---|
55 | public object Content {
|
---|
56 | get { return content; }
|
---|
57 | set {
|
---|
58 | if (value != content) {
|
---|
59 | content = value;
|
---|
60 | Initialize();
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public ViewHost() {
|
---|
66 | typeMenuItemTable = new Dictionary<Type, ToolStripMenuItem>();
|
---|
67 | viewType = null;
|
---|
68 | content = null;
|
---|
69 | InitializeComponent();
|
---|
70 | Initialize();
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void Initialize() {
|
---|
74 | viewsLabel.Enabled = false;
|
---|
75 | viewsLabel.Visible = false;
|
---|
76 | typeMenuItemTable.Clear();
|
---|
77 | contextMenuStrip.Items.Clear();
|
---|
78 | contextMenuStrip.Enabled = false;
|
---|
79 | messageLabel.Visible = false;
|
---|
80 | if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
|
---|
81 | viewPanel.Controls.Clear();
|
---|
82 | viewPanel.Enabled = false;
|
---|
83 | viewPanel.Visible = false;
|
---|
84 |
|
---|
85 | if (Content != null) {
|
---|
86 | var viewTypes = from t in MainFormManager.GetViewTypes(Content.GetType())
|
---|
87 | orderby t.Name ascending
|
---|
88 | select t;
|
---|
89 | foreach (Type v in viewTypes) {
|
---|
90 | ToolStripMenuItem item = new ToolStripMenuItem(v.GetPrettyName());
|
---|
91 | item.Name = v.FullName;
|
---|
92 | item.ToolTipText = v.GetPrettyName(true);
|
---|
93 | item.Tag = v;
|
---|
94 | contextMenuStrip.Items.Add(item);
|
---|
95 | typeMenuItemTable.Add(v, item);
|
---|
96 | }
|
---|
97 | if (contextMenuStrip.Items.Count == 0) {
|
---|
98 | messageLabel.Visible = true;
|
---|
99 | } else {
|
---|
100 | viewsLabel.Enabled = true;
|
---|
101 | viewsLabel.Visible = true;
|
---|
102 | contextMenuStrip.Enabled = true;
|
---|
103 | messageLabel.Visible = false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (!ViewCanShowContent(viewType, Content)) {
|
---|
107 | viewType = MainFormManager.GetDefaultViewType(Content.GetType());
|
---|
108 | if ((viewType == null) && (contextMenuStrip.Items.Count > 0)) // create first available view if default view is not available
|
---|
109 | viewType = (Type)contextMenuStrip.Items[0].Tag;
|
---|
110 | }
|
---|
111 | UpdateView();
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void UpdateView() {
|
---|
116 | if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
|
---|
117 | viewPanel.Controls.Clear();
|
---|
118 |
|
---|
119 | if (viewType == null || content == null)
|
---|
120 | return;
|
---|
121 |
|
---|
122 | if (!ViewCanShowContent(viewType, content) || !typeMenuItemTable.ContainsKey(viewType))
|
---|
123 | throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
124 | viewType.GetPrettyName(),
|
---|
125 | Content.GetType().GetPrettyName()));
|
---|
126 |
|
---|
127 | foreach (ToolStripMenuItem item in typeMenuItemTable.Values) {
|
---|
128 | item.Checked = false;
|
---|
129 | item.Enabled = true;
|
---|
130 | }
|
---|
131 | typeMenuItemTable[viewType].Checked = true;
|
---|
132 | typeMenuItemTable[viewType].Enabled = false;
|
---|
133 |
|
---|
134 | Control view = (Control)MainFormManager.CreateView(viewType, Content);
|
---|
135 | viewPanel.Controls.Add(view);
|
---|
136 | viewPanel.Tag = view;
|
---|
137 | view.Dock = DockStyle.Fill;
|
---|
138 | viewPanel.Enabled = true;
|
---|
139 | viewPanel.Visible = true;
|
---|
140 | }
|
---|
141 |
|
---|
142 | private bool ViewCanShowContent(Type viewType, object content) {
|
---|
143 | if (content == null) // every view can display null
|
---|
144 | return true;
|
---|
145 | if (viewType == null)
|
---|
146 | return false;
|
---|
147 | return ContentAttribute.CanViewType(viewType, Content.GetType());
|
---|
148 | }
|
---|
149 |
|
---|
150 | private void viewsLabel_DoubleClick(object sender, EventArgs e) {
|
---|
151 | MainFormManager.CreateView(viewPanel.Tag.GetType(), Content).Show();
|
---|
152 | }
|
---|
153 | private void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
|
---|
154 | Type viewType = (Type)e.ClickedItem.Tag;
|
---|
155 | ViewType = viewType;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|