Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/MainFormBase.cs @ 2437

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

implemented changes regarding detection of view types and default views as discussed with SWA (ticket #771)

File size: 7.4 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    protected MainFormBase()
36      : base() {
37      InitializeComponent();
38      this.views = new List<IView>();
39      this.userInterfaceItems = new List<IUserInterfaceItem>();
40      MainFormManager.RegisterMainForm(this);
41    }
42
43    protected MainFormBase(Type userInterfaceItemType)
44      : this() {
45      this.userInterfaceItemType = userInterfaceItemType;
46      CreateGUI();
47      OnActiveViewChanged();
48      FireMainFormChanged();
49    }
50
51    #region IMainForm Members
52    public string Title {
53      get { return this.Text; }
54      set {
55        if (InvokeRequired) {
56          Action<string> action = delegate(string s) { this.Title = s; };
57          Invoke(action, value);
58        } else
59          this.Text = value;
60      }
61    }
62
63    public override Cursor Cursor {
64      get { return base.Cursor; }
65      set {
66        if (InvokeRequired) {
67          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
68          Invoke(action, value);
69        } else
70          base.Cursor = value;
71      }
72    }
73
74    private Type userInterfaceItemType;
75    public Type UserInterfaceItemType {
76      get { return this.userInterfaceItemType; }
77    }
78
79    protected List<IView> views;
80    public IEnumerable<IView> Views {
81      get { return views; }
82    }
83
84    private IView activeView;
85    public IView ActiveView {
86      get { return this.activeView; }
87      protected set {
88        if (this.activeView != value) {
89          if (InvokeRequired) {
90            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
91            Invoke(action, value);
92          } else {
93            this.activeView = value;
94            OnActiveViewChanged();
95          }
96        }
97      }
98    }
99
100    private List<IUserInterfaceItem> userInterfaceItems;
101    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
102      get { return this.userInterfaceItems; }
103    }
104
105    public event EventHandler ActiveViewChanged;
106    protected virtual void OnActiveViewChanged() {
107      if (InvokeRequired)
108        Invoke((MethodInvoker)OnActiveViewChanged);
109      else if (ActiveViewChanged != null)
110        ActiveViewChanged(this, new EventArgs());
111    }
112
113    public event EventHandler Changed;
114    public void FireMainFormChanged() {
115      OnMainFormChanged();
116    }
117    protected virtual void OnMainFormChanged() {
118      if (InvokeRequired)
119        Invoke((MethodInvoker)FireMainFormChanged);
120      else if (Changed != null)
121        Changed(this, new EventArgs());
122    }
123
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) {
132    }
133
134    public virtual void CloseAllViews() {
135      foreach (IView view in views.ToArray())
136        CloseView(view);
137    }
138
139    protected virtual void ViewClosed(IView view) {
140      views.Remove(view);
141      if (ActiveView == view)
142        ActiveView = null;
143    }
144    #endregion
145
146    #region create menu and toolbar
147    protected virtual void CreateGUI() {
148      DiscoveryService ds = new DiscoveryService();
149
150      object[] items = ds.GetInstances(userInterfaceItemType);
151      IEnumerable<MenuItemBase> toolStripMenuItems =
152        from mi in items
153        where mi is MenuItemBase
154        orderby ((MenuItemBase)mi).Position
155        select (MenuItemBase)mi;
156      foreach (MenuItemBase menuItem in toolStripMenuItems)
157        AddToolStripMenuItem(menuItem);
158
159      items = ds.GetInstances(userInterfaceItemType);
160      IEnumerable<ToolBarItemBase> toolStripButtonItems =
161        from bi in items
162        where bi is ToolBarItemBase
163        orderby ((ToolBarItemBase)bi).Position
164        select (ToolBarItemBase)bi;
165      foreach (ToolBarItemBase toolStripButtonItem in toolStripButtonItems)
166        AddToolStripButtonItem(toolStripButtonItem);
167    }
168
169    private void AddToolStripMenuItem(MenuItemBase menuItem) {
170      ToolStripMenuItem item = new ToolStripMenuItem();
171      SetToolStripItemProperties(item, menuItem);
172      menuItem.ToolStripItem = item;
173      item.ShortcutKeys = menuItem.ShortCutKeys;
174      item.DisplayStyle = menuItem.ToolStripItemDisplayStyle;
175      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
176    }
177
178    private void AddToolStripButtonItem(ToolBarItemBase buttonItem) {
179      ToolStripItem item;
180      if (buttonItem.IsDropDownButton)
181        item = new ToolStripDropDownButton();
182      else
183        item = new ToolStripButton();
184
185      SetToolStripItemProperties(item, buttonItem);
186      item.DisplayStyle = buttonItem.ToolStripItemDisplayStyle;
187      buttonItem.ToolStripItem = item;
188      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
189    }
190
191    private void InsertItem(IEnumerable<string> structure, Type t,ToolStripItem item, ToolStripItemCollection parentItems) {
192      ToolStripDropDownItem parent = null;
193      foreach (string s in structure) {
194        if (parentItems.ContainsKey(s))
195          parent = (ToolStripDropDownItem)parentItems[s];
196        else {
197          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s,null,null,s); ;
198          parentItems.Add(parent);
199        }
200        parentItems = parent.DropDownItems;
201      }
202      parentItems.Add(item);
203    }
204
205    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IUserInterfaceItem userInterfaceItem) {
206      toolStripItem.Name = userInterfaceItem.Name;
207      toolStripItem.Text = userInterfaceItem.Name;
208      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
209      toolStripItem.Tag = userInterfaceItem;
210      toolStripItem.Image = userInterfaceItem.Image;
211      this.ActiveViewChanged += new EventHandler(userInterfaceItem.ActiveViewChanged);
212      this.Changed += new EventHandler(userInterfaceItem.MainFormChanged);
213      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
214      this.userInterfaceItems.Add(userInterfaceItem);
215    }
216
217    private void ToolStripItemClicked(object sender, EventArgs e) {
218      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
219      ((IUserInterfaceItem)item.Tag).Execute();
220    }
221    #endregion
222  }
223}
Note: See TracBrowser for help on using the repository browser.