Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/View.cs @ 4449

Last change on this file since 4449 was 4449, checked in by mkommend, 14 years ago

Refactored View.Enabled to hide the Control.Enabled property and additionally implement IView.Enabled explicitly (ticket #1155).

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