Free cookie consent management tool by TermsFeed Policy Generator

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

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

set ActiveView to null during ViewClosed, if the active view is being closed (ticket #743)

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