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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Threading;
|
---|
27 | using System.Reflection;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Common {
|
---|
30 | public abstract class StorableContent : Content, IStorableContent {
|
---|
31 | public StorableContent()
|
---|
32 | : base() {
|
---|
33 | this.filename = string.Empty;
|
---|
34 | }
|
---|
35 | public StorableContent(string filename)
|
---|
36 | : base() {
|
---|
37 | this.Filename = filename;
|
---|
38 | }
|
---|
39 |
|
---|
40 | private string filename;
|
---|
41 | public string Filename {
|
---|
42 | get { return this.filename; }
|
---|
43 | set {
|
---|
44 | if (this.filename != value) {
|
---|
45 | this.filename = value;
|
---|
46 | this.OnFilenameChanged();
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected abstract void Save();
|
---|
52 | void IStorableContent.Save() {
|
---|
53 | this.OnSaveOperationStarted();
|
---|
54 | Exception ex = null;
|
---|
55 | try {
|
---|
56 | this.Save();
|
---|
57 | }
|
---|
58 | catch (Exception e) {
|
---|
59 | ex = e;
|
---|
60 | }
|
---|
61 | this.OnSaveOperationFinished(ex);
|
---|
62 | }
|
---|
63 | public void Save(string filename) {
|
---|
64 | this.Filename = filename;
|
---|
65 | ((IStorableContent)this).Save();
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected virtual void SaveAsnychronous() {
|
---|
69 | ThreadPool.QueueUserWorkItem(
|
---|
70 | new WaitCallback(delegate(object arg) {
|
---|
71 | this.Save();
|
---|
72 | })
|
---|
73 | );
|
---|
74 | }
|
---|
75 | void IStorableContent.SaveAsynchronous() {
|
---|
76 | this.OnSaveOperationStarted();
|
---|
77 | this.SaveAsnychronous();
|
---|
78 | this.OnSaveOperationFinished(null);
|
---|
79 | }
|
---|
80 | public void SaveAsynchronous(string filename) {
|
---|
81 | this.Filename = filename;
|
---|
82 | ((IStorableContent)this).SaveAsynchronous();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public event EventHandler FilenameChanged;
|
---|
86 | protected virtual void OnFilenameChanged() {
|
---|
87 | EventHandler handler = FilenameChanged;
|
---|
88 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
89 | }
|
---|
90 | public event EventHandler SaveOperationStarted;
|
---|
91 | protected virtual void OnSaveOperationStarted() {
|
---|
92 | EventHandler handler = SaveOperationStarted;
|
---|
93 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
94 | }
|
---|
95 | public event EventHandler<EventArgs<Exception>> SaveOperationFinished;
|
---|
96 | protected virtual void OnSaveOperationFinished(Exception ex) {
|
---|
97 | EventHandler<EventArgs<Exception>> handler = SaveOperationFinished;
|
---|
98 | if (handler != null) handler(this, new EventArgs<Exception>(ex));
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|