Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm/3.2/MainFormBase.cs @ 2249

Last change on this file since 2249 was 2249, checked in by mkommend, 15 years ago

adapted MainForm to create DropDownButtons (ticket #716)

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.MainForm {
34  public abstract partial class MainFormBase : Form, IMainForm {
35    protected MainFormBase(Type userInterfaceItemType)
36      : base() {
37      InitializeComponent();
38      openViews = new List<IView>();
39      this.userInterfaceItemType = userInterfaceItemType;
40      CreateGUI();
41    }
42
43    #region IMainForm Members
44    public string Title {
45      get { return this.Text; }
46      set { this.Text = value; }
47    }
48
49    public string StatusStripText {
50      get { return this.statusStripLabel.Text; }
51      set { this.statusStripLabel.Text = value; }
52    }
53
54    protected Type userInterfaceItemType;
55    public Type UserInterfaceItemType {
56      get { return this.userInterfaceItemType; }
57    }
58
59    protected IView activeView;
60    public IView ActiveView {
61      get { return this.activeView; }
62    }
63
64    protected List<IView> openViews;
65    public IEnumerable<IView> OpenViews {
66      get { return openViews; }
67    }
68
69    public virtual void ShowView(IView view) {
70      view.MainForm = this;
71      activeView = view;
72      openViews.Add(view);
73    }
74    #endregion
75
76    #region create menu and toolbar
77    private void CreateGUI() {
78      DiscoveryService ds = new DiscoveryService();
79
80      object[] items = ds.GetInstances(userInterfaceItemType);
81      IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
82      toolStripItems = toolStripItems.OrderBy(x => x.Position);
83      foreach (IToolStripMenuItem menuItem in toolStripItems) {
84        AddToolStripMenuItem(menuItem);
85      }
86
87      items = ds.GetInstances(userInterfaceItemType);
88      toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
89      toolStripItems = toolStripItems.OrderBy(x => x.Position);
90      foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
91        AddToolStripButtonItem(toolStripButtonItem);
92      }
93    }
94
95    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
96      ToolStripMenuItem item = new ToolStripMenuItem();
97      SetToolStripItemProperties(item, menuItem);
98      item.ShortcutKeys = menuItem.ShortCutKeys;
99
100      ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
101      if (parent == null)
102        menuStrip.Items.Add(item);
103      else
104        parent.DropDownItems.Add(item);
105    }
106
107    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
108      ToolStripItem item;
109      if (buttonItem.IsDropDownButton)
110        item = new ToolStripDropDownButton();
111      else
112        item = new ToolStripButton();
113
114      SetToolStripItemProperties(item, buttonItem);
115      ToolStripDropDownItem parent = FindParent(buttonItem,toolStrip.Items);
116      if (parent == null)
117        toolStrip.Items.Add(item);
118      else
119        parent.DropDownItems.Add(item);
120    }
121
122    private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
123      ToolStripDropDownItem parent = null;
124      foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
125        if (parentItems.ContainsKey(structure))
126          parent = (ToolStripDropDownItem)parentItems[structure];
127        else
128          break;
129        parentItems = parent.DropDownItems;
130      }
131      return parent;
132    }
133
134    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
135      toolStripItem.Text = iToolStripItem.Name;
136      toolStripItem.Name = iToolStripItem.Name;
137      toolStripItem.Tag = iToolStripItem;
138      toolStripItem.Image = iToolStripItem.Image;
139      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
140      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
141      iToolStripItem.ToolStripItem = toolStripItem;
142    }
143
144    private void ToolStripItemClicked(object sender, EventArgs e) {
145      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
146      ((IAction)item.Tag).Execute(this);
147    }
148    #endregion
149  }
150}
Note: See TracBrowser for help on using the repository browser.