Free cookie consent management tool by TermsFeed Policy Generator

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

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

added possibility to programmatically close views (ticket #716)

File size: 7.1 KB
RevLine 
[2233]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
[2243]31using HeuristicLab.PluginInfrastructure;
32
[2233]33namespace HeuristicLab.MainForm {
[2268]34  public partial class MainFormBase : Form, IMainForm {
35    protected MainFormBase()
[2233]36      : base() {
37      InitializeComponent();
[2256]38      views = new List<IView>();
[2269]39      toolStripItems = new List<IToolStripItem>();
[2268]40    }
41
42    protected MainFormBase(Type userInterfaceItemType)
43      : this() {
[2233]44      this.userInterfaceItemType = userInterfaceItemType;
[2243]45      CreateGUI();
[2254]46      OnActiveViewChanged();
[2302]47      FireMainFormChanged();
[2233]48    }
49
50    #region IMainForm Members
51    public string Title {
[2243]52      get { return this.Text; }
[2256]53      set {
54        if (InvokeRequired) {
55          Action<string> action = delegate(string s) { this.Title = s; };
56          Invoke(action, new object[] { value });
57        } else
58          this.Text = value;
59      }
[2233]60    }
61
62    public string StatusStripText {
63      get { return this.statusStripLabel.Text; }
[2256]64      set {
65        if (InvokeRequired) {
[2268]66          Action<string> action = delegate(string s) { this.StatusStripText = s; };
[2256]67          Invoke(action, new object[] { value });
68        } else
69          this.statusStripLabel.Text = value;
70      }
[2233]71    }
72
[2256]73    private Type userInterfaceItemType;
[2233]74    public Type UserInterfaceItemType {
75      get { return this.userInterfaceItemType; }
[2256]76      protected set { this.userInterfaceItemType = value; }
[2233]77    }
78
[2254]79    private IView activeView;
[2233]80    public IView ActiveView {
81      get { return this.activeView; }
[2254]82      protected set {
83        if (this.activeView != value) {
84          this.activeView = value;
85          OnActiveViewChanged();
86        }
87      }
[2233]88    }
89
[2269]90    private List<IToolStripItem> toolStripItems;
91    protected IEnumerable<IToolStripItem> ToolStripItems {
92      get { return this.toolStripItems; }
[2256]93    }
[2254]94
95    public event EventHandler ActiveViewChanged;
96    protected virtual void OnActiveViewChanged() {
[2256]97      if (ActiveViewChanged != null)
98        ActiveViewChanged(this, new EventArgs());
[2254]99    }
100
[2300]101    public event EventHandler MainFormChanged;
102    public void FireMainFormChanged() {
103      OnMainFormChanged();
104    }
105    protected virtual void OnMainFormChanged() {
106      if (MainFormChanged != null)
107        MainFormChanged(this, new EventArgs());
108    }
109
[2256]110    protected List<IView> views;
111    public IEnumerable<IView> Views {
112      get { return views; }
[2233]113    }
114
115    public virtual void ShowView(IView view) {
[2256]116      view.MainForm = this;
117      views.Add(view);
[2254]118      ActiveView = view;
[2233]119    }
[2297]120
[2305]121    public virtual void CloseView(IView view) {
122    }
123
124    public virtual void CloseAllViews() {
125      foreach (IView view in views.ToArray())
126        CloseView(view);
127    }
128
[2298]129    protected virtual void ViewClosed(IView view) {
[2297]130    }
[2243]131    #endregion
[2233]132
[2243]133    #region create menu and toolbar
[2268]134    protected virtual void CreateGUI() {
[2243]135      DiscoveryService ds = new DiscoveryService();
136
[2247]137      object[] items = ds.GetInstances(userInterfaceItemType);
138      IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
139      toolStripItems = toolStripItems.OrderBy(x => x.Position);
140      foreach (IToolStripMenuItem menuItem in toolStripItems) {
141        AddToolStripMenuItem(menuItem);
[2243]142      }
143
[2247]144      items = ds.GetInstances(userInterfaceItemType);
145      toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
146      toolStripItems = toolStripItems.OrderBy(x => x.Position);
147      foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
148        AddToolStripButtonItem(toolStripButtonItem);
[2254]149      }
[2243]150    }
151
152    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
153      ToolStripMenuItem item = new ToolStripMenuItem();
[2249]154      SetToolStripItemProperties(item, menuItem);
[2243]155      item.ShortcutKeys = menuItem.ShortCutKeys;
156
[2249]157      ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
[2243]158      if (parent == null)
159        menuStrip.Items.Add(item);
160      else
161        parent.DropDownItems.Add(item);
162    }
163
164    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
[2249]165      ToolStripItem item;
166      if (buttonItem.IsDropDownButton)
167        item = new ToolStripDropDownButton();
168      else
169        item = new ToolStripButton();
170
[2243]171      SetToolStripItemProperties(item, buttonItem);
[2254]172      ToolStripDropDownItem parent = FindParent(buttonItem, toolStrip.Items);
[2249]173      if (parent == null)
174        toolStrip.Items.Add(item);
175      else
176        parent.DropDownItems.Add(item);
[2243]177    }
178
[2269]179    private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
[2268]180      if (String.IsNullOrEmpty(item.Structure))
181        return null;
182
[2249]183      ToolStripDropDownItem parent = null;
184      foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
185        if (parentItems.ContainsKey(structure))
186          parent = (ToolStripDropDownItem)parentItems[structure];
187        else
[2268]188          throw new ArgumentException("Structure string for item " + item.Name +
189            " is invalid. Could not find " + structure + " in toolstrip!");
[2249]190        parentItems = parent.DropDownItems;
191      }
192      return parent;
193    }
194
[2243]195    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
196      toolStripItem.Text = iToolStripItem.Name;
197      toolStripItem.Name = iToolStripItem.Name;
198      toolStripItem.Tag = iToolStripItem;
199      toolStripItem.Image = iToolStripItem.Image;
200      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
[2300]201      this.ActiveViewChanged += new EventHandler(iToolStripItem.ActiveViewChanged);
202      this.MainFormChanged += new EventHandler(iToolStripItem.MainFormChanged);
[2243]203      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
[2269]204      this.toolStripItems.Add(iToolStripItem);
[2243]205      iToolStripItem.ToolStripItem = toolStripItem;
206    }
207
208    private void ToolStripItemClicked(object sender, EventArgs e) {
209      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
210      ((IAction)item.Tag).Execute(this);
211    }
[2233]212    #endregion
213  }
214}
Note: See TracBrowser for help on using the repository browser.