Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ViewContextMenuStrip.cs @ 10078

Last change on this file since 10078 was 10042, checked in by jkarder, 11 years ago

#2116: added prototype of a breadcrumb navigation

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
26using System.Windows.Forms;
27
28namespace HeuristicLab.MainForm.WindowsForms {
29  public sealed partial class ViewContextMenuStrip : ContextMenuStrip {
30    public ViewContextMenuStrip() {
31      InitializeComponent();
32      menuItems = new Dictionary<Type, ToolStripMenuItem>();
33      ignoredViewTypes = new List<Type>();
34    }
35
36    public ViewContextMenuStrip(IContainer container)
37      : base(container) {
38      InitializeComponent();
39      menuItems = new Dictionary<Type, ToolStripMenuItem>();
40      ignoredViewTypes = new List<Type>();
41      ShowBreadcrumbsToolStripMenuItem = new ToolStripMenuItem("Show Breadcrumbs");
42      ShowBreadcrumbsToolStripMenuItem.Click += ShowBreadcrumbsToolStripMenuItem_Click;
43      ShowBreadcrumbsToolStripMenuItem.CheckedChanged += ShowBreadcrumbsToolStripMenuItem_CheckedChanged;
44    }
45
46    private object item;
47    public object Item {
48      get { return item; }
49      set {
50        if (item != value) {
51          item = value;
52          RefreshMenuItems();
53        }
54      }
55    }
56
57    private List<Type> ignoredViewTypes;
58    public IEnumerable<Type> IgnoredViewTypes {
59      get { return ignoredViewTypes; }
60      set { ignoredViewTypes = new List<Type>(value); RefreshMenuItems(); }
61    }
62
63    private Dictionary<Type, ToolStripMenuItem> menuItems;
64    public IEnumerable<KeyValuePair<Type, ToolStripMenuItem>> MenuItems {
65      get { return menuItems; }
66    }
67
68    public ToolStripMenuItem ShowBreadcrumbsToolStripMenuItem { get; private set; }
69
70    private void RefreshMenuItems() {
71      if (InvokeRequired) Invoke((Action)RefreshMenuItems);
72      else {
73        foreach (ToolStripMenuItem m in menuItems.Values)
74          m.Dispose();
75        Items.Clear();
76        menuItems.Clear();
77
78        if (item != null) {
79          ToolStripMenuItem menuItem;
80          IEnumerable<Type> types = MainFormManager.GetViewTypes(item.GetType(), true);
81          foreach (Type t in types.Except(IgnoredViewTypes)) {
82            menuItem = new ToolStripMenuItem();
83            menuItem.Tag = t;
84            menuItem.Text = ViewAttribute.GetViewName(t);
85
86            menuItems.Add(t, menuItem);
87            Items.Add(menuItem);
88          }
89          if (Items.Count > 0)
90            Items.Add(new ToolStripSeparator());
91          Items.Add(ShowBreadcrumbsToolStripMenuItem);
92        }
93      }
94    }
95
96    public event EventHandler ShowBreadcrumbsChanged;
97    private void OnShowBreadcrumbsChanged() {
98      var handler = ShowBreadcrumbsChanged;
99      if (handler != null) handler(this, EventArgs.Empty);
100    }
101
102    private void ShowBreadcrumbsToolStripMenuItem_CheckedChanged(object sender, EventArgs e) {
103      OnShowBreadcrumbsChanged();
104    }
105
106    private void ShowBreadcrumbsToolStripMenuItem_Click(object sender, EventArgs e) {
107      ShowBreadcrumbsToolStripMenuItem.Checked = !ShowBreadcrumbsToolStripMenuItem.Checked;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.