Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3500


Ignore:
Timestamp:
04/22/10 23:33:13 (14 years ago)
Author:
swagner
Message:

Finished refactoring of saving and loading items (#990)

Location:
trunk/sources
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Common/3.2/Content/ContentManager.cs

    r3483 r3500  
    6060    public static void Save(IStorableContent content, string filename, bool compressed) {
    6161      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    62       instance.SaveContent(content, filename, compressed);
     62      instance.SaveContent((IStorableContent)content.Clone(), filename, compressed);
    6363      content.Filename = filename;
    6464    }
    6565    public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action<IStorableContent, Exception> savingCompletedCallback) {
    6666      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    67       var action = new Action<IStorableContent, string, bool>(instance.SaveContent);
    68       action.BeginInvoke(content, filename, compressed, delegate(IAsyncResult result) {
    69         Exception error = null;
    70         try {
    71           action.EndInvoke(result);
    72           content.Filename = filename;
    73         }
    74         catch (Exception ex) {
    75           error = ex;
    76         }
    77         savingCompletedCallback(content, error);
    78       }, null);
     67
     68      IStorableContent clone = null;
     69      try {
     70        clone = (IStorableContent)content.Clone();
     71      }
     72      catch (Exception ex) {
     73        savingCompletedCallback(content, ex);
     74      }
     75
     76      if (clone != null) {
     77        var action = new Action<IStorableContent, string, bool>(instance.SaveContent);
     78        action.BeginInvoke(clone, filename, compressed, delegate(IAsyncResult result) {
     79          Exception error = null;
     80          try {
     81            action.EndInvoke(result);
     82            content.Filename = filename;
     83          }
     84          catch (Exception ex) {
     85            error = ex;
     86          }
     87          savingCompletedCallback(content, error);
     88        }, null);
     89      }
    7990    }
    8091    protected abstract void SaveContent(IStorableContent content, string filename, bool compressed);
  • trunk/sources/HeuristicLab.Common/3.2/Content/IStorableContent.cs

    r3483 r3500  
    2323
    2424namespace HeuristicLab.Common {
    25   public interface IStorableContent : IContent {
     25  public interface IStorableContent : IContent, IDeepCloneable {
    2626    string Filename { get; set; }
    2727
  • trunk/sources/HeuristicLab.Common/3.2/Content/StorableContent.cs

    r3483 r3500  
    2323
    2424namespace HeuristicLab.Common {
    25   public class StorableContent : IStorableContent {
     25  public class StorableContent : DeepCloneable, IStorableContent {
    2626    private string filename;
    2727    public string Filename {
  • trunk/sources/HeuristicLab.Common/3.3/Content/ContentManager.cs

    r3483 r3500  
    6060    public static void Save(IStorableContent content, string filename, bool compressed) {
    6161      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    62       instance.SaveContent(content, filename, compressed);
     62      instance.SaveContent((IStorableContent)content.Clone(), filename, compressed);
    6363      content.Filename = filename;
    6464    }
    6565    public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action<IStorableContent, Exception> savingCompletedCallback) {
    6666      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    67       var action = new Action<IStorableContent, string, bool>(instance.SaveContent);
    68       action.BeginInvoke(content, filename, compressed, delegate(IAsyncResult result) {
    69         Exception error = null;
    70         try {
    71           action.EndInvoke(result);
    72           content.Filename = filename;
    73         }
    74         catch (Exception ex) {
    75           error = ex;
    76         }
    77         savingCompletedCallback(content, error);
    78       }, null);
     67
     68      IStorableContent clone = null;
     69      try {
     70        clone = (IStorableContent)content.Clone();
     71      }
     72      catch (Exception ex) {
     73        savingCompletedCallback(content, ex);
     74      }
     75
     76      if (clone != null) {
     77        var action = new Action<IStorableContent, string, bool>(instance.SaveContent);
     78        action.BeginInvoke(clone, filename, compressed, delegate(IAsyncResult result) {
     79          Exception error = null;
     80          try {
     81            action.EndInvoke(result);
     82            content.Filename = filename;
     83          }
     84          catch (Exception ex) {
     85            error = ex;
     86          }
     87          savingCompletedCallback(content, error);
     88        }, null);
     89      }
    7990    }
    8091    protected abstract void SaveContent(IStorableContent content, string filename, bool compressed);
  • trunk/sources/HeuristicLab.Common/3.3/Content/IStorableContent.cs

    r3483 r3500  
    2323
    2424namespace HeuristicLab.Common {
    25   public interface IStorableContent : IContent {
     25  public interface IStorableContent : IContent, IDeepCloneable {
    2626    string Filename { get; set; }
    2727
  • trunk/sources/HeuristicLab.Common/3.3/Content/StorableContent.cs

    r3483 r3500  
    2323
    2424namespace HeuristicLab.Common {
    25   public class StorableContent : IStorableContent {
     25  public class StorableContent : DeepCloneable, IStorableContent {
    2626    private string filename;
    2727    public string Filename {
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IItem.cs

    r3483 r3500  
    2828  /// Interface to represent (almost) every HeuristicLab object (an object, an operator,...).
    2929  /// </summary>
    30   public interface IItem : IStorableContent, IDeepCloneable {
     30  public interface IItem : IStorableContent {
    3131    string ItemName { get; }
    3232    string ItemDescription { get; }
  • trunk/sources/HeuristicLab.Optimization.Views/3.3/AlgorithmView.cs

    r3455 r3500  
    187187      openFileDialog.Title = "Open Problem";
    188188      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
    189         this.Cursor = Cursors.AppStarting;
    190189        newProblemButton.Enabled = openProblemButton.Enabled = false;
    191190        problemViewHost.Enabled = false;
    192191
    193         var call = new Func<string, object>(XmlParser.Deserialize);
    194         call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
    195           IProblem problem = null;
     192        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
    196193          try {
    197             problem = call.EndInvoke(a) as IProblem;
    198           } catch (Exception ex) {
    199             Auxiliary.ShowErrorMessageBox(ex);
    200           }
    201           Invoke(new Action(delegate() {
     194            if (error != null) throw error;
     195            IProblem problem = content as IProblem;
    202196            if (problem == null)
    203197              MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
     
    206200            else
    207201              Content.Problem = problem;
    208             problemViewHost.Enabled = true;
    209             newProblemButton.Enabled = openProblemButton.Enabled = true;
    210             this.Cursor = Cursors.Default;
    211           }));
    212         }, null);
     202          }
     203          catch (Exception ex) {
     204            Auxiliary.ShowErrorMessageBox(ex);
     205          }
     206          finally {
     207            Invoke(new Action(delegate() {
     208              problemViewHost.Enabled = true;
     209              newProblemButton.Enabled = openProblemButton.Enabled = true;
     210            }));
     211          }
     212        });
    213213      }
    214214    }
  • trunk/sources/HeuristicLab.Optimization.Views/3.3/BatchRunView.cs

    r3455 r3500  
    174174      openFileDialog.Title = "Open Algorithm";
    175175      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
    176         this.Cursor = Cursors.AppStarting;
    177176        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
    178177        algorithmViewHost.Enabled = false;
    179178
    180         var call = new Func<string, object>(XmlParser.Deserialize);
    181         call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
    182           IAlgorithm algorithm = null;
     179        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
    183180          try {
    184             algorithm = call.EndInvoke(a) as IAlgorithm;
    185           } catch (Exception ex) {
    186             Auxiliary.ShowErrorMessageBox(ex);
    187           }
    188           Invoke(new Action(delegate() {
     181            if (error != null) throw error;
     182            IAlgorithm algorithm = content as IAlgorithm;
    189183            if (algorithm == null)
    190184              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
    191185            else
    192186              Content.Algorithm = algorithm;
    193             algorithmViewHost.Enabled = true;
    194             newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
    195             this.Cursor = Cursors.Default;
    196           }));
    197         }, null);
     187          }
     188          catch (Exception ex) {
     189            Auxiliary.ShowErrorMessageBox(ex);
     190          }
     191          finally {
     192            Invoke(new Action(delegate() {
     193              algorithmViewHost.Enabled = true;
     194              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
     195            }));
     196          }
     197        });
    198198      }
    199199    }
  • trunk/sources/HeuristicLab.Optimization.Views/3.3/UserDefinedAlgorithmView.cs

    r3455 r3500  
    8080      openFileDialog.Title = "Open Operator Graph";
    8181      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
    82         this.Cursor = Cursors.AppStarting;
    8382        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = false;
    8483        operatorGraphViewHost.Enabled = false;
    8584
    86         var call = new Func<string, object>(XmlParser.Deserialize);
    87         call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
    88           OperatorGraph operatorGraph = null;
     85        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
    8986          try {
    90             operatorGraph = call.EndInvoke(a) as OperatorGraph;
     87            if (error != null) throw error;
     88            OperatorGraph operatorGraph = content as OperatorGraph;
     89            if (operatorGraph == null)
     90              MessageBox.Show(this, "The selected file does not contain an operator graph.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
     91            else
     92              Content.OperatorGraph = operatorGraph;
    9193          }
    9294          catch (Exception ex) {
    9395            Auxiliary.ShowErrorMessageBox(ex);
    9496          }
    95           Invoke(new Action(delegate() {
    96             if (operatorGraph == null)
    97               MessageBox.Show(this, "The selected file does not contain an operator graph.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
    98             else
    99               Content.OperatorGraph = operatorGraph;
    100             operatorGraphViewHost.Enabled = true;
    101             newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = true;
    102             this.Cursor = Cursors.Default;
    103           }));
    104         }, null);
     97          finally {
     98            Invoke(new Action(delegate() {
     99              operatorGraphViewHost.Enabled = true;
     100              newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = true;
     101            }));
     102          }
     103        });
    105104      }
    106105    }
  • trunk/sources/HeuristicLab.Optimizer/3.3/FileManager.cs

    r3498 r3500  
    9595        else {
    9696          ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
    97           ((Form)MainFormManager.MainForm).Enabled = false;
    9897          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
    9998        }
     
    120119        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
    121120          ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
    122           ((Form)MainFormManager.MainForm).Enabled = false;
    123121          if (saveFileDialog.FilterIndex == 1) {
    124122            ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
     
    134132        Invoke(delegate() {
    135133          ((OptimizerMainForm)MainFormManager.MainForm).UpdateTitle();
    136           ((Form)MainFormManager.MainForm).Enabled = true;
    137134        });
    138135      }
Note: See TracChangeset for help on using the changeset viewer.