Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3405 was 3405, checked in by mkommend, 12 years ago

added stubs for content classes and interfaces(ticket #980)

File size: 3.8 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;
26
27namespace HeuristicLab.Common {
28  public abstract class StorableContent : Content, IStorableContent {
29    public StorableContent()
30      : base() {
31      this.filename = string.Empty;
32      this.saveEnabled = true;
33    }
34    public StorableContent(bool saveEnabled)
35      : this() {
36      this.saveEnabled = saveEnabled;
37      //NOTE: important do not call propagate changes, because derived objects are not constructed
38    }
39    public StorableContent(bool saveEnabled, string filename)
40      : this(saveEnabled) {
41      this.Filename = filename;
42    }
43
44    private string filename;
45    public string Filename {
46      get { return this.filename; }
47      set {
48        if (this.filename != value) {
49          this.filename = value;
50          this.OnFilenameChanged();
51        }
52      }
53    }
54
55    private bool saveEnabled;
56    public virtual bool SaveEnabled {
57      get { return this.saveEnabled; }
58      set {
59        if (this.saveEnabled != value) {
60          this.saveEnabled = value;
61          this.PropagateSaveEnabledChanges();
62          this.OnSaveEnabledChanged();
63        }
64      }
65    }
66
67    protected void PropagateSaveEnabledChanges() {
68      //TODO implement propagation of changes
69    }
70
71    protected abstract void Save();
72    void IStorableContent.Save() {
73      if (this.SaveEnabled) {
74        this.OnSaveOperationStarted();
75        this.Save();
76        this.OnSaveOperationFinished(null);
77      }
78    }
79    public void Save(string filename) {
80      this.Filename = filename;
81      ((IStorableContent)this).Save();
82    }
83
84    protected virtual void SaveAsnychronous() {
85      //TODO implement async call to save method
86    }
87    void IStorableContent.SaveAsynchronous() {
88      this.OnSaveOperationStarted();
89      this.SaveAsnychronous();
90      this.OnSaveOperationFinished(null);
91    }
92    public void SaveAsynchronous(string filename) {
93      this.Filename = filename;
94      ((IStorableContent)this).SaveAsynchronous();
95    }
96
97    public event EventHandler FilenameChanged;
98    protected virtual void OnFilenameChanged() {
99      EventHandler handler = FilenameChanged;
100      if (handler != null) handler(this, EventArgs.Empty);
101    }
102    public event EventHandler SaveEnabledChanged;
103    protected virtual void OnSaveEnabledChanged() {
104      EventHandler handler = SaveEnabledChanged;
105      if (handler != null) handler(this, EventArgs.Empty);
106    }
107    public event EventHandler SaveOperationStarted;
108    protected virtual void OnSaveOperationStarted() {
109      EventHandler handler = SaveOperationStarted;
110      if (handler != null) handler(this, EventArgs.Empty);
111    }
112    public event EventHandler<EventArgs<Exception>> SaveOperationFinished;
113    protected virtual void OnSaveOperationFinished(Exception ex) {
114      EventHandler<EventArgs<Exception>> handler = SaveOperationFinished;
115      if (handler != null) handler(this, new EventArgs<Exception>(ex));
116    }
117
118  }
119}
Note: See TracBrowser for help on using the repository browser.