Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2438 was 2407, checked in by swagner, 15 years ago

Minor code cleanup (#771)

File size: 8.3 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 {
34  public partial class MainFormBase : Form, IMainForm {
35    protected MainFormBase()
36      : base() {
37      InitializeComponent();
38      views = new List<IView>();
39      toolStripItems = new List<IToolStripItem>();
40      this.StatusStripText = "";
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 string StatusStripText {
64      get { return this.toolStripStatusLabel.Text; }
65      set {
66        if (InvokeRequired) {
67          Action<string> action = delegate(string s) { this.StatusStripText = s; };
68          Invoke(action, value);
69        } else
70          this.toolStripStatusLabel.Text = value;
71      }
72    }
73
74    public bool StatusStripProgressBarVisible {
75      get { return this.toolStripProgressBar.Visible; }
76      set {
77        if (InvokeRequired) {
78          Action<bool> action = delegate(bool b) { this.StatusStripProgressBarVisible = b; };
79          Invoke(action, value);
80        } else
81          this.toolStripProgressBar.Visible = value;
82      }
83    }
84
85    public override Cursor Cursor {
86      get { return base.Cursor; }
87      set {
88        if (InvokeRequired) {
89          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
90          Invoke(action, value);
91        } else
92          base.Cursor = value;
93      }
94    }
95
96    private Type userInterfaceItemType;
97    public Type UserInterfaceItemType {
98      get { return this.userInterfaceItemType; }
99      protected set { this.userInterfaceItemType = value; }
100    }
101
102    protected List<IView> views;
103    public IEnumerable<IView> Views {
104      get { return views; }
105    }
106
107    private IView activeView;
108    public IView ActiveView {
109      get { return this.activeView; }
110      protected set {
111        if (this.activeView != value) {
112          if (InvokeRequired) {
113            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
114            Invoke(action, value);
115          } else {
116            this.activeView = value;
117            OnActiveViewChanged();
118          }
119        }
120      }
121    }
122
123    private List<IToolStripItem> toolStripItems;
124    protected IEnumerable<IToolStripItem> ToolStripItems {
125      get { return this.toolStripItems; }
126    }
127
128    public event EventHandler ActiveViewChanged;
129    protected virtual void OnActiveViewChanged() {
130      if (InvokeRequired)
131        Invoke((MethodInvoker)OnActiveViewChanged);
132      else if (ActiveViewChanged != null)
133        ActiveViewChanged(this, new EventArgs());
134    }
135
136    public event EventHandler MainFormChanged;
137    public void FireMainFormChanged() {
138      OnMainFormChanged();
139    }
140    protected virtual void OnMainFormChanged() {
141      if (InvokeRequired)
142        Invoke((MethodInvoker)FireMainFormChanged);
143      else if (MainFormChanged != null)
144        MainFormChanged(this, new EventArgs());
145    }
146
147    public virtual void ShowView(IView view) {
148      if (!views.Contains(view)) {
149        view.MainForm = this;
150        views.Add(view);
151        ActiveView = view;
152      }
153    }
154
155    public virtual void CloseView(IView view) {
156    }
157
158    public virtual void CloseAllViews() {
159      foreach (IView view in views.ToArray())
160        CloseView(view);
161    }
162
163    protected virtual void ViewClosed(IView view) {
164      views.Remove(view);
165      if (ActiveView == view)
166        ActiveView = null;
167    }
168    #endregion
169
170    #region create menu and toolbar
171    protected virtual void CreateGUI() {
172      DiscoveryService ds = new DiscoveryService();
173
174      object[] items = ds.GetInstances(userInterfaceItemType);
175      IEnumerable<IToolStripMenuItem> toolStripMenuItems =
176        from mi in items
177        where mi is IToolStripMenuItem
178        orderby ((IToolStripMenuItem)mi).Position
179        select (IToolStripMenuItem)mi;
180      foreach (IToolStripMenuItem menuItem in toolStripMenuItems)
181        AddToolStripMenuItem(menuItem);
182
183      items = ds.GetInstances(userInterfaceItemType);
184      IEnumerable<IToolStripButtonItem> toolStripButtonItems =
185        from bi in items
186        where bi is IToolStripButtonItem
187        orderby ((IToolStripButtonItem)bi).Position
188        select (IToolStripButtonItem)bi;
189      foreach (IToolStripButtonItem toolStripButtonItem in toolStripButtonItems)
190        AddToolStripButtonItem(toolStripButtonItem);
191    }
192
193    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
194      ToolStripMenuItem item = new ToolStripMenuItem();
195      SetToolStripItemProperties(item, menuItem);
196      item.ShortcutKeys = menuItem.ShortCutKeys;
197
198      ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
199      if (parent == null)
200        menuStrip.Items.Add(item);
201      else
202        parent.DropDownItems.Add(item);
203    }
204
205    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
206      ToolStripItem item;
207      if (buttonItem.IsDropDownButton)
208        item = new ToolStripDropDownButton();
209      else
210        item = new ToolStripButton();
211
212      SetToolStripItemProperties(item, buttonItem);
213      ToolStripDropDownItem parent = FindParent(buttonItem, toolStrip.Items);
214      if (parent == null)
215        toolStrip.Items.Add(item);
216      else
217        parent.DropDownItems.Add(item);
218    }
219
220    private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
221      if (String.IsNullOrEmpty(item.Structure))
222        return null;
223
224      ToolStripDropDownItem parent = null;
225      foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
226        if (parentItems.ContainsKey(structure))
227          parent = (ToolStripDropDownItem)parentItems[structure];
228        else
229          throw new ArgumentException("Structure string for item " + item.Name +
230            " is invalid. Could not find " + structure + " in toolstrip!");
231        parentItems = parent.DropDownItems;
232      }
233      return parent;
234    }
235
236    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
237      toolStripItem.Text = iToolStripItem.Name;
238      toolStripItem.Name = iToolStripItem.Name;
239      toolStripItem.Tag = iToolStripItem;
240      toolStripItem.Image = iToolStripItem.Image;
241      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
242      this.ActiveViewChanged += new EventHandler(iToolStripItem.ActiveViewChanged);
243      this.MainFormChanged += new EventHandler(iToolStripItem.MainFormChanged);
244      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
245      this.toolStripItems.Add(iToolStripItem);
246      iToolStripItem.ToolStripItem = toolStripItem;
247    }
248
249    private void ToolStripItemClicked(object sender, EventArgs e) {
250      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
251      ((IAction)item.Tag).Execute(this);
252    }
253    #endregion
254  }
255}
Note: See TracBrowser for help on using the repository browser.