Free cookie consent management tool by TermsFeed Policy Generator

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

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

added MainFormChanged event (ticket #716)

File size: 6.9 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    }
41
42    protected MainFormBase(Type userInterfaceItemType)
43      : this() {
44      this.userInterfaceItemType = userInterfaceItemType;
45      CreateGUI();
46      OnActiveViewChanged();
47    }
48
49    #region IMainForm Members
50    public string Title {
51      get { return this.Text; }
52      set {
53        if (InvokeRequired) {
54          Action<string> action = delegate(string s) { this.Title = s; };
55          Invoke(action, new object[] { value });
56        } else
57          this.Text = value;
58      }
59    }
60
61    public string StatusStripText {
62      get { return this.statusStripLabel.Text; }
63      set {
64        if (InvokeRequired) {
65          Action<string> action = delegate(string s) { this.StatusStripText = s; };
66          Invoke(action, new object[] { value });
67        } else
68          this.statusStripLabel.Text = value;
69      }
70    }
71
72    private Type userInterfaceItemType;
73    public Type UserInterfaceItemType {
74      get { return this.userInterfaceItemType; }
75      protected set { this.userInterfaceItemType = value; }
76    }
77
78    private IView activeView;
79    public IView ActiveView {
80      get { return this.activeView; }
81      protected set {
82        if (this.activeView != value) {
83          this.activeView = value;
84          OnActiveViewChanged();
85        }
86      }
87    }
88
89    private List<IToolStripItem> toolStripItems;
90    protected IEnumerable<IToolStripItem> ToolStripItems {
91      get { return this.toolStripItems; }
92    }
93
94    public event EventHandler ActiveViewChanged;
95    protected virtual void OnActiveViewChanged() {
96      if (ActiveViewChanged != null)
97        ActiveViewChanged(this, new EventArgs());
98    }
99
100    public event EventHandler MainFormChanged;
101    public void FireMainFormChanged() {
102      OnMainFormChanged();
103    }
104    protected virtual void OnMainFormChanged() {
105      if (MainFormChanged != null)
106        MainFormChanged(this, new EventArgs());
107    }
108
109    protected List<IView> views;
110    public IEnumerable<IView> Views {
111      get { return views; }
112    }
113
114    public virtual void ShowView(IView view) {
115      view.MainForm = this;
116      views.Add(view);
117      ActiveView = view;
118    }
119
120    protected virtual void ViewClosed(IView view) {
121    }
122    #endregion
123
124    #region create menu and toolbar
125    protected virtual void CreateGUI() {
126      DiscoveryService ds = new DiscoveryService();
127
128      object[] items = ds.GetInstances(userInterfaceItemType);
129      IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
130      toolStripItems = toolStripItems.OrderBy(x => x.Position);
131      foreach (IToolStripMenuItem menuItem in toolStripItems) {
132        AddToolStripMenuItem(menuItem);
133      }
134
135      items = ds.GetInstances(userInterfaceItemType);
136      toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
137      toolStripItems = toolStripItems.OrderBy(x => x.Position);
138      foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
139        AddToolStripButtonItem(toolStripButtonItem);
140      }
141    }
142
143    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
144      ToolStripMenuItem item = new ToolStripMenuItem();
145      SetToolStripItemProperties(item, menuItem);
146      item.ShortcutKeys = menuItem.ShortCutKeys;
147
148      ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
149      if (parent == null)
150        menuStrip.Items.Add(item);
151      else
152        parent.DropDownItems.Add(item);
153    }
154
155    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
156      ToolStripItem item;
157      if (buttonItem.IsDropDownButton)
158        item = new ToolStripDropDownButton();
159      else
160        item = new ToolStripButton();
161
162      SetToolStripItemProperties(item, buttonItem);
163      ToolStripDropDownItem parent = FindParent(buttonItem, toolStrip.Items);
164      if (parent == null)
165        toolStrip.Items.Add(item);
166      else
167        parent.DropDownItems.Add(item);
168    }
169
170    private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
171      if (String.IsNullOrEmpty(item.Structure))
172        return null;
173
174      ToolStripDropDownItem parent = null;
175      foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
176        if (parentItems.ContainsKey(structure))
177          parent = (ToolStripDropDownItem)parentItems[structure];
178        else
179          throw new ArgumentException("Structure string for item " + item.Name +
180            " is invalid. Could not find " + structure + " in toolstrip!");
181        parentItems = parent.DropDownItems;
182      }
183      return parent;
184    }
185
186    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
187      toolStripItem.Text = iToolStripItem.Name;
188      toolStripItem.Name = iToolStripItem.Name;
189      toolStripItem.Tag = iToolStripItem;
190      toolStripItem.Image = iToolStripItem.Image;
191      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
192      this.ActiveViewChanged += new EventHandler(iToolStripItem.ActiveViewChanged);
193      this.MainFormChanged += new EventHandler(iToolStripItem.MainFormChanged);
194      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
195      this.toolStripItems.Add(iToolStripItem);
196      iToolStripItem.ToolStripItem = toolStripItem;
197    }
198
199    private void ToolStripItemClicked(object sender, EventArgs e) {
200      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
201      ((IAction)item.Tag).Execute(this);
202    }
203    #endregion
204  }
205}
Note: See TracBrowser for help on using the repository browser.