Free cookie consent management tool by TermsFeed Policy Generator

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

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

added SingleDocumentMainForm
reenabled designer support for MainForms (ticket #716)

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