Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.MainForm.WindowsForms/3.3/MainForm.cs @ 7716

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

corrected opening of views from the RunCollectionView (ticket #893)

File size: 14.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
32using System.Collections;
33using WeifenLuo.WinFormsUI.Docking;
34using HeuristicLab.Common;
35
36namespace HeuristicLab.MainForm.WindowsForms {
37  public partial class MainForm : Form, IMainForm {
38    private bool initialized;
39
40    protected MainForm()
41      : base() {
42      InitializeComponent();
43      this.views = new Dictionary<IView, Form>();
44      this.userInterfaceItems = new List<IUserInterfaceItem>();
45      this.initialized = false;
46      this.showContentInViewHost = false;
47    }
48
49    protected MainForm(Type userInterfaceItemType)
50      : this() {
51      this.userInterfaceItemType = userInterfaceItemType;
52    }
53
54    #region properties
55    private bool showContentInViewHost;
56    public bool ShowContentInViewHost {
57      get { return this.showContentInViewHost; }
58      set { this.showContentInViewHost = value; }
59    }
60
61    public string Title {
62      get { return this.Text; }
63      set {
64        if (InvokeRequired) {
65          Action<string> action = delegate(string s) { this.Title = s; };
66          Invoke(action, value);
67        } else
68          this.Text = value;
69      }
70    }
71
72    public override Cursor Cursor {
73      get { return base.Cursor; }
74      set {
75        if (InvokeRequired) {
76          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
77          Invoke(action, value);
78        } else
79          base.Cursor = value;
80      }
81    }
82
83    private Type userInterfaceItemType;
84    public Type UserInterfaceItemType {
85      get { return this.userInterfaceItemType; }
86    }
87
88    private Dictionary<IView, Form> views;
89    public IEnumerable<IView> Views {
90      get { return views.Keys; }
91    }
92
93    private IView activeView;
94    public IView ActiveView {
95      get { return this.activeView; }
96      protected set {
97        if (this.activeView != value) {
98          if (InvokeRequired) {
99            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
100            Invoke(action, value);
101          } else {
102            this.activeView = value;
103            OnActiveViewChanged();
104          }
105        }
106      }
107    }
108
109    private List<IUserInterfaceItem> userInterfaceItems;
110    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
111      get { return this.userInterfaceItems; }
112    }
113    #endregion
114
115    #region events
116    public event EventHandler ActiveViewChanged;
117    protected virtual void OnActiveViewChanged() {
118      if (InvokeRequired)
119        Invoke((MethodInvoker)OnActiveViewChanged);
120      else if (ActiveViewChanged != null)
121        ActiveViewChanged(this, EventArgs.Empty);
122    }
123
124    public event EventHandler<ViewEventArgs> ViewClosed;
125    protected virtual void OnViewClosed(IView view) {
126      if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
127      else {
128        EventHandler<ViewEventArgs> handler = ViewClosed;
129        if (handler != null)
130          handler(this, new ViewEventArgs(view));
131      }
132    }
133
134    public event EventHandler<ViewShownEventArgs> ViewShown;
135    protected virtual void OnViewShown(IView view, bool firstTimeShown) {
136      if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
137      else {
138        EventHandler<ViewShownEventArgs> handler = ViewShown;
139        if (handler != null)
140          handler(this, new ViewShownEventArgs(view, firstTimeShown));
141      }
142    }
143
144    public event EventHandler<ViewEventArgs> ViewHidden;
145    protected virtual void OnViewHidden(IView view) {
146      if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
147      else {
148        EventHandler<ViewEventArgs> handler = ViewHidden;
149        if (handler != null)
150          handler(this, new ViewEventArgs(view));
151      }
152    }
153
154    public event EventHandler Changed;
155    protected void OnChanged() {
156      if (InvokeRequired)
157        Invoke((MethodInvoker)OnChanged);
158      else {
159        EventHandler handler = Changed;
160        if (handler != null)
161          Changed(this, EventArgs.Empty);
162      }
163    }
164
165    private void MainFormBase_Load(object sender, EventArgs e) {
166      if (!DesignMode) {
167        MainFormManager.RegisterMainForm(this);
168        this.CreateGUI();
169        if (!this.initialized) {
170          this.initialized = true;
171          this.OnInitialized(EventArgs.Empty);
172        }
173      }
174    }
175
176    protected virtual void OnInitialized(EventArgs e) {
177    }
178
179    private void FormActivated(object sender, EventArgs e) {
180      this.ActiveView = GetView((Form)sender);
181    }
182
183    protected override void OnFormClosing(FormClosingEventArgs e) {
184      foreach (KeyValuePair<IView, Form> pair in this.views) {
185        DockForm dockForm = pair.Value as DockForm;
186        View view = pair.Key as View;
187        if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
188          view.CloseReason = CloseReason.ApplicationExitCall;
189          view.OnClosingHelper(dockForm, e);
190        }
191      }
192      base.OnFormClosing(e);
193    }
194
195    protected override void OnFormClosed(FormClosedEventArgs e) {
196      foreach (KeyValuePair<IView, Form> pair in this.views.ToList()) {
197        DockForm dockForm = pair.Value as DockForm;
198        View view = pair.Key as View;
199        if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
200          view.CloseReason = CloseReason.ApplicationExitCall;
201          view.OnClosedHelper(dockForm, e);
202          dockForm.Close();
203        }
204      }
205      base.OnFormClosed(e);
206    }
207    #endregion
208
209    #region create, get, show, hide, close views
210    protected virtual Form CreateForm(IView view) {
211      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
212    }
213
214    internal Form GetForm(IView view) {
215      if (views.ContainsKey(view))
216        return views[view];
217      return null;
218    }
219    protected IView GetView(Form form) {
220      return views.Where(x => x.Value == form).Single().Key;
221    }
222
223    public IContentView ShowContent(IContent content) {
224      if (content == null)
225        throw new ArgumentNullException("Content cannot be null.");
226      Type viewType = MainFormManager.GetDefaultViewType(content.GetType());
227      if (viewType != null)
228        return ShowContent(content, viewType);
229      return null;
230    }
231
232    public IContentView ShowContent(IContent content, Type viewType) {
233      if (content == null)
234        throw new ArgumentNullException("Content cannot be null.");
235      if (viewType == null)
236        throw new ArgumentNullException("ViewType cannot be null.");
237
238      IContentView view;
239      if (this.ShowContentInViewHost) {
240        ViewHost viewHost = new ViewHost();
241        viewHost.ViewType = viewType;
242        view = viewHost;
243      } else
244        view = MainFormManager.CreateView(viewType);
245
246      if (view != null) {
247        view.Show();
248        view.Content = content;
249      }
250      return view;
251    }
252
253    internal void ShowView(IView view) {
254      if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
255      else {
256        Form form = GetForm(view);
257        bool firstTimeShown = form == null;
258        if (firstTimeShown) {
259          form = CreateForm(view);
260          form.Activated += new EventHandler(FormActivated);
261          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
262          view.Changed += new EventHandler(View_Changed);
263          views[view] = form;
264        }
265        this.ShowView(view, firstTimeShown);
266        this.OnViewShown(view, firstTimeShown);
267      }
268    }
269
270    private void View_Changed(object sender, EventArgs e) {
271      IView view = (IView)sender;
272      if (view == this.ActiveView)
273        this.OnActiveViewChanged();
274      this.OnChanged();
275    }
276
277    protected virtual void ShowView(IView view, bool firstTimeShown) {
278    }
279
280    internal void HideView(IView view) {
281      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
282      else {
283        this.Hide(view);
284        if (this.activeView == view)
285          this.ActiveView = null;
286        this.OnViewHidden(view);
287      }
288    }
289
290    protected virtual void Hide(IView view) {
291    }
292
293    private void ChildFormClosed(object sender, FormClosedEventArgs e) {
294      Form form = (Form)sender;
295      IView view = GetView(form);
296
297      view.Changed -= new EventHandler(View_Changed);
298      form.Activated -= new EventHandler(FormActivated);
299      form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
300
301      views.Remove(view);
302      this.OnViewClosed(view);
303      if (ActiveView == view)
304        ActiveView = null;
305    }
306
307    internal void CloseView(IView view) {
308      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
309      else if (views.ContainsKey(view)) {
310        this.views[view].Close();
311        this.OnViewClosed(view);
312      }
313    }
314
315    internal void CloseView(IView view, CloseReason closeReason) {
316      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
317      else if (views.ContainsKey(view)) {
318        ((View)view).CloseReason = closeReason;
319        this.CloseView(view);
320      }
321    }
322
323    public void CloseAllViews() {
324      foreach (IView view in views.Keys.ToArray())
325        CloseView(view);
326    }
327
328    public void CloseAllViews(CloseReason closeReason) {
329      foreach (IView view in views.Keys.ToArray())
330        CloseView(view, closeReason);
331    }
332    #endregion
333
334    #region create menu and toolbar
335    private void CreateGUI() {
336      IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
337
338      IEnumerable<IPositionableUserInterfaceItem> toolStripMenuItems =
339        from mi in allUserInterfaceItems
340        where (mi is IPositionableUserInterfaceItem) &&
341              (mi is IMenuItem || mi is IMenuSeparatorItem)
342        orderby ((IPositionableUserInterfaceItem)mi).Position
343        select (IPositionableUserInterfaceItem)mi;
344
345      foreach (IPositionableUserInterfaceItem menuItem in toolStripMenuItems) {
346        if (menuItem is IMenuItem)
347          AddToolStripMenuItem((IMenuItem)menuItem);
348        else if (menuItem is IMenuSeparatorItem)
349          AddToolStripMenuItem((IMenuSeparatorItem)menuItem);
350      }
351
352      IEnumerable<IPositionableUserInterfaceItem> toolStripButtonItems =
353        from bi in allUserInterfaceItems
354        where (bi is IPositionableUserInterfaceItem) &&
355              (bi is IToolBarItem || bi is IToolBarSeparatorItem)
356        orderby ((IPositionableUserInterfaceItem)bi).Position
357        select (IPositionableUserInterfaceItem)bi;
358
359      foreach (IPositionableUserInterfaceItem toolStripButtonItem in toolStripButtonItems) {
360        if (toolStripButtonItem is IToolBarItem)
361          AddToolStripButtonItem((IToolBarItem)toolStripButtonItem);
362        else if (toolStripButtonItem is IToolBarSeparatorItem)
363          AddToolStripButtonItem((IToolBarSeparatorItem)toolStripButtonItem);
364      }
365
366      this.AdditionalCreationOfGuiElements();
367    }
368
369    protected virtual void AdditionalCreationOfGuiElements() {
370    }
371
372    private void AddToolStripMenuItem(IMenuItem menuItem) {
373      ToolStripMenuItem item = new ToolStripMenuItem();
374      this.SetToolStripItemProperties(item, menuItem);
375      if (menuItem is MenuItem) {
376        MenuItem menuItemBase = (MenuItem)menuItem;
377        menuItemBase.ToolStripItem = item;
378        item.ShortcutKeys = menuItemBase.ShortCutKeys;
379        item.DisplayStyle = menuItemBase.ToolStripItemDisplayStyle;
380      }
381      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
382    }
383
384    private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
385      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
386    }
387
388    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
389      ToolStripItem item = new ToolStripButton();
390      if (buttonItem is ToolBarItem) {
391        ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
392        if (buttonItemBase.IsDropDownButton)
393          item = new ToolStripDropDownButton();
394
395        item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
396        buttonItemBase.ToolStripItem = item;
397      }
398
399      this.SetToolStripItemProperties(item, buttonItem);
400      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
401    }
402
403    private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
404      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
405    }
406
407    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
408      ToolStripDropDownItem parent = null;
409      foreach (string s in structure) {
410        if (parentItems.ContainsKey(s))
411          parent = (ToolStripDropDownItem)parentItems[s];
412        else {
413          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
414          parentItems.Add(parent);
415        }
416        parentItems = parent.DropDownItems;
417      }
418      parentItems.Add(item);
419    }
420
421    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
422      toolStripItem.Name = userInterfaceItem.Name;
423      toolStripItem.Text = userInterfaceItem.Name;
424      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
425      toolStripItem.Tag = userInterfaceItem;
426      toolStripItem.Image = userInterfaceItem.Image;
427      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
428      this.userInterfaceItems.Add(userInterfaceItem);
429    }
430
431    private void ToolStripItemClicked(object sender, EventArgs e) {
432      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
433      ((IActionUserInterfaceItem)item.Tag).Execute();
434    }
435    #endregion
436  }
437}
Note: See TracBrowser for help on using the repository browser.