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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Data;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 |
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 |
|
---|
33 | namespace 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 | Type[] userInterfaceTypes = ds.GetTypes(userInterfaceItemType);
|
---|
80 |
|
---|
81 | foreach (Type t in userInterfaceTypes.Where(t=> typeof(IToolStripMenuItem).IsAssignableFrom(t))) {
|
---|
82 | if (!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
|
---|
83 | IToolStripMenuItem item = (IToolStripMenuItem) Activator.CreateInstance(t);
|
---|
84 | AddToolStripMenuItem(item);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | foreach (Type t in userInterfaceTypes.Where(t => typeof(IToolStripButtonItem).IsAssignableFrom(t))) {
|
---|
89 | if (!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
|
---|
90 | IToolStripButtonItem item = (IToolStripButtonItem)Activator.CreateInstance(t);
|
---|
91 | AddToolStripButtonItem(item);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
|
---|
97 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
98 | SetToolStripItemProperties(item, menuItem);
|
---|
99 | item.ShortcutKeys = menuItem.ShortCutKeys;
|
---|
100 |
|
---|
101 | ToolStripMenuItem parent = null;
|
---|
102 | if (!String.IsNullOrEmpty(menuItem.MenuStructure)) {
|
---|
103 | ToolStripItemCollection parentItems = menuStrip.Items;
|
---|
104 | foreach (string structure in menuItem.MenuStructure.Split(menuItem.MenuStructureSeparator)) {
|
---|
105 | if (parentItems.ContainsKey(structure))
|
---|
106 | parent = (ToolStripMenuItem)parentItems[structure];
|
---|
107 | else {
|
---|
108 | parent = new ToolStripMenuItem(structure);
|
---|
109 | parent.Name = structure;
|
---|
110 | parentItems.Add(parent);
|
---|
111 | }
|
---|
112 | parentItems = parent.DropDownItems;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (parent == null)
|
---|
117 | menuStrip.Items.Add(item);
|
---|
118 | else
|
---|
119 | parent.DropDownItems.Add(item);
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
|
---|
123 | ToolStripButton item = new ToolStripButton();
|
---|
124 | SetToolStripItemProperties(item, buttonItem);
|
---|
125 | toolStrip.Items.Add(item);
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
|
---|
129 | toolStripItem.Text = iToolStripItem.Name;
|
---|
130 | toolStripItem.Name = iToolStripItem.Name;
|
---|
131 | toolStripItem.Tag = iToolStripItem;
|
---|
132 | toolStripItem.Image = iToolStripItem.Image;
|
---|
133 | toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
|
---|
134 | toolStripItem.Click += new EventHandler(ToolStripItemClicked);
|
---|
135 | iToolStripItem.ToolStripItem = toolStripItem;
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void ToolStripItemClicked(object sender, EventArgs e) {
|
---|
139 | System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
|
---|
140 | ((IAction)item.Tag).Execute(this);
|
---|
141 | }
|
---|
142 | #endregion
|
---|
143 | }
|
---|
144 | }
|
---|