Changeset 2443


Ignore:
Timestamp:
10/20/09 15:34:02 (13 years ago)
Author:
mkommend
Message:

refactored mainform (ticket #771)

Location:
branches/Mainform refactoring
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/Actions/NewEditorAction.cs

    r2426 r2443  
    66
    77namespace HeuristicLab.MainForm.Test {
    8   public class NewEditorAction{
    9     public void Execute(IMainForm mainform) {
    10       MessageBox.Show("New Editor action called");
    11       EditorView x = new EditorView();
    12       x.Caption = "Editor View";
    13       mainform.ShowView(x);
     8  public static class NewEditorAction{
     9    private static IView view;
     10    public static void Execute(IMainForm mainform) {
     11      if (view == null)
     12        view = new EditorView();
     13      view.Caption = "Editor View";
     14      mainform.ShowView(view);
    1415    }
    1516  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/Actions/NewFormAction.cs

    r2437 r2443  
    88  public class NewFormAction {
    99    public void Execute(IMainForm mainform) {
    10       MessageBox.Show("New form called");
    1110      FormView1 x = new FormView1();
    1211      x.Caption = "FormView";
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/ButtonItems/NewEditorToolStripButtonItem.cs

    r2433 r2443  
    2424
    2525    public override void Execute() {
    26       new NewEditorAction().Execute(MainFormManager.MainForm);
     26      NewEditorAction.Execute(MainFormManager.MainForm);
    2727    }
    2828  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/EditorView.cs

    r2437 r2443  
    1212namespace HeuristicLab.MainForm.Test {
    1313  [DefaultView]
    14   public partial class EditorView : ViewBase<IList> {
     14  public partial class EditorView : ViewBase<ArrayList> {
    1515    public EditorView() {
    1616      InitializeComponent();
     
    3131      //def3.ToString();
    3232
     33      MainFormManager.MainForm.HideView(this);
    3334      this.OnChanged();
    3435    }
     
    4445      }
    4546    }
    46 
    47     #region IView<string> Members
    48 
    49     public void View(string item) {
    50       throw new NotImplementedException();
    51     }
    52 
    53     #endregion
    5447  }
    5548}
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/FormView.cs

    r2437 r2443  
    88using System.Windows.Forms;
    99using HeuristicLab.MainForm.WindowsForms;
     10using System.Collections;
    1011
    1112namespace HeuristicLab.MainForm.Test {
    1213  [DefaultView]
    13   public partial class FormView1 : ViewBase<ICloneable> {
     14  public partial class FormView1 : ViewBase<ICollection> {
    1415    private int[] array;
    1516    public FormView1() {
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/FormView2.cs

    r2437 r2443  
    1010
    1111namespace HeuristicLab.MainForm.Test {
    12   public partial class FormView2 : ViewBase<ArrayList>  {
     12  public partial class FormView2 : ViewBase<IEnumerable>  {
    1313    private int[] array;
    1414    public FormView2() {
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/MenuItems/NewEditorToolStripMenuItem.cs

    r2433 r2443  
    2323
    2424    public override void Execute() {
    25       new NewEditorAction().Execute(MainFormManager.MainForm);
     25      NewEditorAction.Execute(MainFormManager.MainForm);
    2626    }
    2727  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/DockingMainForm.cs

    r2434 r2443  
    4242    }
    4343
    44     public override void ShowView(IView view) {
    45       if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
     44    public override bool ShowView(IView view) {
     45      if (InvokeRequired) return (bool)Invoke((Func<IView, bool>)ShowView, view);
    4646      else {
    47         if (views.Contains(view)) {
    48           DockForm dockform = FindForm(view);
    49           if (dockform != null)
    50             dockform.Activate();
    51         } else {
    52           base.ShowView(view);
    53           DockContent dockForm = new DockForm(view);
    54           dockForm.Activated += new EventHandler(DockFormActivated);
    55           dockForm.GotFocus += new EventHandler(DockFormActivated);
    56           dockForm.FormClosing += new FormClosingEventHandler(view.OnClosing);
    57           dockForm.FormClosed += new FormClosedEventHandler(view.OnClosed);
    58           dockForm.FormClosed += new FormClosedEventHandler(DockFormClosed);
    59           foreach (IUserInterfaceItem item in UserInterfaceItems)
    60             view.Changed += new EventHandler(item.ViewChanged);
    61           dockForm.Show(dockPanel);
    62         }
     47        bool ret = base.ShowView(view);
     48        if (ret)
     49          ((DockForm)GetForm(view)).Show(dockPanel);
     50        else
     51          ((DockForm)GetForm(view)).Activate();
     52        return ret;
     53      }
     54    }
     55    public override void HideView(IView view) {
     56      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
     57      else {
     58        Form form = base.GetForm(view);
     59        if (form != null)
     60          ((DockForm)form).Hide();
    6361      }
    6462    }
    6563
    66     public override void CloseView(IView view) {
    67       if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
    68       else {
    69         DockForm dockform = FindForm(view);
    70         if (dockform != null)
    71           dockform.Close();
    72       }
     64    protected override Form CreateForm(IView view) {
     65      return new DockForm(view);
    7366    }
    7467
    75     private void DockFormClosed(object sender, FormClosedEventArgs e) {
    76       DockForm dockForm = (DockForm)sender;
    77       ViewClosed(dockForm.View);
    78       dockForm.Activated -= new EventHandler(DockFormActivated);
    79       dockForm.GotFocus -= new EventHandler(DockFormActivated);
    80       dockForm.FormClosing -= new FormClosingEventHandler(dockForm.View.OnClosing);
    81       dockForm.FormClosed -= new FormClosedEventHandler(dockForm.View.OnClosed);
    82       dockForm.FormClosed -= new FormClosedEventHandler(DockFormClosed);
    83       foreach (IUserInterfaceItem item in UserInterfaceItems)
    84         dockForm.View.Changed -= new EventHandler(item.ViewChanged);
    85     }
    86 
    87     private void DockFormActivated(object sender, EventArgs e) {
    88       base.ActiveView = ((DockForm)sender).View;
    89     }
    90 
    91     protected DockForm FindForm(IView view) {
    92       IEnumerable<DockForm> dockforms;
    93 
    94       dockforms = from df in dockPanel.Documents
    95                   where ((DockForm)df).View == view
    96                   select (DockForm)df;
    97       if (dockforms.Count() == 1)
    98         return dockforms.Single();
    99 
    100       dockforms = from fw in dockPanel.FloatWindows
    101                   from np in fw.NestedPanes
    102                   from dc in np.Contents
    103                   where ((DockForm)dc).View == view
    104                   select (DockForm)dc;
    105       if (dockforms.Count() == 1)
    106         return dockforms.Single();
    107 
    108       dockforms = from dw in dockPanel.DockWindows
    109                   from np in dw.NestedPanes
    110                   from dc in np.Contents
    111                   where ((DockForm)dc).View == view
    112                   select (DockForm)dc;
    113       if (dockforms.Count() == 1)
    114         return dockforms.Single();
    115 
    116       return null;
    117     }
    11868  }
    11969}
  • branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/MainFormBase.cs

    r2437 r2443  
    3636      : base() {
    3737      InitializeComponent();
    38       this.views = new List<IView>();
     38      this.views = new Dictionary<IView, Form>();
    3939      this.userInterfaceItems = new List<IUserInterfaceItem>();
    4040      MainFormManager.RegisterMainForm(this);
     
    7777    }
    7878
    79     protected List<IView> views;
     79    private Dictionary<IView, Form> views;
    8080    public IEnumerable<IView> Views {
    81       get { return views; }
     81      get { return views.Keys; }
     82    }
     83
     84    public Form GetForm(IView view) {
     85      if (views.ContainsKey(view))
     86        return views[view];
     87      return null;
    8288    }
    8389
     
    117123    protected virtual void OnMainFormChanged() {
    118124      if (InvokeRequired)
    119         Invoke((MethodInvoker)FireMainFormChanged);
     125        Invoke((MethodInvoker)OnMainFormChanged);
    120126      else if (Changed != null)
    121127        Changed(this, new EventArgs());
    122128    }
    123129
    124     public virtual void ShowView(IView view) {
    125       if (!views.Contains(view)) {
    126         views.Add(view);
    127       }
    128       this.ActiveView = view;
    129     }
    130 
    131     public virtual void CloseView(IView view) {
     130    protected virtual Form CreateForm(IView view) {
     131      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainFormBase.");
     132    }
     133
     134    public virtual bool ShowView(IView view) {
     135      if (InvokeRequired) return (bool)Invoke((Func<IView, bool>)ShowView, view);
     136      else {
     137        if (!views.Keys.Contains(view)) {
     138          Form form = CreateForm(view);
     139          views[view] = form;
     140          form.Activated += new EventHandler(FormActivated);
     141          form.GotFocus += new EventHandler(FormActivated);
     142          form.FormClosing += new FormClosingEventHandler(view.OnClosing);
     143          form.FormClosed += new FormClosedEventHandler(view.OnClosed);
     144          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
     145          foreach (IUserInterfaceItem item in UserInterfaceItems)
     146            view.Changed += new EventHandler(item.ViewChanged);
     147          return true;
     148        } else
     149          return false;
     150      }
     151    }
     152
     153    public virtual void HideView(IView view) {
     154      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
     155      else {
     156        if (views.ContainsKey(view))
     157          views[view].Hide();
     158      }
     159    }
     160
     161    public void CloseView(IView view) {
     162      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
     163      else {
     164        if (views.ContainsKey(view))
     165          views[view].Close();
     166      }
    132167    }
    133168
    134169    public virtual void CloseAllViews() {
    135       foreach (IView view in views.ToArray())
     170      foreach (IView view in views.Keys.ToArray())
    136171        CloseView(view);
    137172    }
    138 
    139     protected virtual void ViewClosed(IView view) {
     173    #endregion
     174
     175    #region events
     176    private void ChildFormClosed(object sender, FormClosedEventArgs e) {
     177      Form form = (Form)sender;
     178      IView view = GetViewForForm(form);
     179
     180      form.Activated -= new EventHandler(FormActivated);
     181      form.GotFocus -= new EventHandler(FormActivated);
     182      form.FormClosing -= new FormClosingEventHandler(view.OnClosing);
     183      form.FormClosed -= new FormClosedEventHandler(view.OnClosed);
     184      form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
     185      foreach (IUserInterfaceItem item in UserInterfaceItems)
     186        view.Changed -= new EventHandler(item.ViewChanged);
     187
    140188      views.Remove(view);
    141189      if (ActiveView == view)
    142190        ActiveView = null;
     191    }
     192
     193    private void FormActivated(object sender, EventArgs e) {
     194      this.ActiveView = GetViewForForm((Form)sender);
     195    }
     196
     197    private IView GetViewForForm(Form form) {
     198      return views.Where(x => x.Value == form).Single().Key;
    143199    }
    144200    #endregion
     
    189245    }
    190246
    191     private void InsertItem(IEnumerable<string> structure, Type t,ToolStripItem item, ToolStripItemCollection parentItems) {
     247    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
    192248      ToolStripDropDownItem parent = null;
    193249      foreach (string s in structure) {
     
    195251          parent = (ToolStripDropDownItem)parentItems[s];
    196252        else {
    197           parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s,null,null,s); ;
     253          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
    198254          parentItems.Add(parent);
    199255        }
  • branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/MultipleDocumentMainForm.Designer.cs

    r2426 r2443  
    1414        components.Dispose();
    1515      }
    16       this.MdiChildActivate -= new System.EventHandler(MultipleDocumentFormActivated);
    1716      base.Dispose(disposing);
    1817    }
  • branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/MultipleDocumentMainForm.cs

    r2434 r2443  
    4848    }
    4949
    50     public override void ShowView(IView view) {
    51       if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
     50    public override bool ShowView(IView view) {
     51      if (InvokeRequired) return (bool)Invoke((Func<IView, bool>)ShowView, view);
    5252      else {
    53         if (views.Contains(view)) {
    54           DocumentForm documentForm = FindForm(view);
    55           if (documentForm != null)
    56             documentForm.Focus();
    57         } else {
    58           base.ShowView(view);
    59           DocumentForm form = new DocumentForm(view);
    60           form.Activated += new EventHandler(MultipleDocumentFormActivated);
    61           form.GotFocus += new EventHandler(MultipleDocumentFormActivated);
    62           form.FormClosing += new FormClosingEventHandler(view.OnClosing);
    63           form.FormClosed += new FormClosedEventHandler(view.OnClosed);
    64           form.FormClosed += new FormClosedEventHandler(MultipleDocumentFormClosed);
    65           form.MdiParent = this;
    66           foreach (IUserInterfaceItem item in UserInterfaceItems)
    67             view.Changed += new EventHandler(item.ViewChanged);
    68           form.Show();
     53        bool ret = base.ShowView(view);
     54        if (ret)
     55          GetForm(view).Show();
     56        else {
     57          GetForm(view).Visible = true;
     58          GetForm(view).Activate();
    6959        }
     60        return ret;
    7061      }
    7162    }
    7263
    73     public override void CloseView(IView view) {
    74       if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
    75       else {
    76         DocumentForm documentForm = FindForm(view);
    77         if (documentForm != null)
    78           documentForm.Close();
    79       }
    80     }
    81 
    82     private void MultipleDocumentFormActivated(object sender, EventArgs e) {
    83       base.ActiveView = ((DocumentForm)sender).View;
    84     }
    85 
    86     private void MultipleDocumentFormClosed(object sender, FormClosedEventArgs e) {
    87       DocumentForm form = (DocumentForm)sender;
    88       ViewClosed(form.View);
    89       form.Activated -= new EventHandler(MultipleDocumentFormActivated);
    90       form.GotFocus -= new EventHandler(MultipleDocumentFormActivated);
    91       form.FormClosing -= new FormClosingEventHandler(form.View.OnClosing);
    92       form.FormClosed -= new FormClosedEventHandler(form.View.OnClosed);
    93       form.FormClosed -= new FormClosedEventHandler(MultipleDocumentFormClosed);
    94       foreach (IUserInterfaceItem item in UserInterfaceItems)
    95         form.View.Changed -= new EventHandler(item.ViewChanged);
    96     }
    97 
    98     protected DocumentForm FindForm(IView view) {
    99       IEnumerable<DocumentForm> forms =
    100         from df in MdiChildren
    101         where ((DocumentForm)df).View == view
    102         select (DocumentForm)df;
    103       if (forms.Count() == 1)
    104         return forms.Single();
    105       return null;
     64    protected override Form CreateForm(IView view) {
     65      Form form = new DocumentForm(view);
     66      form.MdiParent = this;
     67      return form;
    10668    }
    10769  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/SingleDocumentMainForm.cs

    r2434 r2443  
    4242    }
    4343
    44     public override void ShowView(IView view) {
    45       if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
     44    public override bool ShowView(IView view) {
     45      if (InvokeRequired) return (bool)Invoke((Func<IView, bool>)ShowView, view);
    4646      else {
    47         if (views.Contains(view)) {
    48           DocumentForm documentForm = FindForm(view);
    49           if (documentForm != null)
    50             documentForm.Focus();
    51         } else {
    52           base.ShowView(view);
    53           DocumentForm form = new DocumentForm(view);
    54           form.ShowInTaskbar = true;
    55           form.Activated += new EventHandler(DocumentFormActivated);
    56           form.GotFocus += new EventHandler(DocumentFormActivated);
    57           form.FormClosing += new FormClosingEventHandler(view.OnClosing);
    58           form.FormClosed += new FormClosedEventHandler(view.OnClosed);
    59           form.FormClosed += new FormClosedEventHandler(DocumentFormClosed);
    60           foreach (IUserInterfaceItem item in UserInterfaceItems)
    61             view.Changed += new EventHandler(item.ViewChanged);
    62           form.Show(this);
     47        bool ret = base.ShowView(view);
     48        if (ret)
     49          GetForm(view).Show(this);
     50        else {
     51          GetForm(view).Visible = true;
     52          GetForm(view).Activate();
    6353        }
     54        return ret;
    6455      }
    6556    }
    6657
    67     public override void CloseView(IView view) {
    68       if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
    69       else {
    70         DocumentForm documentForm = FindForm(view);
    71         if (documentForm != null)
    72           documentForm.Close();
    73       }
    74     }
    75 
    76     private void DocumentFormClosed(object sender, FormClosedEventArgs e) {
    77       DocumentForm form = (DocumentForm)sender;
    78       ViewClosed(form.View);
    79       form.Activated -= new EventHandler(DocumentFormActivated);
    80       form.GotFocus -= new EventHandler(DocumentFormActivated);
    81       form.FormClosing -= new FormClosingEventHandler(form.View.OnClosing);
    82       form.FormClosed -= new FormClosedEventHandler(form.View.OnClosed);
    83       form.FormClosed -= new FormClosedEventHandler(DocumentFormClosed);
    84       foreach (IUserInterfaceItem item in UserInterfaceItems)
    85         form.View.Changed -= new EventHandler(item.ViewChanged);
    86     }
    87 
    88     private void DocumentFormActivated(object sender, EventArgs e) {
    89       base.ActiveView = ((DocumentForm)sender).View;
    90     }
    91 
    92     protected DocumentForm FindForm(IView view) {
    93       IEnumerable<DocumentForm> forms =
    94         from df in OwnedForms
    95         where ((DocumentForm)df).View == view
    96         select (DocumentForm)df;
    97       if (forms.Count() == 1)
    98         return forms.Single();
    99       return null;
     58    protected override Form CreateForm(IView view) {
     59      DocumentForm form = new DocumentForm(view);
     60      form.ShowInTaskbar = true;
     61      return form;
    10062    }
    10163  }
  • branches/Mainform refactoring/HeuristicLab.MainForm/3.2/Interfaces/IMainForm.cs

    r2426 r2443  
    3636
    3737    Type UserInterfaceItemType { get; }
    38     void ShowView(IView view);
     38    bool ShowView(IView view);  //return value indicates if a new form for the view is created
     39    void HideView(IView view);
    3940    void CloseView(IView view);
    4041    void CloseAllViews();
  • branches/Mainform refactoring/HeuristicLab.MainForm/3.2/MainFormManager.cs

    r2438 r2443  
    113113            if (t1.IsAssignableFrom(t2))
    114114              return 1;
    115             return -1;
     115            else if (t2.IsAssignableFrom(t1))
     116              return -1;
     117            else
     118              return 0;
    116119          }
    117120          );
     
    121124            throw new Exception("Could not determine which is the default view for type " + viewableType.ToString() + ".");
    122125        }
    123       }     
     126      }
    124127    }
    125128
Note: See TracChangeset for help on using the changeset viewer.