Free cookie consent management tool by TermsFeed Policy Generator

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

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

tried to add docking views - not working right now (ticket #716)

File size: 5.2 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
75    public void Close() {
76      ((Form)this).Close();
77    }
78    #endregion
79
80    #region create menu and toolbar
81    private void CreateGUI() {
82      DiscoveryService ds = new DiscoveryService();
83
84      object[] items = ds.GetInstances(userInterfaceItemType);
85      IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
86      toolStripItems = toolStripItems.OrderBy(x => x.Position);
87      foreach (IToolStripMenuItem menuItem in toolStripItems) {
88        AddToolStripMenuItem(menuItem);
89      }
90
91      items = ds.GetInstances(userInterfaceItemType);
92      toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
93      toolStripItems = toolStripItems.OrderBy(x => x.Position);
94      foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
95        AddToolStripButtonItem(toolStripButtonItem);
96      }     
97    }
98
99    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
100      ToolStripMenuItem item = new ToolStripMenuItem();
101      SetToolStripItemProperties(item, menuItem);
102      item.ShortcutKeys = menuItem.ShortCutKeys;
103
104      ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
105      if (parent == null)
106        menuStrip.Items.Add(item);
107      else
108        parent.DropDownItems.Add(item);
109    }
110
111    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
112      ToolStripItem item;
113      if (buttonItem.IsDropDownButton)
114        item = new ToolStripDropDownButton();
115      else
116        item = new ToolStripButton();
117
118      SetToolStripItemProperties(item, buttonItem);
119      ToolStripDropDownItem parent = FindParent(buttonItem,toolStrip.Items);
120      if (parent == null)
121        toolStrip.Items.Add(item);
122      else
123        parent.DropDownItems.Add(item);
124    }
125
126    private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
127      ToolStripDropDownItem parent = null;
128      foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
129        if (parentItems.ContainsKey(structure))
130          parent = (ToolStripDropDownItem)parentItems[structure];
131        else
132          break;
133        parentItems = parent.DropDownItems;
134      }
135      return parent;
136    }
137
138    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
139      toolStripItem.Text = iToolStripItem.Name;
140      toolStripItem.Name = iToolStripItem.Name;
141      toolStripItem.Tag = iToolStripItem;
142      toolStripItem.Image = iToolStripItem.Image;
143      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
144      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
145      iToolStripItem.ToolStripItem = toolStripItem;
146    }
147
148    private void ToolStripItemClicked(object sender, EventArgs e) {
149      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
150      ((IAction)item.Tag).Execute(this);
151    }
152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.