Free cookie consent management tool by TermsFeed Policy Generator

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

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

added ViewClosed method to tell MainForm subclasses that a view was closed (ticket #716)

File size: 6.6 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    protected List<IView> views;
101    public IEnumerable<IView> Views {
102      get { return views; }
103    }
104
105    public virtual void ShowView(IView view) {
106      view.MainForm = this;
107      views.Add(view);
108      ActiveView = view;
109    }
110
111    public virtual void ViewClosed(IView view) {
112    }
113    #endregion
114
115    #region create menu and toolbar
116    protected virtual void CreateGUI() {
117      DiscoveryService ds = new DiscoveryService();
118
119      object[] items = ds.GetInstances(userInterfaceItemType);
120      IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
121      toolStripItems = toolStripItems.OrderBy(x => x.Position);
122      foreach (IToolStripMenuItem menuItem in toolStripItems) {
123        AddToolStripMenuItem(menuItem);
124      }
125
126      items = ds.GetInstances(userInterfaceItemType);
127      toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
128      toolStripItems = toolStripItems.OrderBy(x => x.Position);
129      foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
130        AddToolStripButtonItem(toolStripButtonItem);
131      }
132    }
133
134    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
135      ToolStripMenuItem item = new ToolStripMenuItem();
136      SetToolStripItemProperties(item, menuItem);
137      item.ShortcutKeys = menuItem.ShortCutKeys;
138
139      ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
140      if (parent == null)
141        menuStrip.Items.Add(item);
142      else
143        parent.DropDownItems.Add(item);
144    }
145
146    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
147      ToolStripItem item;
148      if (buttonItem.IsDropDownButton)
149        item = new ToolStripDropDownButton();
150      else
151        item = new ToolStripButton();
152
153      SetToolStripItemProperties(item, buttonItem);
154      ToolStripDropDownItem parent = FindParent(buttonItem, toolStrip.Items);
155      if (parent == null)
156        toolStrip.Items.Add(item);
157      else
158        parent.DropDownItems.Add(item);
159    }
160
161    private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
162      if (String.IsNullOrEmpty(item.Structure))
163        return null;
164
165      ToolStripDropDownItem parent = null;
166      foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
167        if (parentItems.ContainsKey(structure))
168          parent = (ToolStripDropDownItem)parentItems[structure];
169        else
170          throw new ArgumentException("Structure string for item " + item.Name +
171            " is invalid. Could not find " + structure + " in toolstrip!");
172        parentItems = parent.DropDownItems;
173      }
174      return parent;
175    }
176
177    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
178      toolStripItem.Text = iToolStripItem.Name;
179      toolStripItem.Name = iToolStripItem.Name;
180      toolStripItem.Tag = iToolStripItem;
181      toolStripItem.Image = iToolStripItem.Image;
182      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
183      this.ActiveViewChanged += new EventHandler(iToolStripItem.ActiveViewChanged);   
184      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
185      this.toolStripItems.Add(iToolStripItem);
186      iToolStripItem.ToolStripItem = toolStripItem;
187    }
188
189    private void ToolStripItemClicked(object sender, EventArgs e) {
190      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
191      ((IAction)item.Tag).Execute(this);
192    }
193    #endregion
194  }
195}
Note: See TracBrowser for help on using the repository browser.