Free cookie consent management tool by TermsFeed Policy Generator

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

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

first part of changes after discussion with SWA (ticket #716)

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