Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.MainForm.WindowsForms/3.3/Views/View.cs @ 10355

Last change on this file since 10355 was 10106, checked in by jkarder, 11 years ago

#2116:

  • refactored outermost view host detection
  • fixed update logic of breadcrumb trail
  • fixed some views
File size: 8.7 KB
RevLine 
[3437]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3437]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;
[10106]23using System.Collections.Generic;
[3437]24using System.Linq;
[4068]25using System.Reflection;
[3437]26using System.Windows.Forms;
27
28namespace HeuristicLab.MainForm.WindowsForms {
29  public partial class View : UserControl, IView {
30    private bool initialized;
31    public View() {
32      InitializeComponent();
33      this.initialized = false;
34      this.isShown = false;
35      this.closeReason = CloseReason.None;
36      this.readOnly = false;
[3764]37      if (ViewAttribute.HasViewAttribute(this.GetType()))
38        this.Caption = ViewAttribute.GetViewName(this.GetType());
39      else
40        this.Caption = "View";
[3437]41    }
42
43    private string caption;
44    public string Caption {
45      get { return caption; }
46      set {
47        if (InvokeRequired) {
48          Action<string> action = delegate(string s) { this.Caption = s; };
49          Invoke(action, value);
50        } else {
51          if (value != caption) {
52            caption = value;
53            OnCaptionChanged();
54          }
55        }
56      }
57    }
58
59    private bool readOnly;
60    public virtual bool ReadOnly {
61      get { return this.readOnly; }
62      set {
63        if (InvokeRequired) {
64          Action<bool> action = delegate(bool b) { this.ReadOnly = b; };
65          Invoke(action, value);
66        } else {
67          if (value != readOnly) {
[3904]68            this.readOnly = value;
[4435]69            OnReadOnlyChanged();
[3437]70            PropertyInfo prop = typeof(IView).GetProperty("ReadOnly");
71            PropagateStateChanges(this, typeof(IView), prop);
[4519]72            SetEnabledStateOfControls();
[4435]73            OnChanged();
[3437]74          }
75        }
76      }
77    }
[4435]78
[4449]79    public new bool Enabled {
[4435]80      get { return base.Enabled; }
81      set {
82        if (base.Enabled != value) {
83          this.SuspendRepaint();
84          base.Enabled = value;
[4449]85          this.ResumeRepaint(true);
[4532]86          OnChanged();
[4435]87        }
88      }
89    }
90
[4449]91    bool IView.Enabled {
92      get { return Enabled; }
93      set { Enabled = value; }
94    }
95
[4435]96    protected override void OnEnabledChanged(EventArgs e) {
97      base.OnEnabledChanged(e);
98      if (Enabled) SetEnabledStateOfControls();
99    }
100
[3904]101    /// <summary>
102    /// This method is called if the ReadyOnly property of the View changes to update the controls of the view.
103    /// </summary>
104    protected virtual void SetEnabledStateOfControls() {
105    }
[3437]106
107    private bool isShown;
108    public bool IsShown {
109      get { return this.isShown; }
110      private set { this.isShown = value; }
111    }
112
113    public new void Show() {
114      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
115      bool firstTimeShown = mainform.GetForm(this) == null;
116
117      this.IsShown = true;
118      mainform.ShowView(this);
119      if (firstTimeShown) {
120        Form form = mainform.GetForm(this);
121        form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
122        form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
123      }
124      this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
125    }
126
127    public void Close() {
128      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
129      Form form = mainform.GetForm(this);
130      if (form != null) {
131        this.IsShown = false;
132        mainform.CloseView(this);
133      }
134    }
135
136    public void Close(CloseReason closeReason) {
137      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
138      Form form = mainform.GetForm(this);
139      if (form != null) {
140        this.IsShown = false;
141        mainform.CloseView(this, closeReason);
142      }
143    }
144
145    public new void Hide() {
146      this.IsShown = false;
147      MainFormManager.GetMainForm<MainForm>().HideView(this);
148      this.OnHidden(EventArgs.Empty);
149    }
150
151    public event EventHandler CaptionChanged;
152    protected virtual void OnCaptionChanged() {
153      if (InvokeRequired)
154        Invoke((MethodInvoker)OnCaptionChanged);
155      else {
156        EventHandler handler = CaptionChanged;
157        if (handler != null)
158          handler(this, EventArgs.Empty);
159      }
160    }
161    public event EventHandler ReadOnlyChanged;
162    protected virtual void OnReadOnlyChanged() {
163      if (InvokeRequired)
164        Invoke((MethodInvoker)OnReadOnlyChanged);
165      else {
166        EventHandler handler = ReadOnlyChanged;
167        if (handler != null)
168          handler(this, EventArgs.Empty);
169      }
170    }
[4083]171    protected virtual void PropagateStateChanges(Control control, Type type, PropertyInfo propertyInfo) {
[3437]172      if (!type.GetProperties().Contains(propertyInfo))
173        throw new ArgumentException("The specified type " + type + "implement the property " + propertyInfo.Name + ".");
174      if (!type.IsAssignableFrom(this.GetType()))
175        throw new ArgumentException("The specified type " + type + "must be the same or a base class / interface of this object.");
176      if (!propertyInfo.CanWrite)
177        throw new ArgumentException("The specified property " + propertyInfo.Name + " must have a setter.");
178
179      foreach (Control c in control.Controls) {
180        Type controlType = c.GetType();
181        PropertyInfo controlPropertyInfo = controlType.GetProperty(propertyInfo.Name, propertyInfo.PropertyType);
182        if (type.IsAssignableFrom(controlType) && controlPropertyInfo != null) {
183          var thisValue = propertyInfo.GetValue(this, null);
184          controlPropertyInfo.SetValue(c, thisValue, null);
[4435]185        } else PropagateStateChanges(c, type, propertyInfo);
[3437]186      }
187    }
188    public event EventHandler Changed;
189    protected virtual void OnChanged() {
190      if (InvokeRequired)
191        Invoke((MethodInvoker)OnChanged);
192      else {
193        EventHandler handler = Changed;
194        if (handler != null)
195          handler(this, EventArgs.Empty);
196      }
197    }
198
199    internal protected virtual void OnShown(ViewShownEventArgs e) {
200    }
201
202    internal protected virtual void OnHidden(EventArgs e) {
203    }
204
205    private CloseReason closeReason;
206    internal CloseReason CloseReason {
207      get { return this.closeReason; }
208      set { this.closeReason = value; }
209    }
210
211    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
212      FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
213      if (this.closeReason != CloseReason.None) {
214        this.OnClosing(eventArgs);
215        if (eventArgs.Cancel != e.Cancel)
216          e.Cancel = eventArgs.Cancel;
217      } else
218        this.OnClosing(e);
219      this.closeReason = CloseReason.None;
220    }
221
222    internal protected virtual void OnClosing(FormClosingEventArgs e) {
223    }
224
225    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
226      if (this.closeReason != CloseReason.None)
227        this.OnClosed(new FormClosedEventArgs(this.closeReason));
228      else
229        this.OnClosed(e);
230
231      Form form = (Form)sender;
232      form.FormClosed -= new FormClosedEventHandler(OnClosedHelper);
233      form.FormClosing -= new FormClosingEventHandler(OnClosingHelper);
234      this.closeReason = CloseReason.None;
235    }
236
237    internal protected virtual void OnClosed(FormClosedEventArgs e) {
238    }
239
240    private void View_Load(object sender, EventArgs e) {
241      if (!this.initialized && !this.DesignMode) {
242        this.OnInitialized(e);
243        this.initialized = true;
244      }
245    }
246
247    protected virtual void OnInitialized(EventArgs e) {
[4436]248      SetEnabledStateOfControls();
[3437]249    }
250
251
252    public void SuspendRepaint() {
253      if (InvokeRequired)
254        Invoke((MethodInvoker)SuspendRepaint);
255      else
256        ((Control)this).SuspendRepaint();
257    }
258    public void ResumeRepaint(bool refresh) {
259      if (InvokeRequired)
260        Invoke((Action<bool>)ResumeRepaint, refresh);
261      else
262        ((Control)this).ResumeRepaint(refresh);
263    }
[10106]264
265    public IEnumerable<T> GetParentViewsOfType<T>() where T : View {
266      for (var view = Parent; view != null; view = view.Parent)
267        if (view is View) yield return view as T;
268    }
[3437]269  }
270}
Note: See TracBrowser for help on using the repository browser.