Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/EditorBase.cs @ 2097

Last change on this file since 2097 was 1921, checked in by epitzer, 15 years ago

Refactor saving into IEditor and provide choice of compressed and uncompressed HeuristicLab archives during saving (#646)

File size: 4.0 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.Threading;
29using System.Windows.Forms;
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    /// Gets or sets, if the contained item should be compressed.
53    /// </summary>
54    public bool Compressed { get; set; }
55
56    /// <summary>
57    /// Initializes a new instance of <see cref="EditorBase"/> with the caption "Editor".
58    /// </summary>
59    public EditorBase() {
60      InitializeComponent();
61      Caption = "Editor";
62    }
63
64    /// <summary>
65    /// Asynchronously saves the contained object to a file.
66    /// </summary>
67    /// <remarks>The filename to save the contained item to is given by <see cref="Filename"/>.</remarks>
68    public void Save() {
69      Enabled = false;
70      ThreadPool.QueueUserWorkItem((o) => {
71        try {
72          if (Compressed)
73            PersistenceManager.SaveCompressed(Item, Filename);
74          else
75            PersistenceManager.Save(Item, Filename);
76        } catch (Exception e) {
77          MessageBox.Show(String.Format(
78            "Sorry couldn't save file \"{0}\".\n The following exception occurred: {1}",
79            Filename, e.ToString()),
80            "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
81        } finally {
82          Invoke(new Action(() => Enabled = true));
83          OnSaveFinished();
84        }
85      });
86    }
87
88    /// <summary>
89    /// Updates all controls with the latest data of the model.
90    /// </summary>
91    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
92    protected override void UpdateControls() {
93      base.UpdateControls();
94      if (Item == null)
95        Caption = "Editor";
96      else
97        Caption = "Editor (" + Item.GetType().Name + ")";
98    }
99
100    /// <summary>
101    /// Occurs when the filename is changed.
102    /// </summary>
103    public event EventHandler FilenameChanged;
104    /// <summary>
105    /// Fires a new <c>FilenameChanged</c> event.
106    /// </summary>
107    protected virtual void OnFilenameChanged() {
108      if (FilenameChanged != null)
109        FilenameChanged(this, new EventArgs());
110    }
111
112    /// <summary>
113    /// Occurs after a save operation is finished.
114    /// </summary>
115    public event EventHandler SaveFinished;
116
117    /// <summary>
118    /// Fires a new <c>SaveFinished</c> event.
119    /// </summary>
120    protected virtual void OnSaveFinished() {
121      SaveFinished(this, new EventArgs());
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.