Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2805 was 2800, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • minor changes in ViewHost
File size: 5.5 KB
RevLine 
[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
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;
[2739]30using HeuristicLab.Common;
[2655]31using HeuristicLab.MainForm;
32
[2797]33namespace HeuristicLab.Core.Views {
[2800]34  public sealed partial class ViewHost : UserControl {
[2687]35    private Dictionary<Type, ToolStripMenuItem> typeMenuItemTable;
[2800]36
[2792]37    public IEnumerable<Type> AvailableViewTypes {
38      get { return typeMenuItemTable.Keys; }
39    }
40
[2800]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
[2713]54    private object content;
55    public object Content {
56      get { return content; }
[2655]57      set {
[2713]58        if (value != content) {
59          content = value;
[2655]60          Initialize();
61        }
62      }
63    }
64
65    public ViewHost() {
[2687]66      typeMenuItemTable = new Dictionary<Type, ToolStripMenuItem>();
[2797]67      viewType = null;
68      content = null;
[2655]69      InitializeComponent();
70      Initialize();
71    }
72
[2800]73    private void Initialize() {
[2687]74      viewsLabel.Enabled = false;
75      viewsLabel.Visible = false;
76      typeMenuItemTable.Clear();
77      contextMenuStrip.Items.Clear();
78      contextMenuStrip.Enabled = false;
[2665]79      messageLabel.Visible = false;
[2655]80      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
81      viewPanel.Controls.Clear();
82      viewPanel.Enabled = false;
[2684]83      viewPanel.Visible = false;
[2655]84
[2713]85      if (Content != null) {
86        var viewTypes = from t in MainFormManager.GetViewTypes(Content.GetType())
[2655]87                        orderby t.Name ascending
88                        select t;
[2792]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;
[2687]94          contextMenuStrip.Items.Add(item);
[2792]95          typeMenuItemTable.Add(v, item);
[2687]96        }
97        if (contextMenuStrip.Items.Count == 0) {
[2665]98          messageLabel.Visible = true;
99        } else {
[2687]100          viewsLabel.Enabled = true;
101          viewsLabel.Visible = true;
102          contextMenuStrip.Enabled = true;
[2665]103          messageLabel.Visible = false;
[2655]104        }
105
[2800]106        if (!ViewCanShowContent(viewType, Content)) {
[2797]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        }
[2800]111        UpdateView();
[2655]112      }
113    }
114
[2800]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) {
[2797]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
[2694]150    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
[2713]151      MainFormManager.CreateView(viewPanel.Tag.GetType(), Content).Show();
[2694]152    }
[2800]153    private void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
[2687]154      Type viewType = (Type)e.ClickedItem.Tag;
[2797]155      ViewType = viewType;
[2655]156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.