Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/EditorBase.cs @ 3200

Last change on this file since 3200 was 2143, checked in by swagner, 15 years ago

Refactoring of the saving mechanism for editors (#685)

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using System.Threading;
30
31namespace HeuristicLab.Core {
32  /// <summary>
33  /// Base class for views that can load and save data.
34  /// </summary>
35  public partial class EditorBase : ViewBase, IEditor {
36    private string myFilename;
37    /// <summary>
38    /// Gets or sets the filename of the current editor.
39    /// </summary>
40    /// <remarks>Calls <see cref="OnFilenameChanged"/> in the setter if the filename is new.</remarks>
41    public string Filename {
42      get { return myFilename; }
43      set {
44        if (value != myFilename) {
45          myFilename = value;
46          OnFilenameChanged();
47        }
48      }
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="EditorBase"/> with the caption "Editor".
53    /// </summary>
54    public EditorBase() {
55      InitializeComponent();
56      Caption = "Editor";
57    }
58
59    /// <summary>
60    /// Saves the contained item to a file.
61    /// </summary>
62    /// <remarks>
63    ///   The filename to save the contained item to is given by <see cref="Filename"/>.
64    ///   Save is an asynchronous method. After saving the contained item is finished, the
65    ///   <see cref="SaveFinished"/> event is fired.
66    /// </remarks>
67    public void Save() {
68      Enabled = false;
69      ThreadPool.QueueUserWorkItem((o) => {
70        try {
71          PersistenceManager.Save(Item, Filename);
72        }
73        catch (Exception e) {
74          MessageBox.Show(String.Format(
75            "Sorry couldn't save file \"{0}\".\n The following exception occurred: {1}",
76            Filename, e.ToString()),
77            "Writer Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
78        }
79        finally {
80          Invoke(new Action(() => Enabled = true));
81          OnSaveFinished();
82        }
83      });
84    }
85
86    /// <summary>
87    /// Updates all controls with the latest data of the model.
88    /// </summary>
89    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
90    protected override void UpdateControls() {
91      base.UpdateControls();
92      if (Item == null)
93        Caption = "Editor";
94      else
95        Caption = "Editor (" + Item.GetType().Name + ")";
96    }
97
98    /// <summary>
99    /// Occurs when the filename is changed.
100    /// </summary>
101    public event EventHandler FilenameChanged;
102    /// <summary>
103    /// Fires a new <c>FilenameChanged</c> event.
104    /// </summary>
105    protected virtual void OnFilenameChanged() {
106      if (FilenameChanged != null)
107        FilenameChanged(this, new EventArgs());
108    }
109    /// <summary>
110    /// Occurs when the filename was changed.
111    /// </summary>
112    public event EventHandler SaveFinished;
113    /// <summary>
114    /// Fires the <see cref="SaveFinished"/> event.
115    /// </summary>
116    protected virtual void OnSaveFinished() {
117      if (SaveFinished != null)
118        SaveFinished(this, new EventArgs());
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.