Free cookie consent management tool by TermsFeed Policy Generator

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

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

added progressBar to the status strip (ticket #716)

File size: 7.8 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    private Type userInterfaceItemType;
86    public Type UserInterfaceItemType {
87      get { return this.userInterfaceItemType; }
88      protected set { this.userInterfaceItemType = value; }
89    }
90
91    private IView activeView;
92    public IView ActiveView {
93      get { return this.activeView; }
94      protected set {
95        if (this.activeView != value) {
96          if (InvokeRequired) {
97            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
98            Invoke(action, new object[] { value });
99          } else {
100            this.activeView = value;
101            OnActiveViewChanged();
102          }
103        }
104      }
105    }
106
107    private List<IToolStripItem> toolStripItems;
108    protected IEnumerable<IToolStripItem> ToolStripItems {
109      get { return this.toolStripItems; }
110    }
111
112    public event EventHandler ActiveViewChanged;
113    protected virtual void OnActiveViewChanged() {
114      if (ActiveViewChanged != null)
115        ActiveViewChanged(this, new EventArgs());
116    }
117
118    public event EventHandler MainFormChanged;
119    public void FireMainFormChanged() {
120      OnMainFormChanged();
121    }
122    protected virtual void OnMainFormChanged() {
123      if (MainFormChanged != null)
124        MainFormChanged(this, new EventArgs());
125    }
126
127    protected List<IView> views;
128    public IEnumerable<IView> Views {
129      get { return views; }
130    }
131
132    public virtual void ShowView(IView view) {
133      if (!views.Contains(view)) {
134        view.MainForm = this;
135        views.Add(view);
136        ActiveView = view;
137      }
138    }
139
140    public virtual void CloseView(IView view) {
141    }
142
143    public virtual void CloseAllViews() {
144      foreach (IView view in views.ToArray())
145        CloseView(view);
146    }
147
148    protected virtual void ViewClosed(IView view) {
149    }
150    #endregion
151
152    #region create menu and toolbar
153    protected virtual void CreateGUI() {
154      DiscoveryService ds = new DiscoveryService();
155
156      object[] items = ds.GetInstances(userInterfaceItemType);
157      IEnumerable<IToolStripItem> toolStripItems = items.Where(mi => mi as IToolStripMenuItem != null).Cast<IToolStripItem>();
158      toolStripItems = toolStripItems.OrderBy(x => x.Position);
159      foreach (IToolStripMenuItem menuItem in toolStripItems) {
160        AddToolStripMenuItem(menuItem);
161      }
162
163      items = ds.GetInstances(userInterfaceItemType);
164      toolStripItems = items.Where(mi => mi as IToolStripButtonItem != null).Cast<IToolStripItem>();
165      toolStripItems = toolStripItems.OrderBy(x => x.Position);
166      foreach (IToolStripButtonItem toolStripButtonItem in toolStripItems) {
167        AddToolStripButtonItem(toolStripButtonItem);
168      }
169    }
170
171    private void AddToolStripMenuItem(IToolStripMenuItem menuItem) {
172      ToolStripMenuItem item = new ToolStripMenuItem();
173      SetToolStripItemProperties(item, menuItem);
174      item.ShortcutKeys = menuItem.ShortCutKeys;
175
176      ToolStripDropDownItem parent = FindParent(menuItem, menuStrip.Items);
177      if (parent == null)
178        menuStrip.Items.Add(item);
179      else
180        parent.DropDownItems.Add(item);
181    }
182
183    private void AddToolStripButtonItem(IToolStripButtonItem buttonItem) {
184      ToolStripItem item;
185      if (buttonItem.IsDropDownButton)
186        item = new ToolStripDropDownButton();
187      else
188        item = new ToolStripButton();
189
190      SetToolStripItemProperties(item, buttonItem);
191      ToolStripDropDownItem parent = FindParent(buttonItem, toolStrip.Items);
192      if (parent == null)
193        toolStrip.Items.Add(item);
194      else
195        parent.DropDownItems.Add(item);
196    }
197
198    private ToolStripDropDownItem FindParent(IToolStripItem item, ToolStripItemCollection parentItems) {
199      if (String.IsNullOrEmpty(item.Structure))
200        return null;
201
202      ToolStripDropDownItem parent = null;
203      foreach (string structure in item.Structure.Split(item.StructureSeparator)) {
204        if (parentItems.ContainsKey(structure))
205          parent = (ToolStripDropDownItem)parentItems[structure];
206        else
207          throw new ArgumentException("Structure string for item " + item.Name +
208            " is invalid. Could not find " + structure + " in toolstrip!");
209        parentItems = parent.DropDownItems;
210      }
211      return parent;
212    }
213
214    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IToolStripItem iToolStripItem) {
215      toolStripItem.Text = iToolStripItem.Name;
216      toolStripItem.Name = iToolStripItem.Name;
217      toolStripItem.Tag = iToolStripItem;
218      toolStripItem.Image = iToolStripItem.Image;
219      toolStripItem.DisplayStyle = iToolStripItem.DisplayStyle;
220      this.ActiveViewChanged += new EventHandler(iToolStripItem.ActiveViewChanged);
221      this.MainFormChanged += new EventHandler(iToolStripItem.MainFormChanged);
222      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
223      this.toolStripItems.Add(iToolStripItem);
224      iToolStripItem.ToolStripItem = toolStripItem;
225    }
226
227    private void ToolStripItemClicked(object sender, EventArgs e) {
228      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
229      ((IAction)item.Tag).Execute(this);
230    }
231    #endregion
232  }
233}
Note: See TracBrowser for help on using the repository browser.