Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.MainForm.WindowsForms/3.2/MainFormBase.cs @ 3620

Last change on this file since 3620 was 2469, checked in by mkommend, 15 years ago

moved registering of mainform from ctor to mainform.load event, because designmode property is not set in the ctor (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    }
41
42    protected MainFormBase(Type userInterfaceItemType)
43      : this() {
44      this.userInterfaceItemType = userInterfaceItemType;
45      CreateGUI();
46    }
47
48    private void MainFormBase_Load(object sender, EventArgs e) {
49      if (!DesignMode)
50        MainFormManager.RegisterMainForm(this);
51    }
52
53    #region IMainForm Members
54    public string Title {
55      get { return this.Text; }
56      set {
57        if (InvokeRequired) {
58          Action<string> action = delegate(string s) { this.Title = s; };
59          Invoke(action, value);
60        } else
61          this.Text = value;
62      }
63    }
64
65    public override Cursor Cursor {
66      get { return base.Cursor; }
67      set {
68        if (InvokeRequired) {
69          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
70          Invoke(action, value);
71        } else
72          base.Cursor = value;
73      }
74    }
75
76    private Type userInterfaceItemType;
77    public Type UserInterfaceItemType {
78      get { return this.userInterfaceItemType; }
79    }
80
81    private Dictionary<IView, Form> views;
82    public IEnumerable<IView> Views {
83      get { return views.Keys; }
84    }
85
86    public Form GetForm(IView view) {
87      if (views.ContainsKey(view))
88        return views[view];
89      return null;
90    }
91
92    private IView activeView;
93    public IView ActiveView {
94      get { return this.activeView; }
95      protected set {
96        if (this.activeView != value) {
97          if (InvokeRequired) {
98            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
99            Invoke(action, value);
100          } else {
101            this.activeView = value;
102            OnActiveViewChanged();
103          }
104        }
105      }
106    }
107
108    private List<IUserInterfaceItem> userInterfaceItems;
109    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
110      get { return this.userInterfaceItems; }
111    }
112
113    public event EventHandler ActiveViewChanged;
114    protected virtual void OnActiveViewChanged() {
115      if (InvokeRequired)
116        Invoke((MethodInvoker)OnActiveViewChanged);
117      else if (ActiveViewChanged != null)
118        ActiveViewChanged(this, new EventArgs());
119    }
120
121    public event EventHandler Changed;
122    public void FireMainFormChanged() {
123      OnMainFormChanged();
124    }
125    protected virtual void OnMainFormChanged() {
126      if (InvokeRequired)
127        Invoke((MethodInvoker)OnMainFormChanged);
128      else if (Changed != null)
129        Changed(this, new EventArgs());
130    }
131
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;
150        } else
151          return false;
152      }
153    }
154
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      }
161    }
162
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
171    public virtual void CloseAllViews() {
172      foreach (IView view in views.Keys.ToArray())
173        CloseView(view);
174    }
175    #endregion
176
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
190      views.Remove(view);
191      if (ActiveView == view)
192        ActiveView = null;
193    }
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    }
202    #endregion
203
204    #region create menu and toolbar
205    protected virtual void CreateGUI() {
206      DiscoveryService ds = new DiscoveryService();
207
208      object[] items = ds.GetInstances(userInterfaceItemType);
209      IEnumerable<MenuItemBase> toolStripMenuItems =
210        from mi in items
211        where mi is MenuItemBase
212        orderby ((MenuItemBase)mi).Position
213        select (MenuItemBase)mi;
214      foreach (MenuItemBase menuItem in toolStripMenuItems)
215        AddToolStripMenuItem(menuItem);
216
217      items = ds.GetInstances(userInterfaceItemType);
218      IEnumerable<ToolBarItemBase> toolStripButtonItems =
219        from bi in items
220        where bi is ToolBarItemBase
221        orderby ((ToolBarItemBase)bi).Position
222        select (ToolBarItemBase)bi;
223      foreach (ToolBarItemBase toolStripButtonItem in toolStripButtonItems)
224        AddToolStripButtonItem(toolStripButtonItem);
225    }
226
227    private void AddToolStripMenuItem(MenuItemBase menuItem) {
228      ToolStripMenuItem item = new ToolStripMenuItem();
229      SetToolStripItemProperties(item, menuItem);
230      menuItem.ToolStripItem = item;
231      item.ShortcutKeys = menuItem.ShortCutKeys;
232      item.DisplayStyle = menuItem.ToolStripItemDisplayStyle;
233      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
234    }
235
236    private void AddToolStripButtonItem(ToolBarItemBase buttonItem) {
237      ToolStripItem item;
238      if (buttonItem.IsDropDownButton)
239        item = new ToolStripDropDownButton();
240      else
241        item = new ToolStripButton();
242
243      SetToolStripItemProperties(item, buttonItem);
244      item.DisplayStyle = buttonItem.ToolStripItemDisplayStyle;
245      buttonItem.ToolStripItem = item;
246      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
247    }
248
249    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
250      ToolStripDropDownItem parent = null;
251      foreach (string s in structure) {
252        if (parentItems.ContainsKey(s))
253          parent = (ToolStripDropDownItem)parentItems[s];
254        else {
255          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
256          parentItems.Add(parent);
257        }
258        parentItems = parent.DropDownItems;
259      }
260      parentItems.Add(item);
261    }
262
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);
271      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
272      this.userInterfaceItems.Add(userInterfaceItem);
273    }
274
275    private void ToolStripItemClicked(object sender, EventArgs e) {
276      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
277      ((IUserInterfaceItem)item.Tag).Execute();
278    }
279    #endregion
280  }
281}
Note: See TracBrowser for help on using the repository browser.