Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2458 was 2458, checked in by mkommend, 14 years ago

integrated branch MainForm refactoring into trunk (ticket #771)

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