Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/MainForms/MainForm.cs @ 9849

Last change on this file since 9849 was 9849, checked in by jkarder, 11 years ago

#1042: ProgressView overlays are now shown to indicate save operations

File size: 19.5 KB
RevLine 
[3437]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3437]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.Linq;
25using System.Windows.Forms;
[4068]26using HeuristicLab.Common;
[3437]27using HeuristicLab.PluginInfrastructure;
28using WeifenLuo.WinFormsUI.Docking;
29
30namespace HeuristicLab.MainForm.WindowsForms {
31  public partial class MainForm : Form, IMainForm {
[9849]32    private readonly Dictionary<IContent, IProgress> contentProgressLookup;
33    private readonly Dictionary<IView, IProgress> viewProgressLookup;
[3437]34    private bool initialized;
[6827]35    private int appStartingCursors;
36    private int waitingCursors;
[3437]37
38    protected MainForm()
39      : base() {
40      InitializeComponent();
41      this.views = new Dictionary<IView, Form>();
42      this.userInterfaceItems = new List<IUserInterfaceItem>();
[9849]43      this.contentProgressLookup = new Dictionary<IContent, IProgress>();
44      this.viewProgressLookup = new Dictionary<IView, IProgress>();
[3437]45      this.initialized = false;
[3557]46      this.showContentInViewHost = false;
[6827]47      appStartingCursors = 0;
48      waitingCursors = 0;
[3437]49    }
50
51    protected MainForm(Type userInterfaceItemType)
52      : this() {
53      this.userInterfaceItemType = userInterfaceItemType;
54    }
55
56    #region properties
[3557]57    private bool showContentInViewHost;
58    public bool ShowContentInViewHost {
59      get { return this.showContentInViewHost; }
60      set { this.showContentInViewHost = value; }
[3437]61    }
62
63    public string Title {
64      get { return this.Text; }
65      set {
66        if (InvokeRequired) {
67          Action<string> action = delegate(string s) { this.Title = s; };
68          Invoke(action, value);
69        } else
70          this.Text = value;
71      }
72    }
73
74    public override Cursor Cursor {
75      get { return base.Cursor; }
76      set {
77        if (InvokeRequired) {
78          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
79          Invoke(action, value);
80        } else
81          base.Cursor = value;
82      }
83    }
84
85    private Type userInterfaceItemType;
86    public Type UserInterfaceItemType {
87      get { return this.userInterfaceItemType; }
88    }
89
90    private Dictionary<IView, Form> views;
91    public IEnumerable<IView> Views {
92      get { return views.Keys; }
93    }
94
95    private IView activeView;
96    public IView ActiveView {
97      get { return this.activeView; }
98      protected set {
99        if (this.activeView != value) {
100          if (InvokeRequired) {
101            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
102            Invoke(action, value);
103          } else {
104            this.activeView = value;
105            OnActiveViewChanged();
106          }
107        }
108      }
109    }
110
111    private List<IUserInterfaceItem> userInterfaceItems;
112    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
113      get { return this.userInterfaceItems; }
114    }
115    #endregion
116
117    #region events
118    public event EventHandler ActiveViewChanged;
119    protected virtual void OnActiveViewChanged() {
120      if (InvokeRequired)
121        Invoke((MethodInvoker)OnActiveViewChanged);
[4234]122      else {
123        EventHandler handler = ActiveViewChanged;
124        if (handler != null)
125          handler(this, EventArgs.Empty);
126      }
[3437]127    }
128
129    public event EventHandler<ViewEventArgs> ViewClosed;
130    protected virtual void OnViewClosed(IView view) {
131      if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
132      else {
133        EventHandler<ViewEventArgs> handler = ViewClosed;
134        if (handler != null)
135          handler(this, new ViewEventArgs(view));
136      }
137    }
138
139    public event EventHandler<ViewShownEventArgs> ViewShown;
140    protected virtual void OnViewShown(IView view, bool firstTimeShown) {
141      if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
142      else {
143        EventHandler<ViewShownEventArgs> handler = ViewShown;
144        if (handler != null)
145          handler(this, new ViewShownEventArgs(view, firstTimeShown));
146      }
147    }
148
149    public event EventHandler<ViewEventArgs> ViewHidden;
150    protected virtual void OnViewHidden(IView view) {
151      if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
152      else {
153        EventHandler<ViewEventArgs> handler = ViewHidden;
154        if (handler != null)
155          handler(this, new ViewEventArgs(view));
156      }
157    }
158
159    public event EventHandler Changed;
[3488]160    protected void OnChanged() {
[3437]161      if (InvokeRequired)
[3488]162        Invoke((MethodInvoker)OnChanged);
[3437]163      else {
164        EventHandler handler = Changed;
165        if (handler != null)
166          Changed(this, EventArgs.Empty);
167      }
168    }
169
170    private void MainFormBase_Load(object sender, EventArgs e) {
171      if (!DesignMode) {
172        MainFormManager.RegisterMainForm(this);
[3794]173        this.CreateGUI();
[3437]174        if (!this.initialized) {
175          this.initialized = true;
176          this.OnInitialized(EventArgs.Empty);
177        }
178      }
179    }
180
[7013]181    protected virtual void OnInitialized(EventArgs e) { }
[3437]182
[7013]183    public virtual void UpdateTitle() { }
184
[3437]185    private void FormActivated(object sender, EventArgs e) {
186      this.ActiveView = GetView((Form)sender);
187    }
188
189    protected override void OnFormClosing(FormClosingEventArgs e) {
190      foreach (KeyValuePair<IView, Form> pair in this.views) {
191        DockForm dockForm = pair.Value as DockForm;
192        View view = pair.Key as View;
193        if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
194          view.CloseReason = CloseReason.ApplicationExitCall;
195          view.OnClosingHelper(dockForm, e);
196        }
197      }
198      base.OnFormClosing(e);
199    }
200
201    protected override void OnFormClosed(FormClosedEventArgs e) {
202      foreach (KeyValuePair<IView, Form> pair in this.views.ToList()) {
203        DockForm dockForm = pair.Value as DockForm;
204        View view = pair.Key as View;
205        if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
206          view.CloseReason = CloseReason.ApplicationExitCall;
207          view.OnClosedHelper(dockForm, e);
208          dockForm.Close();
209        }
210      }
211      base.OnFormClosed(e);
212    }
213    #endregion
214
215    #region create, get, show, hide, close views
216    protected virtual Form CreateForm(IView view) {
217      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
218    }
219
220    internal Form GetForm(IView view) {
[3557]221      if (views.ContainsKey(view))
222        return views[view];
[3437]223      return null;
224    }
225    protected IView GetView(Form form) {
226      return views.Where(x => x.Value == form).Single().Key;
227    }
[3557]228
229    public IContentView ShowContent(IContent content) {
[5270]230      if (content == null) throw new ArgumentNullException("Content cannot be null.");
[3670]231      Type viewType = MainFormManager.GetDefaultViewType(content.GetType());
[5270]232      if (viewType != null) return ShowContent(content, viewType);
[3670]233      return null;
234    }
235
[5270]236    public IContentView ShowContent<T>(T content, bool reuseExistingView, IEqualityComparer<T> comparer = null) where T : class,IContent {
237      if (content == null) throw new ArgumentNullException("Content cannot be null.");
238      if (!reuseExistingView) return ShowContent(content);
239
240      IContentView view = null;
241      if (comparer == null) view = Views.OfType<IContentView>().Where(v => (v.Content as T) == content).FirstOrDefault();
242      else view = Views.OfType<IContentView>().Where(v => comparer.Equals((v.Content as T), content)).FirstOrDefault();
243
244      if (view == null) view = ShowContent(content);
245      else view.Show();
246
247      return view;
248    }
249
[3796]250    public IContentView ShowContent(IContent content, Type viewType) {
[4234]251      if (InvokeRequired) return (IContentView)Invoke((Func<IContent, Type, IContentView>)ShowContent, content, viewType);
252      else {
[5270]253        if (content == null) throw new ArgumentNullException("Content cannot be null.");
254        if (viewType == null) throw new ArgumentNullException("ViewType cannot be null.");
[3670]255
[5270]256        IContentView view = null;
257        if (ShowContentInViewHost) {
[4234]258          ViewHost viewHost = new ViewHost();
259          viewHost.ViewType = viewType;
260          view = viewHost;
[3557]261
[5270]262        } else view = MainFormManager.CreateView(viewType);
263
[4234]264        view.Content = content;
265        view.Show();
266        return view;
267      }
[3437]268    }
269
270    internal void ShowView(IView view) {
271      if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
272      else {
273        Form form = GetForm(view);
274        bool firstTimeShown = form == null;
[3557]275        if (firstTimeShown) {
[3437]276          form = CreateForm(view);
277          form.Activated += new EventHandler(FormActivated);
278          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
[3557]279          view.Changed += new EventHandler(View_Changed);
280          views[view] = form;
[3437]281        }
[3557]282        this.ShowView(view, firstTimeShown);
283        this.OnViewShown(view, firstTimeShown);
[3437]284      }
285    }
286
[3488]287    private void View_Changed(object sender, EventArgs e) {
[3437]288      IView view = (IView)sender;
289      if (view == this.ActiveView)
290        this.OnActiveViewChanged();
[3488]291      this.OnChanged();
[3437]292    }
293
294    protected virtual void ShowView(IView view, bool firstTimeShown) {
295    }
296
297    internal void HideView(IView view) {
298      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
299      else {
[3557]300        this.Hide(view);
301        if (this.activeView == view)
302          this.ActiveView = null;
303        this.OnViewHidden(view);
[3437]304      }
305    }
306
307    protected virtual void Hide(IView view) {
308    }
309
310    private void ChildFormClosed(object sender, FormClosedEventArgs e) {
311      Form form = (Form)sender;
312      IView view = GetView(form);
313
[3488]314      view.Changed -= new EventHandler(View_Changed);
[3437]315      form.Activated -= new EventHandler(FormActivated);
316      form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
317
318      views.Remove(view);
319      this.OnViewClosed(view);
320      if (ActiveView == view)
321        ActiveView = null;
322    }
323
324    internal void CloseView(IView view) {
325      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
[3557]326      else if (views.ContainsKey(view)) {
327        this.views[view].Close();
328        this.OnViewClosed(view);
[3437]329      }
330    }
331
332    internal void CloseView(IView view, CloseReason closeReason) {
333      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
[3557]334      else if (views.ContainsKey(view)) {
335        ((View)view).CloseReason = closeReason;
336        this.CloseView(view);
[3437]337      }
338    }
339
340    public void CloseAllViews() {
341      foreach (IView view in views.Keys.ToArray())
342        CloseView(view);
343    }
344
345    public void CloseAllViews(CloseReason closeReason) {
346      foreach (IView view in views.Keys.ToArray())
347        CloseView(view, closeReason);
348    }
[9849]349
350    /// <summary>
351    /// Adds a <see cref="ProgressView"/> to the <see cref="ContentView"/>s showing the specified content.
352    /// </summary>
353    public void AddOperationProgressToContent(IContent content, IProgress progress, bool addToObjectGraphObjects = true) {
354      if (contentProgressLookup.ContainsKey(content))
355        throw new ArgumentException("A progress is already registered for the specified content.", "content");
356
357      var contentViews = Enumerable.Empty<IContentView>();
358      if (addToObjectGraphObjects) {
359        var containedObjects = content.GetObjectGraphObjects();
360        contentViews = views.Keys.OfType<IContentView>().Where(v => containedObjects.Contains(v.Content));
361      } else
362        contentViews = views.Keys.OfType<IContentView>().Where(v => v.Content == content);
363
364      foreach (var contentView in contentViews)
365        ProgressView.Attach(contentView, progress, true);
366
367      contentProgressLookup[content] = progress;
368    }
369
370    /// <summary>
371    /// Adds a <see cref="ProgressView"/> to the specified view.
372    /// </summary>
373    public void AddOperationProgressToView(IView view, Progress progress) {
374      if (viewProgressLookup.ContainsKey(view))
375        throw new ArgumentException("A progress is already registered for the specified view.", "view");
376
377      ProgressView.Attach(view, progress, true);
378      viewProgressLookup[view] = progress;
379    }
380
381    /// <summary>
382    /// Removes an existing <see cref="ProgressView"/> from the <see cref="ContentView"/>s showing the specified content.
383    /// </summary>
384    public void RemoveOperationProgressFromContent(IContent content) {
385      IProgress progress;
386      if (!contentProgressLookup.TryGetValue(content, out progress))
387        throw new ArgumentException("No progress is registered for the specified content.", "content");
388
389      progress.Finish();
390      contentProgressLookup.Remove(content);
391    }
392
393    /// <summary>
394    /// Removes an existing <see cref="ProgressView"/> from the specified view.
395    /// </summary>
396    public void RemoveOperationProgressFromView(IView view) {
397      IProgress progress;
398      if (!viewProgressLookup.TryGetValue(view, out progress))
399        throw new ArgumentException("No progress is registered for the specified view.", "view");
400
401      progress.Finish();
402      viewProgressLookup.Remove(view);
403    }
[3437]404    #endregion
405
406    #region create menu and toolbar
407    private void CreateGUI() {
408      IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
409
410      IEnumerable<IPositionableUserInterfaceItem> toolStripMenuItems =
411        from mi in allUserInterfaceItems
412        where (mi is IPositionableUserInterfaceItem) &&
413              (mi is IMenuItem || mi is IMenuSeparatorItem)
414        orderby ((IPositionableUserInterfaceItem)mi).Position
415        select (IPositionableUserInterfaceItem)mi;
416
417      foreach (IPositionableUserInterfaceItem menuItem in toolStripMenuItems) {
418        if (menuItem is IMenuItem)
419          AddToolStripMenuItem((IMenuItem)menuItem);
420        else if (menuItem is IMenuSeparatorItem)
421          AddToolStripMenuItem((IMenuSeparatorItem)menuItem);
422      }
423
424      IEnumerable<IPositionableUserInterfaceItem> toolStripButtonItems =
425        from bi in allUserInterfaceItems
426        where (bi is IPositionableUserInterfaceItem) &&
427              (bi is IToolBarItem || bi is IToolBarSeparatorItem)
428        orderby ((IPositionableUserInterfaceItem)bi).Position
429        select (IPositionableUserInterfaceItem)bi;
430
431      foreach (IPositionableUserInterfaceItem toolStripButtonItem in toolStripButtonItems) {
432        if (toolStripButtonItem is IToolBarItem)
433          AddToolStripButtonItem((IToolBarItem)toolStripButtonItem);
434        else if (toolStripButtonItem is IToolBarSeparatorItem)
435          AddToolStripButtonItem((IToolBarSeparatorItem)toolStripButtonItem);
436      }
437
438      this.AdditionalCreationOfGuiElements();
439    }
440
441    protected virtual void AdditionalCreationOfGuiElements() {
442    }
443
444    private void AddToolStripMenuItem(IMenuItem menuItem) {
445      ToolStripMenuItem item = new ToolStripMenuItem();
446      this.SetToolStripItemProperties(item, menuItem);
[9626]447      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
[3437]448      if (menuItem is MenuItem) {
449        MenuItem menuItemBase = (MenuItem)menuItem;
450        menuItemBase.ToolStripItem = item;
451        item.ShortcutKeys = menuItemBase.ShortCutKeys;
452        item.DisplayStyle = menuItemBase.ToolStripItemDisplayStyle;
453      }
454    }
455
456    private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
457      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
458    }
459
460    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
461      ToolStripItem item = new ToolStripButton();
462      if (buttonItem is ToolBarItem) {
463        ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
464        if (buttonItemBase.IsDropDownButton)
465          item = new ToolStripDropDownButton();
466
467        item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
468        buttonItemBase.ToolStripItem = item;
469      }
470
471      this.SetToolStripItemProperties(item, buttonItem);
472      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
473    }
474
475    private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
476      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
477    }
478
479    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
480      ToolStripDropDownItem parent = null;
481      foreach (string s in structure) {
482        if (parentItems.ContainsKey(s))
483          parent = (ToolStripDropDownItem)parentItems[s];
484        else {
485          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
486          parentItems.Add(parent);
487        }
488        parentItems = parent.DropDownItems;
489      }
490      parentItems.Add(item);
491    }
492
493    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
494      toolStripItem.Name = userInterfaceItem.Name;
495      toolStripItem.Text = userInterfaceItem.Name;
496      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
497      toolStripItem.Tag = userInterfaceItem;
498      toolStripItem.Image = userInterfaceItem.Image;
499      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
500      this.userInterfaceItems.Add(userInterfaceItem);
501    }
502
503    private void ToolStripItemClicked(object sender, EventArgs e) {
504      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
[5237]505      try {
506        ((IActionUserInterfaceItem)item.Tag).Execute();
507      }
508      catch (Exception ex) {
509        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, ex);
510      }
[3437]511    }
512    #endregion
[6827]513
514    #region Cursor Handling
515    public void SetAppStartingCursor() {
516      if (InvokeRequired)
517        Invoke(new Action(SetAppStartingCursor));
518      else {
519        appStartingCursors++;
520        SetCursor();
521      }
522    }
523    public void ResetAppStartingCursor() {
524      if (InvokeRequired)
525        Invoke(new Action(ResetAppStartingCursor));
526      else {
527        appStartingCursors--;
528        SetCursor();
529      }
530    }
531    public void SetWaitCursor() {
532      if (InvokeRequired)
533        Invoke(new Action(SetWaitCursor));
534      else {
535        waitingCursors++;
536        SetCursor();
537      }
538    }
539    public void ResetWaitCursor() {
540      if (InvokeRequired)
541        Invoke(new Action(ResetWaitCursor));
542      else {
543        waitingCursors--;
544        SetCursor();
545      }
546    }
547    private void SetCursor() {
548      if (waitingCursors > 0) Cursor = Cursors.WaitCursor;
549      else if (appStartingCursors > 0) Cursor = Cursors.AppStarting;
550      else Cursor = Cursors.Default;
551    }
552    #endregion
[3437]553  }
554}
Note: See TracBrowser for help on using the repository browser.