source: trunk/sources/HeuristicLab.Common/3.3/Content/StorableContent.cs @ 3406

Last change on this file since 3406 was 3406, checked in by mkommend, 11 years ago

added implementation of propagate changes and save asynchronous (ticket #980)

File size: 4.4 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Threading;
27using System.Reflection;
28
29namespace HeuristicLab.Common {
30  public abstract class StorableContent : Content, IStorableContent {
31    public StorableContent()
32      : base() {
33      this.filename = string.Empty;
34      this.saveEnabled = true;
35    }
36    public StorableContent(bool saveEnabled)
37      : this() {
38      this.saveEnabled = saveEnabled;
39      //NOTE: important do not call propagate changes, because derived objects are not constructed
40    }
41    public StorableContent(bool saveEnabled, string filename)
42      : this(saveEnabled) {
43      this.Filename = filename;
44    }
45
46    private string filename;
47    public string Filename {
48      get { return this.filename; }
49      set {
50        if (this.filename != value) {
51          this.filename = value;
52          this.OnFilenameChanged();
53        }
54      }
55    }
56
57    private bool saveEnabled;
58    public virtual bool SaveEnabled {
59      get { return this.saveEnabled; }
60      set {
61        if (this.saveEnabled != value) {
62          this.saveEnabled = value;
63          this.PropagateSaveEnabledChanges();
64          this.OnSaveEnabledChanged();
65        }
66      }
67    }
68
69    protected void PropagateSaveEnabledChanges() {
70      Type type = this.GetType();
71      foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Default)) {
72        if (typeof(IStorableContent).IsAssignableFrom(fieldInfo.GetType())) {
73          IStorableContent storableContent = (IStorableContent)fieldInfo.GetValue(this);
74          storableContent.SaveEnabled = this.saveEnabled;
75        }
76      }
77    }
78
79    protected abstract void Save();
80    void IStorableContent.Save() {
81      if (this.SaveEnabled) {
82        this.OnSaveOperationStarted();
83        this.Save();
84        this.OnSaveOperationFinished(null);
85      }
86    }
87    public void Save(string filename) {
88      this.Filename = filename;
89      ((IStorableContent)this).Save();
90    }
91
92    protected virtual void SaveAsnychronous() {
93      ThreadPool.QueueUserWorkItem(
94        new WaitCallback(delegate(object arg) {
95        try { this.Save(); }
96        catch (Exception ex) {
97          this.OnSaveOperationFinished(ex);
98        }
99      })
100      );
101    }
102    void IStorableContent.SaveAsynchronous() {
103      this.OnSaveOperationStarted();
104      this.SaveAsnychronous();
105      this.OnSaveOperationFinished(null);
106    }
107    public void SaveAsynchronous(string filename) {
108      this.Filename = filename;
109      ((IStorableContent)this).SaveAsynchronous();
110    }
111
112    public event EventHandler FilenameChanged;
113    protected virtual void OnFilenameChanged() {
114      EventHandler handler = FilenameChanged;
115      if (handler != null) handler(this, EventArgs.Empty);
116    }
117    public event EventHandler SaveEnabledChanged;
118    protected virtual void OnSaveEnabledChanged() {
119      EventHandler handler = SaveEnabledChanged;
120      if (handler != null) handler(this, EventArgs.Empty);
121    }
122    public event EventHandler SaveOperationStarted;
123    protected virtual void OnSaveOperationStarted() {
124      EventHandler handler = SaveOperationStarted;
125      if (handler != null) handler(this, EventArgs.Empty);
126    }
127    public event EventHandler<EventArgs<Exception>> SaveOperationFinished;
128    protected virtual void OnSaveOperationFinished(Exception ex) {
129      EventHandler<EventArgs<Exception>> handler = SaveOperationFinished;
130      if (handler != null) handler(this, new EventArgs<Exception>(ex));
131    }
132
133  }
134}
Note: See TracBrowser for help on using the repository browser.