Free cookie consent management tool by TermsFeed Policy Generator

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

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

added CloseView methods with CloseReason (ticket #771)

File size: 11.1 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 Changed;
133    public void FireMainFormChanged() {
134      OnMainFormChanged();
135    }
136    protected virtual void OnMainFormChanged() {
137      if (InvokeRequired)
138        Invoke((MethodInvoker)OnMainFormChanged);
139      else if (Changed != null)
140        Changed(this, new EventArgs());
141    }
142
143    protected virtual Form CreateForm(IView view) {
144      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainFormBase.");
145    }
146
147    public virtual bool ShowView(IView view) {
148      if (InvokeRequired) return (bool)Invoke((Func<IView, bool>)ShowView, view);
149      else {
150        if (!views.Keys.Contains(view)) {
151          Form form = CreateForm(view);
152          views[view] = form;
153          form.Activated += new EventHandler(FormActivated);
154          form.GotFocus += new EventHandler(FormActivated);
155          form.FormClosing += new FormClosingEventHandler(view.OnClosing);
156          form.FormClosing += new FormClosingEventHandler(((ViewBase)view).OnClosingHelper);
157          form.FormClosed += new FormClosedEventHandler(view.OnClosed);
158          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
159          foreach (IUserInterfaceItem item in UserInterfaceItems)
160            view.Changed += new EventHandler(item.ViewChanged);
161          return true;
162        } else
163          return false;
164      }
165    }
166
167    public virtual void HideView(IView view) {
168      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
169      else {
170        if (views.ContainsKey(view))
171          views[view].Hide();
172      }
173    }
174
175    public void CloseView(IView view) {
176      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
177      else {
178        if (views.ContainsKey(view))
179          views[view].Close();
180      }
181    }
182
183    public void CloseView(IView view, CloseReason closeReason) {
184      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
185      else {
186        if (views.ContainsKey(view)) {
187          ((ViewBase)view).closeReason = closeReason;
188          views[view].Close();
189        }
190      }
191    }
192
193    public virtual void CloseAllViews() {
194      foreach (IView view in views.Keys.ToArray())
195        CloseView(view);
196    }
197
198    public virtual void CloseAllViews(CloseReason closeReason) {
199      foreach (IView view in views.Keys.ToArray())
200        CloseView(view,closeReason);
201    }
202    #endregion
203
204    #region events
205    private void ChildFormClosed(object sender, FormClosedEventArgs e) {
206      Form form = (Form)sender;
207      IView view = GetViewForForm(form);
208
209      form.Activated -= new EventHandler(FormActivated);
210      form.GotFocus -= new EventHandler(FormActivated);
211      form.FormClosing -= new FormClosingEventHandler(view.OnClosing);
212      form.FormClosing -= new FormClosingEventHandler(((ViewBase)view).OnClosingHelper);
213      form.FormClosed -= new FormClosedEventHandler(view.OnClosed);
214      form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
215      foreach (IUserInterfaceItem item in UserInterfaceItems)
216        view.Changed -= new EventHandler(item.ViewChanged);
217
218      views.Remove(view);
219      if (ActiveView == view)
220        ActiveView = null;
221    }
222
223    private void FormActivated(object sender, EventArgs e) {
224      this.ActiveView = GetViewForForm((Form)sender);
225    }
226
227    private IView GetViewForForm(Form form) {
228      return views.Where(x => x.Value == form).Single().Key;
229    }
230    #endregion
231
232    #region create menu and toolbar
233    protected virtual void CreateGUI() {
234      DiscoveryService ds = new DiscoveryService();
235
236      object[] items = ds.GetInstances(userInterfaceItemType);
237      IEnumerable<IMenuItem> toolStripMenuItems =
238        from mi in items
239        where mi is IMenuItem
240        orderby ((IMenuItem)mi).Position
241        select (IMenuItem)mi;
242      foreach (IMenuItem menuItem in toolStripMenuItems)
243        AddToolStripMenuItem(menuItem);
244
245      items = ds.GetInstances(userInterfaceItemType);
246      IEnumerable<IToolBarItem> toolStripButtonItems =
247        from bi in items
248        where bi is IToolBarItem
249        orderby ((IToolBarItem)bi).Position
250        select (IToolBarItem)bi;
251      foreach (IToolBarItem toolStripButtonItem in toolStripButtonItems)
252        AddToolStripButtonItem(toolStripButtonItem);
253    }
254
255    private void AddToolStripMenuItem(IMenuItem menuItem) {
256      if (menuItem is MenuItemBase) {
257        ToolStripMenuItem item = new ToolStripMenuItem();
258        SetToolStripItemProperties(item, menuItem);
259        ((MenuItemBase)menuItem).ToolStripItem = item;
260        item.ShortcutKeys = ((MenuItemBase)menuItem).ShortCutKeys;
261        item.DisplayStyle = ((MenuItemBase)menuItem).ToolStripItemDisplayStyle;
262        this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
263      } else if (menuItem is MenuSeparatorItemBase) {
264        this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
265      }
266    }
267
268    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
269      ToolStripItem item = null;
270
271      if (buttonItem is ToolBarItemBase) {
272        if (((ToolBarItemBase)buttonItem).IsDropDownButton)
273          item = new ToolStripDropDownButton();
274        else
275          item = new ToolStripButton();
276
277        SetToolStripItemProperties(item, buttonItem);
278        item.DisplayStyle = ((ToolBarItemBase)buttonItem).ToolStripItemDisplayStyle;
279        ((ToolBarItemBase)buttonItem).ToolStripItem = item;
280      } else if (buttonItem is IToolBarSeparatorItem)
281        item = new ToolStripSeparator();
282
283      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
284    }
285
286    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
287      ToolStripDropDownItem parent = null;
288      foreach (string s in structure) {
289        if (parentItems.ContainsKey(s))
290          parent = (ToolStripDropDownItem)parentItems[s];
291        else {
292          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
293          parentItems.Add(parent);
294        }
295        parentItems = parent.DropDownItems;
296      }
297      parentItems.Add(item);
298    }
299
300    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IUserInterfaceItem userInterfaceItem) {
301      toolStripItem.Name = userInterfaceItem.Name;
302      toolStripItem.Text = userInterfaceItem.Name;
303      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
304      toolStripItem.Tag = userInterfaceItem;
305      toolStripItem.Image = userInterfaceItem.Image;
306      this.ActiveViewChanged += new EventHandler(userInterfaceItem.ActiveViewChanged);
307      this.Changed += new EventHandler(userInterfaceItem.MainFormChanged);
308      this.Initialized += new EventHandler(userInterfaceItem.MainFormInitialized);
309      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
310      this.userInterfaceItems.Add(userInterfaceItem);
311    }
312
313    private void ToolStripItemClicked(object sender, EventArgs e) {
314      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
315      ((IUserInterfaceItem)item.Tag).Execute();
316    }
317    #endregion
318  }
319}
Note: See TracBrowser for help on using the repository browser.