Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab.MainForm.WindowsForms/3.2/MainFormBase.cs @ 2587

Last change on this file since 2587 was 2587, checked in by gkronber, 14 years ago

Fixed projects to work with new plugin infrastructure. #799

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