Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2143 for trunk


Ignore:
Timestamp:
07/07/09 02:24:34 (15 years ago)
Author:
swagner
Message:

Refactoring of the saving mechanism for editors (#685)

Location:
trunk/sources
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.AdvancedOptimizationFrontend/3.2/MainForm.cs

    r2105 r2143  
    100100    /// is neither a view nor an editor.</exception>
    101101    /// <param name="control">The control to display.</param>
    102     delegate void ShowControlDelegate(IControl control);
    103102    public void ShowControl(IControl control) {
    104103      if (InvokeRequired) Invoke((Action<IControl>)ShowControl,control);
     
    183182        else {
    184183          editor.Filename = task.filename;
     184          editor.SaveFinished += new EventHandler(SaveFinished);
    185185          PluginManager.ControlManager.ShowControl(editor);
    186186        }
     
    198198        lock (locker) runningTasks++;
    199199        Cursor = Cursors.AppStarting;
    200         ((Control)form.Editor).Enabled = false;
    201200        EnableDisableItems();
    202         Task task = new Task(form.Editor.Filename, form.Editor.Item, form.Editor);
    203         ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousSave), task);
     201        form.Editor.Save();
    204202      }
    205203    }
     
    210208      }
    211209    }
    212     private void AsynchronousSave(object state) {
    213       Task task = (Task)state;
    214       PersistenceManager.Save(task.storable, task.filename);
    215       SaveFinished(task);
    216     }
    217     private void SaveFinished(Task task) {
     210    private void SaveFinished(object sender, EventArgs e) {
    218211      if (InvokeRequired)
    219         Invoke(new TaskFinishedDelegate(SaveFinished), task);
     212        Invoke(new EventHandler(SaveFinished), sender, e);
    220213      else {
    221         ((Control)task.editor).Enabled = true;
    222214        EnableDisableItems();
    223215        lock (locker) {
     
    246238          MessageBox.Show("The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    247239        } else {
     240          editor.SaveFinished += new EventHandler(SaveFinished);
    248241          PluginManager.ControlManager.ShowControl(editor);
    249242          EnableDisableItems();
  • trunk/sources/HeuristicLab.Core/3.2/EditorBase.cs

    r1529 r2143  
    2727using System.Text;
    2828using System.Windows.Forms;
     29using System.Threading;
    2930
    3031namespace HeuristicLab.Core {
     
    5758
    5859    /// <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>
    5987    /// Updates all controls with the latest data of the model.
    6088    /// </summary>
     
    79107        FilenameChanged(this, new EventArgs());
    80108    }
     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    }
    81120  }
    82121}
  • trunk/sources/HeuristicLab.Core/3.2/HeuristicLab.Core-3.2.csproj

    r1534 r2143  
    3232    <UseApplicationTrust>false</UseApplicationTrust>
    3333    <BootstrapperEnabled>true</BootstrapperEnabled>
     34    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    3435  </PropertyGroup>
    3536  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     
    9091  <ItemGroup>
    9192    <Reference Include="System" />
     93    <Reference Include="System.Core">
     94      <RequiredTargetFramework>3.5</RequiredTargetFramework>
     95    </Reference>
    9296    <Reference Include="System.Data" />
    9397    <Reference Include="System.Drawing" />
  • trunk/sources/HeuristicLab.Core/3.2/Interfaces/IEditor.cs

    r776 r2143  
    3636
    3737    /// <summary>
     38    /// Saves the contained item to a file.
     39    /// </summary>
     40    /// <remarks>
     41    ///   The filename to save the contained item to is given by <see cref="Filename"/>.
     42    ///   Save is an asynchronous method. After saving the contained item is finished, the
     43    ///   <see cref="SaveFinished"/> event is fired.
     44    /// </remarks>
     45    void Save();
     46
     47    /// <summary>
    3848    /// Occurs when the filename was changed.
    3949    /// </summary>
    4050    event EventHandler FilenameChanged;
     51    /// <summary>
     52    /// Occurs after saving the contained object is finished.
     53    /// </summary>
     54    event EventHandler SaveFinished;
    4155  }
    4256}
  • trunk/sources/HeuristicLab.Core/3.2/PersistenceManager.cs

    r1722 r2143  
    108108    /// <param name="filename">The name of the file where the <paramref name="object"/> should be saved.</param>
    109109    public static void Save(IStorable instance, string filename) {
    110       using(FileStream stream = File.Create(filename)) {
     110      string tempfile = Path.GetTempFileName();
     111      using (FileStream stream = File.Create(tempfile)) {
    111112        Save(instance, stream);
    112113        stream.Close();
    113114      }
     115      File.Copy(tempfile, filename, true);
     116      File.Delete(tempfile);
    114117    }
    115118    /// <summary>
Note: See TracChangeset for help on using the changeset viewer.