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