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

Last change on this file since 2247 was 2247, checked in by mkommend, 12 years ago

adapted HeuristicLab.MainForm.Test to use HL.Common.Resources (ticket #716)

File size: 5.0 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 abstract partial class MainFormBase : Form, IMainForm {
35    protected MainFormBase(Type userInterfaceItemType)
36      : base() {
37      InitializeComponent();
38      openViews = new List<IView>();
39      this.userInterfaceItemType = userInterfaceItemType;
40      CreateGUI();
41    }
42
43    #region IMainForm Members
44    public string Title {
45      get { return this.Text; }
46      set { this.Text = value; }
47    }
48
49    public string StatusStripText {
50      get { return this.statusStripLabel.Text; }
51      set { this.statusStripLabel.Text = value; }
52    }
53
54    protected Type userInterfaceItemType;
55    public Type UserInterfaceItemType {
56      get { return this.userInterfaceItemType; }
57    }
58
59    protected IView activeView;
60    public IView ActiveView {
61      get { return this.activeView; }
62    }
63
64    protected List<IView> openViews;
65    public IEnumerable<IView> OpenViews {
66      get { return openViews; }
67    }
68
69    public virtual void ShowView(IView view) {
70      view.MainForm = this;
71      activeView = view;
72      openViews.Add(view);
73    }
74    #endregion
75
76    #region create menu and toolbar
77    private void CreateGUI() {
78      DiscoveryService ds = new DiscoveryService();
79      Type[] userInterfaceTypes = ds.GetTypes(userInterfaceItemType);
80
81      object[] items = ds.GetInstances(userInterfaceItemType);
82      IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
83      toolStripItems = toolStripItems.OrderBy(x => x.Position);
84      foreach (IToolStripMenuItem menuItem in toolStripItems) {
85        AddToolStripMenuItem(menuItem);
86      }
87
88      items = ds.GetInstances(userInterfaceItemType);
89      toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
90      toolStripItems = toolStripItems.OrderBy(x => x.Position);
91      foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
92        AddToolStripButtonItem(toolStripButtonItem);
93      }
94    }
95
96    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
97      ToolStripMenuItem item = new ToolStripMenuItem();
98      SetToolStripItemProperties(item, menuItem);   
99      item.ShortcutKeys = menuItem.ShortCutKeys;
100
101      ToolStripMenuItem parent = null;
102      if (!String.IsNullOrEmpty(menuItem.MenuStructure)) {
103        ToolStripItemCollection parentItems = menuStrip.Items;
104        foreach (string structure in menuItem.MenuStructure.Split(menuItem.MenuStructureSeparator)) {
105          if (parentItems.ContainsKey(structure))
106            parent = (ToolStripMenuItem)parentItems[structure];
107          else {
108            parent = new ToolStripMenuItem(structure);
109            parent.Name = structure;
110            parentItems.Add(parent);
111          }
112          parentItems = parent.DropDownItems;
113        }
114      }
115
116      if (parent == null)
117        menuStrip.Items.Add(item);
118      else
119        parent.DropDownItems.Add(item);
120    }
121
122    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
123      ToolStripButton item = new ToolStripButton();
124      SetToolStripItemProperties(item, buttonItem);
125      toolStrip.Items.Add(item);
126    }
127
128    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
129      toolStripItem.Text = iToolStripItem.Name;
130      toolStripItem.Name = iToolStripItem.Name;
131      toolStripItem.Tag = iToolStripItem;
132      toolStripItem.Image = iToolStripItem.Image;
133      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
134      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
135      iToolStripItem.ToolStripItem = toolStripItem;
136    }
137
138    private void ToolStripItemClicked(object sender, EventArgs e) {
139      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
140      ((IAction)item.Tag).Execute(this);
141    }
142    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.