Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimizer/3.3/FileManager.cs @ 3242

Last change on this file since 3242 was 3177, checked in by swagner, 14 years ago

Moved extension methods of Control from HeuristicLab.Core.Views to HeuristicLab.MainForm.WindowsForms and suspended/resumed repainting in some controls to improve UI response times (#887).

File size: 8.7 KB
RevLine 
[2790]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
[2546]23using System.Collections.Generic;
[2790]24using System.IO;
[2546]25using System.Linq;
[2547]26using System.Threading;
[2546]27using System.Windows.Forms;
[2790]28using HeuristicLab.Core.Views;
[2546]29using HeuristicLab.MainForm;
[3177]30using HeuristicLab.MainForm.WindowsForms;
[2546]31using HeuristicLab.Persistence.Default.Xml;
32
33namespace HeuristicLab.Optimizer {
34  internal static class FileManager {
35
[2547]36    #region Private Class FileInfo
[2546]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    }
[2547]52    #endregion
[2546]53
[2713]54    private static Dictionary<IContentView, FileInfo> files;
[2547]55    private static NewItemDialog newItemDialog;
56    private static OpenFileDialog openFileDialog;
57    private static SaveFileDialog saveFileDialog;
58    private static int waitingCursors;
59    private static int newDocumentsCounter;
[2546]60
[2547]61    static FileManager() {
[2713]62      files = new Dictionary<IContentView, FileInfo>();
[2547]63      newItemDialog = null;
64      openFileDialog = null;
65      saveFileDialog = null;
66      waitingCursors = 0;
67      newDocumentsCounter = 1;
[2555]68      // NOTE: Events fired by the main form are registered in HeuristicLabOptimizerApplication.
[2547]69    }
70
[2546]71    public static void New() {
72      if (newItemDialog == null) newItemDialog = new NewItemDialog();
73      if (newItemDialog.ShowDialog() == DialogResult.OK) {
[2555]74        IView view = MainFormManager.CreateDefaultView(newItemDialog.Item);
[2665]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 {
[2713]78          if (view is IContentView) {
[2665]79            view.Caption = "Item" + newDocumentsCounter.ToString() + ".hl";
80            newDocumentsCounter++;
81          }
[2708]82          view.Show();
[2547]83        }
[2546]84      }
85    }
86
87    public static void Open() {
88      if (openFileDialog == null) {
89        openFileDialog = new OpenFileDialog();
90        openFileDialog.Title = "Open Item";
91        openFileDialog.FileName = "Item";
92        openFileDialog.Multiselect = true;
93        openFileDialog.DefaultExt = "hl";
94        openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
95      }
96
97      if (openFileDialog.ShowDialog() == DialogResult.OK) {
[2547]98        foreach (string filename in openFileDialog.FileNames)
99          LoadItemAsync(filename);
[2546]100      }
101    }
102
103    public static void Save() {
[2713]104      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]105      if (activeView != null) {
[2547]106        Save(activeView);
[2546]107      }
108    }
[2713]109    private static void Save(IContentView view) {
[2961]110      if (view.SaveEnabled) {
111        if ((!files.ContainsKey(view)) || (!File.Exists(files[view].Filename))) {
112          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);
118        }
[2546]119      }
120    }
121
122    public static void SaveAs() {
[2713]123      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]124      if (activeView != null) {
[2547]125        SaveAs(activeView);
[2546]126      }
127    }
[2713]128    public static void SaveAs(IContentView view) {
[2961]129      if (view.SaveEnabled) {
130        if (saveFileDialog == null) {
131          saveFileDialog = new SaveFileDialog();
132          saveFileDialog.Title = "Save Item";
133          saveFileDialog.DefaultExt = "hl";
134          saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
135          saveFileDialog.FilterIndex = 2;
136        }
[2546]137
[2961]138        if (!files.ContainsKey(view)) {
139          files.Add(view, new FileInfo());
140          saveFileDialog.FileName = view.Caption;
[2546]141        } else {
[2961]142          saveFileDialog.FileName = files[view].Filename;
[2546]143        }
[2961]144        if (!files[view].Compressed)
145          saveFileDialog.FilterIndex = 1;
146        else
147          saveFileDialog.FilterIndex = 2;
148
149        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
150          if (saveFileDialog.FilterIndex == 1) {
151            SaveItemAsync(view, saveFileDialog.FileName, 0);
152          } else {
153            SaveItemAsync(view, saveFileDialog.FileName, 9);
154          }
155        }
[2546]156      }
157    }
158
159    public static void SaveAll() {
[2961]160      foreach (IContentView view in MainFormManager.MainForm.Views.OfType<IContentView>())
161        Save(view);
[2546]162    }
[2547]163
[2555]164    // NOTE: This event is fired by the main form. It is registered in HeuristicLabOptimizerApplication.
165    internal static void ViewClosed(object sender, ViewEventArgs e) {
[2713]166      IContentView view = e.View as IContentView;
[2656]167      if (view != null) files.Remove(view);
[2547]168    }
169
170    #region Asynchronous Save/Load Operations
171    private static void Invoke(Action a) {
172      Form form = MainFormManager.MainForm as Form;
173      if (form.InvokeRequired)
174        form.Invoke(a);
175      else
176        a.Invoke();
177    }
178
[2713]179    private static void SaveItemAsync(IContentView view, string filename, int compression) {
[2547]180      ThreadPool.QueueUserWorkItem(
181        new WaitCallback(
182          delegate(object arg) {
183            try {
[2556]184              DisableView(view);
[2547]185              SetWaitingCursor();
[2713]186              XmlGenerator.Serialize(view.Content, filename, compression);
[2547]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();
[2556]197              EnableView(view);
[2547]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();
[2961]209              object content = XmlParser.Deserialize(filename);
[2547]210              Invoke(delegate() {
[2961]211                IContentView view = MainFormManager.CreateDefaultView(content) as IContentView;
[2665]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 {
[2547]215                  view.Caption = Path.GetFileName(filename);
216                  files.Add(view, new FileInfo(filename));
[2708]217                  view.Show();
[2547]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    }
[2556]242    private static void DisableView(IView view) {
243      Invoke(delegate() {
[3177]244        ((Control)view).SuspendRepaint();
245        ((Control)view).Enabled = false;
246        ((Control)view).ResumeRepaint(true);
[2556]247      });
248    }
249    private static void EnableView(IView view) {
250      Invoke(delegate() {
[3177]251        ((Control)view).SuspendRepaint();
252        ((Control)view).Enabled = true;
253        ((Control)view).ResumeRepaint(true);
[2556]254      });
255    }
[2547]256    #endregion
[2546]257  }
258}
Note: See TracBrowser for help on using the repository browser.