[2233] | 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 |
|
---|
[2243] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
| 32 |
|
---|
[2426] | 33 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
[2268] | 34 | public partial class MainFormBase : Form, IMainForm {
|
---|
| 35 | protected MainFormBase()
|
---|
[2233] | 36 | : base() {
|
---|
| 37 | InitializeComponent();
|
---|
[2443] | 38 | this.views = new Dictionary<IView, Form>();
|
---|
[2426] | 39 | this.userInterfaceItems = new List<IUserInterfaceItem>();
|
---|
[2268] | 40 | }
|
---|
| 41 |
|
---|
| 42 | protected MainFormBase(Type userInterfaceItemType)
|
---|
| 43 | : this() {
|
---|
[2233] | 44 | this.userInterfaceItemType = userInterfaceItemType;
|
---|
[2243] | 45 | CreateGUI();
|
---|
[2233] | 46 | }
|
---|
| 47 |
|
---|
[2469] | 48 | private void MainFormBase_Load(object sender, EventArgs e) {
|
---|
| 49 | if (!DesignMode)
|
---|
| 50 | MainFormManager.RegisterMainForm(this);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[2233] | 53 | #region IMainForm Members
|
---|
| 54 | public string Title {
|
---|
[2243] | 55 | get { return this.Text; }
|
---|
[2256] | 56 | set {
|
---|
| 57 | if (InvokeRequired) {
|
---|
| 58 | Action<string> action = delegate(string s) { this.Title = s; };
|
---|
[2407] | 59 | Invoke(action, value);
|
---|
[2256] | 60 | } else
|
---|
| 61 | this.Text = value;
|
---|
| 62 | }
|
---|
[2233] | 63 | }
|
---|
| 64 |
|
---|
[2350] | 65 | public override Cursor Cursor {
|
---|
[2338] | 66 | get { return base.Cursor; }
|
---|
| 67 | set {
|
---|
| 68 | if (InvokeRequired) {
|
---|
| 69 | Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
|
---|
[2407] | 70 | Invoke(action, value);
|
---|
[2338] | 71 | } else
|
---|
| 72 | base.Cursor = value;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[2256] | 76 | private Type userInterfaceItemType;
|
---|
[2233] | 77 | public Type UserInterfaceItemType {
|
---|
| 78 | get { return this.userInterfaceItemType; }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[2443] | 81 | private Dictionary<IView, Form> views;
|
---|
[2358] | 82 | public IEnumerable<IView> Views {
|
---|
[2443] | 83 | get { return views.Keys; }
|
---|
[2358] | 84 | }
|
---|
| 85 |
|
---|
[2443] | 86 | public Form GetForm(IView view) {
|
---|
| 87 | if (views.ContainsKey(view))
|
---|
| 88 | return views[view];
|
---|
| 89 | return null;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[2254] | 92 | private IView activeView;
|
---|
[2233] | 93 | public IView ActiveView {
|
---|
| 94 | get { return this.activeView; }
|
---|
[2254] | 95 | protected set {
|
---|
| 96 | if (this.activeView != value) {
|
---|
[2309] | 97 | if (InvokeRequired) {
|
---|
| 98 | Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
|
---|
[2407] | 99 | Invoke(action, value);
|
---|
[2309] | 100 | } else {
|
---|
| 101 | this.activeView = value;
|
---|
| 102 | OnActiveViewChanged();
|
---|
| 103 | }
|
---|
[2254] | 104 | }
|
---|
| 105 | }
|
---|
[2233] | 106 | }
|
---|
| 107 |
|
---|
[2426] | 108 | private List<IUserInterfaceItem> userInterfaceItems;
|
---|
| 109 | protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
|
---|
| 110 | get { return this.userInterfaceItems; }
|
---|
[2256] | 111 | }
|
---|
[2254] | 112 |
|
---|
| 113 | public event EventHandler ActiveViewChanged;
|
---|
| 114 | protected virtual void OnActiveViewChanged() {
|
---|
[2336] | 115 | if (InvokeRequired)
|
---|
| 116 | Invoke((MethodInvoker)OnActiveViewChanged);
|
---|
| 117 | else if (ActiveViewChanged != null)
|
---|
[2256] | 118 | ActiveViewChanged(this, new EventArgs());
|
---|
[2254] | 119 | }
|
---|
| 120 |
|
---|
[2426] | 121 | public event EventHandler Changed;
|
---|
[2300] | 122 | public void FireMainFormChanged() {
|
---|
| 123 | OnMainFormChanged();
|
---|
| 124 | }
|
---|
| 125 | protected virtual void OnMainFormChanged() {
|
---|
[2336] | 126 | if (InvokeRequired)
|
---|
[2443] | 127 | Invoke((MethodInvoker)OnMainFormChanged);
|
---|
[2426] | 128 | else if (Changed != null)
|
---|
| 129 | Changed(this, new EventArgs());
|
---|
[2300] | 130 | }
|
---|
| 131 |
|
---|
[2443] | 132 | protected virtual Form CreateForm(IView view) {
|
---|
| 133 | throw new NotImplementedException("CreateForm must be implemented in subclasses of MainFormBase.");
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | public virtual bool ShowView(IView view) {
|
---|
| 137 | if (InvokeRequired) return (bool)Invoke((Func<IView, bool>)ShowView, view);
|
---|
| 138 | else {
|
---|
| 139 | if (!views.Keys.Contains(view)) {
|
---|
| 140 | Form form = CreateForm(view);
|
---|
| 141 | views[view] = form;
|
---|
| 142 | form.Activated += new EventHandler(FormActivated);
|
---|
| 143 | form.GotFocus += new EventHandler(FormActivated);
|
---|
| 144 | form.FormClosing += new FormClosingEventHandler(view.OnClosing);
|
---|
| 145 | form.FormClosed += new FormClosedEventHandler(view.OnClosed);
|
---|
| 146 | form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
|
---|
| 147 | foreach (IUserInterfaceItem item in UserInterfaceItems)
|
---|
| 148 | view.Changed += new EventHandler(item.ViewChanged);
|
---|
| 149 | return true;
|
---|
[2460] | 150 | } else
|
---|
[2443] | 151 | return false;
|
---|
[2306] | 152 | }
|
---|
[2233] | 153 | }
|
---|
[2297] | 154 |
|
---|
[2443] | 155 | public virtual void HideView(IView view) {
|
---|
| 156 | if (InvokeRequired) Invoke((Action<IView>)HideView, view);
|
---|
| 157 | else {
|
---|
| 158 | if (views.ContainsKey(view))
|
---|
| 159 | views[view].Hide();
|
---|
| 160 | }
|
---|
[2305] | 161 | }
|
---|
| 162 |
|
---|
[2443] | 163 | public void CloseView(IView view) {
|
---|
| 164 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
| 165 | else {
|
---|
| 166 | if (views.ContainsKey(view))
|
---|
| 167 | views[view].Close();
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[2305] | 171 | public virtual void CloseAllViews() {
|
---|
[2443] | 172 | foreach (IView view in views.Keys.ToArray())
|
---|
[2305] | 173 | CloseView(view);
|
---|
| 174 | }
|
---|
[2443] | 175 | #endregion
|
---|
[2305] | 176 |
|
---|
[2443] | 177 | #region events
|
---|
| 178 | private void ChildFormClosed(object sender, FormClosedEventArgs e) {
|
---|
| 179 | Form form = (Form)sender;
|
---|
| 180 | IView view = GetViewForForm(form);
|
---|
| 181 |
|
---|
| 182 | form.Activated -= new EventHandler(FormActivated);
|
---|
| 183 | form.GotFocus -= new EventHandler(FormActivated);
|
---|
| 184 | form.FormClosing -= new FormClosingEventHandler(view.OnClosing);
|
---|
| 185 | form.FormClosed -= new FormClosedEventHandler(view.OnClosed);
|
---|
| 186 | form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
|
---|
| 187 | foreach (IUserInterfaceItem item in UserInterfaceItems)
|
---|
| 188 | view.Changed -= new EventHandler(item.ViewChanged);
|
---|
| 189 |
|
---|
[2358] | 190 | views.Remove(view);
|
---|
| 191 | if (ActiveView == view)
|
---|
| 192 | ActiveView = null;
|
---|
[2297] | 193 | }
|
---|
[2443] | 194 |
|
---|
| 195 | private void FormActivated(object sender, EventArgs e) {
|
---|
| 196 | this.ActiveView = GetViewForForm((Form)sender);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | private IView GetViewForForm(Form form) {
|
---|
| 200 | return views.Where(x => x.Value == form).Single().Key;
|
---|
| 201 | }
|
---|
[2243] | 202 | #endregion
|
---|
[2233] | 203 |
|
---|
[2243] | 204 | #region create menu and toolbar
|
---|
[2268] | 205 | protected virtual void CreateGUI() {
|
---|
[2243] | 206 | DiscoveryService ds = new DiscoveryService();
|
---|
| 207 |
|
---|
[2247] | 208 | object[] items = ds.GetInstances(userInterfaceItemType);
|
---|
[2426] | 209 | IEnumerable<MenuItemBase> toolStripMenuItems =
|
---|
[2407] | 210 | from mi in items
|
---|
[2426] | 211 | where mi is MenuItemBase
|
---|
| 212 | orderby ((MenuItemBase)mi).Position
|
---|
| 213 | select (MenuItemBase)mi;
|
---|
| 214 | foreach (MenuItemBase menuItem in toolStripMenuItems)
|
---|
[2247] | 215 | AddToolStripMenuItem(menuItem);
|
---|
[2243] | 216 |
|
---|
[2247] | 217 | items = ds.GetInstances(userInterfaceItemType);
|
---|
[2426] | 218 | IEnumerable<ToolBarItemBase> toolStripButtonItems =
|
---|
[2407] | 219 | from bi in items
|
---|
[2426] | 220 | where bi is ToolBarItemBase
|
---|
| 221 | orderby ((ToolBarItemBase)bi).Position
|
---|
| 222 | select (ToolBarItemBase)bi;
|
---|
| 223 | foreach (ToolBarItemBase toolStripButtonItem in toolStripButtonItems)
|
---|
[2247] | 224 | AddToolStripButtonItem(toolStripButtonItem);
|
---|
[2243] | 225 | }
|
---|
| 226 |
|
---|
[2426] | 227 | private void AddToolStripMenuItem(MenuItemBase menuItem) {
|
---|
[2243] | 228 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
[2249] | 229 | SetToolStripItemProperties(item, menuItem);
|
---|
[2426] | 230 | menuItem.ToolStripItem = item;
|
---|
[2243] | 231 | item.ShortcutKeys = menuItem.ShortCutKeys;
|
---|
[2426] | 232 | item.DisplayStyle = menuItem.ToolStripItemDisplayStyle;
|
---|
| 233 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
|
---|
[2243] | 234 | }
|
---|
| 235 |
|
---|
[2426] | 236 | private void AddToolStripButtonItem(ToolBarItemBase buttonItem) {
|
---|
[2249] | 237 | ToolStripItem item;
|
---|
| 238 | if (buttonItem.IsDropDownButton)
|
---|
| 239 | item = new ToolStripDropDownButton();
|
---|
| 240 | else
|
---|
| 241 | item = new ToolStripButton();
|
---|
| 242 |
|
---|
[2243] | 243 | SetToolStripItemProperties(item, buttonItem);
|
---|
[2426] | 244 | item.DisplayStyle = buttonItem.ToolStripItemDisplayStyle;
|
---|
| 245 | buttonItem.ToolStripItem = item;
|
---|
| 246 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
|
---|
[2243] | 247 | }
|
---|
| 248 |
|
---|
[2443] | 249 | private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
|
---|
[2249] | 250 | ToolStripDropDownItem parent = null;
|
---|
[2426] | 251 | foreach (string s in structure) {
|
---|
| 252 | if (parentItems.ContainsKey(s))
|
---|
| 253 | parent = (ToolStripDropDownItem)parentItems[s];
|
---|
| 254 | else {
|
---|
[2443] | 255 | parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
|
---|
[2426] | 256 | parentItems.Add(parent);
|
---|
| 257 | }
|
---|
[2249] | 258 | parentItems = parent.DropDownItems;
|
---|
| 259 | }
|
---|
[2426] | 260 | parentItems.Add(item);
|
---|
[2249] | 261 | }
|
---|
| 262 |
|
---|
[2426] | 263 | private void SetToolStripItemProperties(ToolStripItem toolStripItem, IUserInterfaceItem userInterfaceItem) {
|
---|
| 264 | toolStripItem.Name = userInterfaceItem.Name;
|
---|
| 265 | toolStripItem.Text = userInterfaceItem.Name;
|
---|
| 266 | toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
|
---|
| 267 | toolStripItem.Tag = userInterfaceItem;
|
---|
| 268 | toolStripItem.Image = userInterfaceItem.Image;
|
---|
| 269 | this.ActiveViewChanged += new EventHandler(userInterfaceItem.ActiveViewChanged);
|
---|
| 270 | this.Changed += new EventHandler(userInterfaceItem.MainFormChanged);
|
---|
[2243] | 271 | toolStripItem.Click += new EventHandler(ToolStripItemClicked);
|
---|
[2426] | 272 | this.userInterfaceItems.Add(userInterfaceItem);
|
---|
[2243] | 273 | }
|
---|
| 274 |
|
---|
| 275 | private void ToolStripItemClicked(object sender, EventArgs e) {
|
---|
| 276 | System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
|
---|
[2426] | 277 | ((IUserInterfaceItem)item.Tag).Execute();
|
---|
[2243] | 278 | }
|
---|
[2233] | 279 | #endregion
|
---|
| 280 | }
|
---|
| 281 | }
|
---|