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 partial class MainFormBase : Form, IMainForm {
|
---|
35 | protected MainFormBase()
|
---|
36 | : base() {
|
---|
37 | InitializeComponent();
|
---|
38 | views = new List<IView>();
|
---|
39 | toolStripItems = new List<IToolStripItem>();
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected MainFormBase(Type userInterfaceItemType)
|
---|
43 | : this() {
|
---|
44 | this.userInterfaceItemType = userInterfaceItemType;
|
---|
45 | CreateGUI();
|
---|
46 | OnActiveViewChanged();
|
---|
47 | FireMainFormChanged();
|
---|
48 | }
|
---|
49 |
|
---|
50 | #region IMainForm Members
|
---|
51 | public string Title {
|
---|
52 | get { return this.Text; }
|
---|
53 | set {
|
---|
54 | if (InvokeRequired) {
|
---|
55 | Action<string> action = delegate(string s) { this.Title = s; };
|
---|
56 | Invoke(action, new object[] { value });
|
---|
57 | } else
|
---|
58 | this.Text = value;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public string StatusStripText {
|
---|
63 | get { return this.statusStripLabel.Text; }
|
---|
64 | set {
|
---|
65 | if (InvokeRequired) {
|
---|
66 | Action<string> action = delegate(string s) { this.StatusStripText = s; };
|
---|
67 | Invoke(action, new object[] { value });
|
---|
68 | } else
|
---|
69 | this.statusStripLabel.Text = value;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private Type userInterfaceItemType;
|
---|
74 | public Type UserInterfaceItemType {
|
---|
75 | get { return this.userInterfaceItemType; }
|
---|
76 | protected set { this.userInterfaceItemType = value; }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private IView activeView;
|
---|
80 | public IView ActiveView {
|
---|
81 | get { return this.activeView; }
|
---|
82 | protected set {
|
---|
83 | if (this.activeView != value) {
|
---|
84 | if (InvokeRequired) {
|
---|
85 | Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
|
---|
86 | Invoke(action, new object[] { value });
|
---|
87 | } else {
|
---|
88 | this.activeView = value;
|
---|
89 | OnActiveViewChanged();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private List<IToolStripItem> toolStripItems;
|
---|
96 | protected IEnumerable<IToolStripItem> ToolStripItems {
|
---|
97 | get { return this.toolStripItems; }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public event EventHandler ActiveViewChanged;
|
---|
101 | protected virtual void OnActiveViewChanged() {
|
---|
102 | if (ActiveViewChanged != null)
|
---|
103 | ActiveViewChanged(this, new EventArgs());
|
---|
104 | }
|
---|
105 |
|
---|
106 | public event EventHandler MainFormChanged;
|
---|
107 | public void FireMainFormChanged() {
|
---|
108 | OnMainFormChanged();
|
---|
109 | }
|
---|
110 | protected virtual void OnMainFormChanged() {
|
---|
111 | if (MainFormChanged != null)
|
---|
112 | MainFormChanged(this, new EventArgs());
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected List<IView> views;
|
---|
116 | public IEnumerable<IView> Views {
|
---|
117 | get { return views; }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public virtual void ShowView(IView view) {
|
---|
121 | if (!views.Contains(view)) {
|
---|
122 | view.MainForm = this;
|
---|
123 | views.Add(view);
|
---|
124 | ActiveView = view;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public virtual void CloseView(IView view) {
|
---|
129 | }
|
---|
130 |
|
---|
131 | public virtual void CloseAllViews() {
|
---|
132 | foreach (IView view in views.ToArray())
|
---|
133 | CloseView(view);
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected virtual void ViewClosed(IView view) {
|
---|
137 | }
|
---|
138 | #endregion
|
---|
139 |
|
---|
140 | #region create menu and toolbar
|
---|
141 | protected virtual void CreateGUI() {
|
---|
142 | DiscoveryService ds = new DiscoveryService();
|
---|
143 |
|
---|
144 | object[] items = ds.GetInstances(userInterfaceItemType);
|
---|
145 | IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
|
---|
146 | toolStripItems = toolStripItems.OrderBy(x => x.Position);
|
---|
147 | foreach (IToolStripMenuItem menuItem in toolStripItems) {
|
---|
148 | AddToolStripMenuItem(menuItem);
|
---|
149 | }
|
---|
150 |
|
---|
151 | items = ds.GetInstances(userInterfaceItemType);
|
---|
152 | toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
|
---|
153 | toolStripItems = toolStripItems.OrderBy(x => x.Position);
|
---|
154 | foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
|
---|
155 | AddToolStripButtonItem(toolStripButtonItem);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
|
---|
160 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
161 | SetToolStripItemProperties(item, menuItem);
|
---|
162 | item.ShortcutKeys = menuItem.ShortCutKeys;
|
---|
163 |
|
---|
164 | ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
|
---|
165 | if (parent == null)
|
---|
166 | menuStrip.Items.Add(item);
|
---|
167 | else
|
---|
168 | parent.DropDownItems.Add(item);
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
|
---|
172 | ToolStripItem item;
|
---|
173 | if (buttonItem.IsDropDownButton)
|
---|
174 | item = new ToolStripDropDownButton();
|
---|
175 | else
|
---|
176 | item = new ToolStripButton();
|
---|
177 |
|
---|
178 | SetToolStripItemProperties(item, buttonItem);
|
---|
179 | ToolStripDropDownItem parent = FindParent(buttonItem, toolStrip.Items);
|
---|
180 | if (parent == null)
|
---|
181 | toolStrip.Items.Add(item);
|
---|
182 | else
|
---|
183 | parent.DropDownItems.Add(item);
|
---|
184 | }
|
---|
185 |
|
---|
186 | private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
|
---|
187 | if (String.IsNullOrEmpty(item.Structure))
|
---|
188 | return null;
|
---|
189 |
|
---|
190 | ToolStripDropDownItem parent = null;
|
---|
191 | foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
|
---|
192 | if (parentItems.ContainsKey(structure))
|
---|
193 | parent = (ToolStripDropDownItem)parentItems[structure];
|
---|
194 | else
|
---|
195 | throw new ArgumentException("Structure string for item " + item.Name +
|
---|
196 | " is invalid. Could not find " + structure + " in toolstrip!");
|
---|
197 | parentItems = parent.DropDownItems;
|
---|
198 | }
|
---|
199 | return parent;
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
|
---|
203 | toolStripItem.Text = iToolStripItem.Name;
|
---|
204 | toolStripItem.Name = iToolStripItem.Name;
|
---|
205 | toolStripItem.Tag = iToolStripItem;
|
---|
206 | toolStripItem.Image = iToolStripItem.Image;
|
---|
207 | toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
|
---|
208 | this.ActiveViewChanged += new EventHandler(iToolStripItem.ActiveViewChanged);
|
---|
209 | this.MainFormChanged += new EventHandler(iToolStripItem.MainFormChanged);
|
---|
210 | toolStripItem.Click += new EventHandler(ToolStripItemClicked);
|
---|
211 | this.toolStripItems.Add(iToolStripItem);
|
---|
212 | iToolStripItem.ToolStripItem = toolStripItem;
|
---|
213 | }
|
---|
214 |
|
---|
215 | private void ToolStripItemClicked(object sender, EventArgs e) {
|
---|
216 | System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
|
---|
217 | ((IAction)item.Tag).Execute(this);
|
---|
218 | }
|
---|
219 | #endregion
|
---|
220 | }
|
---|
221 | }
|
---|