Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/22/10 05:14:39 (15 years ago)
Author:
swagner
Message:

Worked on the refactoring of saving and loading items (#990)

Location:
trunk/sources/HeuristicLab.Optimizer/3.3
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimizer/3.3/FileManager.cs

    r3416 r3483  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.IO;
    2523using System.Linq;
    26 using System.Threading;
    2724using System.Windows.Forms;
     25using HeuristicLab.Common;
    2826using HeuristicLab.Core.Views;
    2927using HeuristicLab.MainForm;
    30 using HeuristicLab.MainForm.WindowsForms;
    31 using HeuristicLab.Persistence.Default.Xml;
    3228
    3329namespace HeuristicLab.Optimizer {
    3430  internal static class FileManager {
    35 
    36     #region Private Class FileInfo
    37     private class FileInfo {
    38       public string Filename { get; set; }
    39       public bool Compressed { get; set; }
    40 
    41       public FileInfo(string filename, bool compressed) {
    42         Filename = filename;
    43         Compressed = compressed;
    44       }
    45       public FileInfo(string filename)
    46         : this(filename, true) {
    47       }
    48       public FileInfo()
    49         : this(string.Empty, true) {
    50       }
    51     }
    52     #endregion
    53 
    54     private static Dictionary<IContentView, FileInfo> files;
    5531    private static NewItemDialog newItemDialog;
    5632    private static OpenFileDialog openFileDialog;
    5733    private static SaveFileDialog saveFileDialog;
    58     private static int waitingCursors;
    59     private static int newDocumentsCounter;
    6034
    6135    static FileManager() {
    62       files = new Dictionary<IContentView, FileInfo>();
    6336      newItemDialog = null;
    6437      openFileDialog = null;
    6538      saveFileDialog = null;
    66       waitingCursors = 0;
    67       newDocumentsCounter = 1;
    68       // NOTE: Events fired by the main form are registered in HeuristicLabOptimizerApplication.
    6939    }
    7040
     
    7343      if (newItemDialog.ShowDialog() == DialogResult.OK) {
    7444        IView view = MainFormManager.CreateDefaultView(newItemDialog.Item);
    75         if (view == null) {
    76           MessageBox.Show("There is no view for the new item. It cannot be displayed. ", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
    77         } else {
    78           if (view is IContentView) {
    79             view.Caption = "Item" + newDocumentsCounter.ToString() + ".hl";
    80             newDocumentsCounter++;
    81           }
    82           view.Show();
    83         }
     45        if (view == null) MessageBox.Show("There is no view for the new item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
     46        else view.Show();
    8447      }
    8548    }
     
    9659
    9760      if (openFileDialog.ShowDialog() == DialogResult.OK) {
    98         foreach (string filename in openFileDialog.FileNames)
    99           LoadItemAsync(filename);
     61        foreach (string filename in openFileDialog.FileNames) {
     62          ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
     63          ContentManager.LoadAsync(filename, LoadingCompleted);
     64        }
     65      }
     66    }
     67    private static void LoadingCompleted(IStorableContent content, Exception error) {
     68      try {
     69        if (error != null) throw error;
     70        Invoke(delegate() {
     71          IView view = MainFormManager.CreateDefaultView(content);
     72          if (view == null) MessageBox.Show("There is no view for the loaded item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
     73          else view.Show();
     74        });
     75      }
     76      catch (Exception ex) {
     77        Auxiliary.ShowErrorMessageBox(ex);
     78      }
     79      finally {
     80        ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
    10081      }
    10182    }
     
    10889    }
    10990    private static void Save(IContentView view) {
    110       if (!view.Locked) {
    111         if ((!files.ContainsKey(view)) || (!File.Exists(files[view].Filename))) {
     91      IStorableContent content = view.Content as IStorableContent;
     92      if (!view.Locked && (content != null)) {
     93        if (string.IsNullOrEmpty(content.Filename))
    11294          SaveAs(view);
    113         } else {
    114           if (files[view].Compressed)
    115             SaveItemAsync(view, files[view].Filename, 9);
    116           else
    117             SaveItemAsync(view, files[view].Filename, 0);
     95        else {
     96          ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
     97          ((Form)MainFormManager.MainForm).Enabled = false;
     98          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
    11899        }
    119100      }
    120101    }
    121 
    122102    public static void SaveAs() {
    123103      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     
    127107    }
    128108    public static void SaveAs(IContentView view) {
    129       if (!view.Locked) {
     109      IStorableContent content = view.Content as IStorableContent;
     110      if (!view.Locked && (content != null)) {
    130111        if (saveFileDialog == null) {
    131112          saveFileDialog = new SaveFileDialog();
     
    135116          saveFileDialog.FilterIndex = 2;
    136117        }
    137 
    138         if (!files.ContainsKey(view)) {
    139           files.Add(view, new FileInfo());
    140           saveFileDialog.FileName = view.Caption;
    141         } else {
    142           saveFileDialog.FileName = files[view].Filename;
    143         }
    144         if (!files[view].Compressed)
    145           saveFileDialog.FilterIndex = 1;
    146         else
    147           saveFileDialog.FilterIndex = 2;
     118        saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename;
    148119
    149120        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
     121          ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
     122          ((Form)MainFormManager.MainForm).Enabled = false;
    150123          if (saveFileDialog.FilterIndex == 1) {
    151             SaveItemAsync(view, saveFileDialog.FileName, 0);
     124            ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
    152125          } else {
    153             SaveItemAsync(view, saveFileDialog.FileName, 9);
     126            ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
    154127          }
    155128        }
    156129      }
    157130    }
    158 
    159131    public static void SaveAll() {
    160132      foreach (IContentView view in MainFormManager.MainForm.Views.OfType<IContentView>())
    161133        Save(view);
    162134    }
    163 
    164     // NOTE: This event is fired by the main form. It is registered in HeuristicLabOptimizerApplication.
    165     internal static void ViewClosed(object sender, ViewEventArgs e) {
    166       IContentView view = e.View as IContentView;
    167       if (view != null) files.Remove(view);
     135    private static void SavingCompleted(IStorableContent content, Exception error) {
     136      try {
     137        if (error != null) throw error;
     138        Invoke(delegate() {
     139          ((OptimizerMainForm)MainFormManager.MainForm).UpdateTitle();
     140          ((Form)MainFormManager.MainForm).Enabled = true;
     141        });
     142      }
     143      catch (Exception ex) {
     144        Auxiliary.ShowErrorMessageBox(ex);
     145      }
     146      finally {
     147        ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
     148      }
    168149    }
    169150
    170     #region Asynchronous Save/Load Operations
    171151    private static void Invoke(Action a) {
    172152      Form form = MainFormManager.MainForm as Form;
     
    176156        a.Invoke();
    177157    }
    178 
    179     private static void SaveItemAsync(IContentView view, string filename, int compression) {
    180       ThreadPool.QueueUserWorkItem(
    181         new WaitCallback(
    182           delegate(object arg) {
    183             try {
    184               DisableView(view);
    185               SetWaitingCursor();
    186               XmlGenerator.Serialize(view.Content, filename, compression);
    187               Invoke(delegate() {
    188                 view.Caption = Path.GetFileName(filename);
    189                 files[view].Filename = filename;
    190                 files[view].Compressed = compression > 0;
    191               });
    192             }
    193             catch (Exception ex) {
    194               Auxiliary.ShowErrorMessageBox(ex);
    195             } finally {
    196               ResetWaitingCursor();
    197               EnableView(view);
    198             }
    199           }
    200         )
    201       );
    202     }
    203     private static void LoadItemAsync(string filename) {
    204       ThreadPool.QueueUserWorkItem(
    205         new WaitCallback(
    206           delegate(object arg) {
    207             try {
    208               SetWaitingCursor();
    209               object content = XmlParser.Deserialize(filename);
    210               Invoke(delegate() {
    211                 IContentView view = MainFormManager.CreateDefaultView(content) as IContentView;
    212                 if (view == null) {
    213                   MessageBox.Show("There is no view for the loaded item. It cannot be displayed. ", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
    214                 } else {
    215                   view.Caption = Path.GetFileName(filename);
    216                   files.Add(view, new FileInfo(filename));
    217                   view.Show();
    218                 }
    219               });
    220             } catch (Exception ex) {
    221               Auxiliary.ShowErrorMessageBox(ex);
    222             } finally {
    223               ResetWaitingCursor();
    224             }
    225           }
    226         )
    227       );
    228     }
    229 
    230     private static void SetWaitingCursor() {
    231       Invoke(delegate() {
    232         waitingCursors++;
    233         ((Form)MainFormManager.MainForm).Cursor = Cursors.AppStarting;
    234       });
    235     }
    236     private static void ResetWaitingCursor() {
    237       Invoke(delegate() {
    238         waitingCursors--;
    239         if (waitingCursors == 0) ((Form)MainFormManager.MainForm).Cursor = Cursors.Default;
    240       });
    241     }
    242     private static void DisableView(IView view) {
    243       Invoke(delegate() {
    244         ((Control)view).SuspendRepaint();
    245         ((Control)view).Enabled = false;
    246         ((Control)view).ResumeRepaint(true);
    247       });
    248     }
    249     private static void EnableView(IView view) {
    250       Invoke(delegate() {
    251         ((Control)view).SuspendRepaint();
    252         ((Control)view).Enabled = true;
    253         ((Control)view).ResumeRepaint(true);
    254       });
    255     }
    256     #endregion
    257158  }
    258159}
  • trunk/sources/HeuristicLab.Optimizer/3.3/MenuItems/CopyToClipboardMenuItem.cs

    r3416 r3483  
    3939      get { return 2100; }
    4040    }
    41     public override Keys ShortCutKeys {
    42       get { return Keys.Control | Keys.C; }
    43     }
    4441
    4542    protected override void OnToolStripItemSet(EventArgs e) {
     
    4845    protected override void OnActiveViewChanged(object sender, EventArgs e) {
    4946      ItemView activeView = MainFormManager.MainForm.ActiveView as ItemView;
    50       ToolStripItem.Enabled = (activeView != null) && (!activeView.Locked);
     47      ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) && !activeView.Locked;
    5148    }
    5249
    5350    public override void Execute() {
    5451      ItemView activeView = MainFormManager.MainForm.ActiveView as ItemView;
    55       if ((activeView != null) && (!activeView.Locked)) {
     52      if ((activeView != null) && (activeView.Content != null) && !activeView.Locked) {
    5653        Clipboard<IItem> clipboard = ((OptimizerMainForm)MainFormManager.MainForm).Clipboard;
    5754        clipboard.AddItem((IItem)activeView.Content.Clone());
  • trunk/sources/HeuristicLab.Optimizer/3.3/MenuItems/SaveAllMenuItem.cs

    r3416 r3483  
    2525using System.Linq;
    2626using System.Windows.Forms;
     27using HeuristicLab.Common;
    2728using HeuristicLab.MainForm;
    2829
     
    4748    protected override void OnActiveViewChanged(object sender, EventArgs e) {
    4849      var views = from v in MainFormManager.MainForm.Views.OfType<IContentView>()
     50                  where v.Content != null
     51                  where v.Content is IStorableContent
    4952                  where !v.Locked
    5053                  select v;
  • trunk/sources/HeuristicLab.Optimizer/3.3/MenuItems/SaveAsMenuItem.cs

    r3416 r3483  
    2323using System.Collections.Generic;
    2424using System.Windows.Forms;
     25using HeuristicLab.Common;
    2526using HeuristicLab.MainForm;
    2627
     
    4546    protected override void OnActiveViewChanged(object sender, EventArgs e) {
    4647      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    47       ToolStripItem.Enabled = (activeView != null) && (!activeView.Locked);
     48      ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) &&
     49                              (activeView.Content is IStorableContent) && !activeView.Locked;
    4850    }
    4951
  • trunk/sources/HeuristicLab.Optimizer/3.3/MenuItems/SaveMenuItem.cs

    r3416 r3483  
    2424using System.Drawing;
    2525using System.Windows.Forms;
     26using HeuristicLab.Common;
    2627using HeuristicLab.MainForm;
    2728
     
    4950    protected override void OnActiveViewChanged(object sender, EventArgs e) {
    5051      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    51       ToolStripItem.Enabled = (activeView != null) && (!activeView.Locked);
     52      ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) &&
     53                              (activeView.Content is IStorableContent) && !activeView.Locked;
    5254    }
    5355
  • trunk/sources/HeuristicLab.Optimizer/3.3/OptimizerMainForm.cs

    r3395 r3483  
    3232namespace HeuristicLab.Optimizer {
    3333  internal partial class OptimizerMainForm : DockingMainForm {
     34    private string title;
     35    private int appStartingCursors;
     36    private int waitingCursors;
     37
    3438    private Clipboard<IItem> clipboard;
    3539    public Clipboard<IItem> Clipboard {
     
    4044      : base() {
    4145      InitializeComponent();
     46      appStartingCursors = 0;
     47      waitingCursors = 0;
    4248    }
    4349    public OptimizerMainForm(Type userInterfaceItemType)
    4450      : base(userInterfaceItemType) {
    4551      InitializeComponent();
     52      appStartingCursors = 0;
     53      waitingCursors = 0;
    4654    }
    4755    public OptimizerMainForm(Type userInterfaceItemType, bool showViewsInViewHost)
     
    5462      AssemblyFileVersionAttribute version = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
    5563                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
    56       Title = "HeuristicLab Optimizer";
    57       if (version != null) Title += " " + version.Version;
    58       ViewClosed += new EventHandler<ViewEventArgs>(FileManager.ViewClosed);
     64      title = "HeuristicLab Optimizer";
     65      if (version != null) title += " " + version.Version;
     66      Title = title;
     67
     68      ContentManager.Initialize(new PersistenceContentManager());
    5969
    6070      clipboard = new Clipboard<IItem>();
     
    7383      }
    7484    }
     85
     86    protected override void OnActiveViewChanged() {
     87      base.OnActiveViewChanged();
     88      UpdateTitle();
     89    }
     90
     91    public void UpdateTitle() {
     92      if (InvokeRequired)
     93        Invoke(new Action(UpdateTitle));
     94      else {
     95        IContentView activeView = ActiveView as IContentView;
     96        if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IStorableContent)) {
     97          IStorableContent content = (IStorableContent)activeView.Content;
     98          Title = title + " [" + (string.IsNullOrEmpty(content.Filename) ? "Unsaved" : content.Filename) + "]";
     99        } else {
     100          Title = title;
     101        }
     102      }
     103    }
     104
     105    #region Cursor Handling
     106    public void SetAppStartingCursor() {
     107      if (InvokeRequired)
     108        Invoke(new Action(SetAppStartingCursor));
     109      else {
     110        appStartingCursors++;
     111        SetCursor();
     112      }
     113    }
     114    public void ResetAppStartingCursor() {
     115      if (InvokeRequired)
     116        Invoke(new Action(ResetAppStartingCursor));
     117      else {
     118        appStartingCursors--;
     119        SetCursor();
     120      }
     121    }
     122    public void SetWaitCursor() {
     123      if (InvokeRequired)
     124        Invoke(new Action(SetWaitCursor));
     125      else {
     126        waitingCursors++;
     127        SetCursor();
     128      }
     129    }
     130    public void ResetWaitCursor() {
     131      if (InvokeRequired)
     132        Invoke(new Action(ResetWaitCursor));
     133      else {
     134        waitingCursors--;
     135        SetCursor();
     136      }
     137    }
     138    private void SetCursor() {
     139      if (waitingCursors > 0) Cursor = Cursors.WaitCursor;
     140      else if (appStartingCursors > 0) Cursor = Cursors.AppStarting;
     141      else Cursor = Cursors.Default;
     142    }
     143    #endregion
    75144  }
    76145}
  • trunk/sources/HeuristicLab.Optimizer/3.3/ToolBarItems/SaveAllToolBarItem.cs

    r3416 r3483  
    2424using System.Linq;
    2525using System.Windows.Forms;
     26using HeuristicLab.Common;
    2627using HeuristicLab.MainForm;
    2728
     
    4647    protected override void OnActiveViewChanged(object sender, EventArgs e) {
    4748      var views = from v in MainFormManager.MainForm.Views.OfType<IContentView>()
     49                  where v.Content != null
     50                  where v.Content is IStorableContent
    4851                  where !v.Locked
    4952                  select v;
  • trunk/sources/HeuristicLab.Optimizer/3.3/ToolBarItems/SaveToolBarItem.cs

    r3416 r3483  
    2323using System.Drawing;
    2424using System.Windows.Forms;
     25using HeuristicLab.Common;
    2526using HeuristicLab.MainForm;
    2627
     
    4546    protected override void OnActiveViewChanged(object sender, EventArgs e) {
    4647      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    47       ToolStripItem.Enabled = (activeView != null) && (!activeView.Locked);
     48      ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) &&
     49                              (activeView.Content is IStorableContent) && !activeView.Locked;
    4850    }
    4951
Note: See TracChangeset for help on using the changeset viewer.