Free cookie consent management tool by TermsFeed Policy Generator

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

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

added check to show every view only one time, second call of ShowView leads to focus of the view (ticket #716)

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