source: trunk/sources/HeuristicLab.Core.Views/3.3/ViewHost.cs @ 2792

Last change on this file since 2792 was 2792, checked in by swagner, 11 years ago

Operator architecture refactoring (#95)

  • added method ShowView to ViewHost to be able to set the currently shown view.
File size: 4.6 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using System.Linq;
30using HeuristicLab.Common;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Core.Views {
34  public 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 object content;
42    public object Content {
43      get { return content; }
44      set {
45        if (value != content) {
46          content = value;
47          Initialize();
48        }
49      }
50    }
51
52    public ViewHost() {
53      typeMenuItemTable = new Dictionary<Type, ToolStripMenuItem>();
54      InitializeComponent();
55      Initialize();
56    }
57
58    public void ShowView(Type viewType) {
59      if (!typeMenuItemTable.ContainsKey(viewType))
60        throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
61                                                  viewType.GetPrettyName(),
62                                                  content.GetType().GetPrettyName()));
63
64      foreach (ToolStripMenuItem item in typeMenuItemTable.Values) {
65        item.Checked = false;
66        item.Enabled = true;
67      }
68      typeMenuItemTable[viewType].Checked = true;
69      typeMenuItemTable[viewType].Enabled = false;
70
71      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
72      viewPanel.Controls.Clear();
73      Control view = (Control)MainFormManager.CreateView(viewType, Content);
74      viewPanel.Controls.Add(view);
75      viewPanel.Tag = view;
76      view.Dock = DockStyle.Fill;
77      viewPanel.Enabled = true;
78      viewPanel.Visible = true;
79    }
80
81    protected virtual void Initialize() {
82      viewsLabel.Enabled = false;
83      viewsLabel.Visible = false;
84      typeMenuItemTable.Clear();
85      contextMenuStrip.Items.Clear();
86      contextMenuStrip.Enabled = false;
87      messageLabel.Visible = false;
88      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
89      viewPanel.Controls.Clear();
90      viewPanel.Enabled = false;
91      viewPanel.Visible = false;
92
93      if (Content != null) {
94        var viewTypes = from t in MainFormManager.GetViewTypes(Content.GetType())
95                        orderby t.Name ascending
96                        select t;
97        foreach (Type v in viewTypes) {
98          ToolStripMenuItem item = new ToolStripMenuItem(v.GetPrettyName());
99          item.Name = v.FullName;
100          item.ToolTipText = v.GetPrettyName(true);
101          item.Tag = v;
102          contextMenuStrip.Items.Add(item);
103          typeMenuItemTable.Add(v, item);
104        }
105        if (contextMenuStrip.Items.Count == 0) {
106          messageLabel.Visible = true;
107        } else {
108          viewsLabel.Enabled = true;
109          viewsLabel.Visible = true;
110          contextMenuStrip.Enabled = true;
111          messageLabel.Visible = false;
112        }
113
114        Type viewType = MainFormManager.GetDefaultViewType(Content.GetType());
115        if ((viewType == null) && (contextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
116          viewType = (Type)contextMenuStrip.Items[0].Tag;
117        if (viewType != null)
118          ShowView(viewType);
119      }
120    }
121
122    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
123      MainFormManager.CreateView(viewPanel.Tag.GetType(), Content).Show();
124    }
125    protected void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
126      Type viewType = (Type)e.ClickedItem.Tag;
127      ShowView(viewType);
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.