Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm/3.2/MainFormBase.cs @ 2356

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

corrected usage of new and override keywords for inherited properties from control or form in MainFormBase (ticket #716)

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, new object[] { 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, new object[] { 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.toolStripProgressBar.Visible = b; };
79          Invoke(action, new object[] { 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, new object[] { 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    private IView activeView;
103    public IView ActiveView {
104      get { return this.activeView; }
105      protected set {
106        if (this.activeView != value) {
107          if (InvokeRequired) {
108            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
109            Invoke(action, new object[] { value });
110          } else {
111            this.activeView = value;
112            OnActiveViewChanged();
113          }
114        }
115      }
116    }
117
118    private List<IToolStripItem> toolStripItems;
119    protected IEnumerable<IToolStripItem> ToolStripItems {
120      get { return this.toolStripItems; }
121    }
122
123    public event EventHandler ActiveViewChanged;
124    protected virtual void OnActiveViewChanged() {
125      if (InvokeRequired)
126        Invoke((MethodInvoker)OnActiveViewChanged);
127      else if (ActiveViewChanged != null)
128        ActiveViewChanged(this, new EventArgs());
129    }
130
131    public event EventHandler MainFormChanged;
132    public void FireMainFormChanged() {
133      OnMainFormChanged();
134    }
135    protected virtual void OnMainFormChanged() {
136      if (InvokeRequired)
137        Invoke((MethodInvoker)FireMainFormChanged);
138      else if (MainFormChanged != null)
139        MainFormChanged(this, new EventArgs());
140    }
141
142    protected List<IView> views;
143    public IEnumerable<IView> Views {
144      get { return views; }
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    }
165    #endregion
166
167    #region create menu and toolbar
168    protected virtual void CreateGUI() {
169      DiscoveryService ds = new DiscoveryService();
170
171      object[] items = ds.GetInstances(userInterfaceItemType);
172      IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
173      toolStripItems = toolStripItems.OrderBy(x => x.Position);
174      foreach (IToolStripMenuItem menuItem in toolStripItems) {
175        AddToolStripMenuItem(menuItem);
176      }
177
178      items = ds.GetInstances(userInterfaceItemType);
179      toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
180      toolStripItems = toolStripItems.OrderBy(x => x.Position);
181      foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
182        AddToolStripButtonItem(toolStripButtonItem);
183      }
184    }
185
186    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
187      ToolStripMenuItem item = new ToolStripMenuItem();
188      SetToolStripItemProperties(item, menuItem);
189      item.ShortcutKeys = menuItem.ShortCutKeys;
190
191      ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
192      if (parent == null)
193        menuStrip.Items.Add(item);
194      else
195        parent.DropDownItems.Add(item);
196    }
197
198    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
199      ToolStripItem item;
200      if (buttonItem.IsDropDownButton)
201        item = new ToolStripDropDownButton();
202      else
203        item = new ToolStripButton();
204
205      SetToolStripItemProperties(item, buttonItem);
206      ToolStripDropDownItem parent = FindParent(buttonItem, toolStrip.Items);
207      if (parent == null)
208        toolStrip.Items.Add(item);
209      else
210        parent.DropDownItems.Add(item);
211    }
212
213    private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
214      if (String.IsNullOrEmpty(item.Structure))
215        return null;
216
217      ToolStripDropDownItem parent = null;
218      foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
219        if (parentItems.ContainsKey(structure))
220          parent = (ToolStripDropDownItem)parentItems[structure];
221        else
222          throw new ArgumentException("Structure string for item " + item.Name +
223            " is invalid. Could not find " + structure + " in toolstrip!");
224        parentItems = parent.DropDownItems;
225      }
226      return parent;
227    }
228
229    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
230      toolStripItem.Text = iToolStripItem.Name;
231      toolStripItem.Name = iToolStripItem.Name;
232      toolStripItem.Tag = iToolStripItem;
233      toolStripItem.Image = iToolStripItem.Image;
234      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
235      this.ActiveViewChanged += new EventHandler(iToolStripItem.ActiveViewChanged);
236      this.MainFormChanged += new EventHandler(iToolStripItem.MainFormChanged);
237      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
238      this.toolStripItems.Add(iToolStripItem);
239      iToolStripItem.ToolStripItem = toolStripItem;
240    }
241
242    private void ToolStripItemClicked(object sender, EventArgs e) {
243      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
244      ((IAction)item.Tag).Execute(this);
245    }
246    #endregion
247  }
248}
Note: See TracBrowser for help on using the repository browser.