Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10744 was 9901, checked in by mkommend, 11 years ago

#1042: Updated progress message while saving a file.

File size: 6.6 KB
RevLine 
[2790]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2790]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;
[8818]23using System.Collections.Generic;
[9849]24using System.IO;
[2546]25using System.Windows.Forms;
[3483]26using HeuristicLab.Common;
[2546]27using HeuristicLab.MainForm;
[3758]28using HeuristicLab.PluginInfrastructure;
[2546]29
30namespace HeuristicLab.Optimizer {
31  internal static class FileManager {
[2547]32    private static NewItemDialog newItemDialog;
33    private static OpenFileDialog openFileDialog;
34    private static SaveFileDialog saveFileDialog;
[2546]35
[2547]36    static FileManager() {
37      newItemDialog = null;
38      openFileDialog = null;
39      saveFileDialog = null;
40    }
41
[2546]42    public static void New() {
43      if (newItemDialog == null) newItemDialog = new NewItemDialog();
44      if (newItemDialog.ShowDialog() == DialogResult.OK) {
[3557]45        IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
[4245]46        if (view == null)
47          ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
[2546]48      }
49    }
50
51    public static void Open() {
52      if (openFileDialog == null) {
53        openFileDialog = new OpenFileDialog();
54        openFileDialog.Title = "Open Item";
55        openFileDialog.FileName = "Item";
56        openFileDialog.Multiselect = true;
57        openFileDialog.DefaultExt = "hl";
58        openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
59      }
60
61      if (openFileDialog.ShowDialog() == DialogResult.OK) {
[8818]62        OpenFiles(openFileDialog.FileNames);
[2546]63      }
64    }
[8818]65
66    public static void OpenFiles(IEnumerable<string> fileNames) {
67      foreach (string filename in fileNames) {
68        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
69        ContentManager.LoadAsync(filename, LoadingCompleted);
70      }
71    }
72
[3483]73    private static void LoadingCompleted(IStorableContent content, Exception error) {
74      try {
75        if (error != null) throw error;
[4245]76        IView view = MainFormManager.MainForm.ShowContent(content);
77        if (view == null)
78          ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
[3483]79      }
80      catch (Exception ex) {
[3758]81        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
[3483]82      }
83      finally {
[6827]84        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
[3483]85      }
86    }
[2546]87
88    public static void Save() {
[2713]89      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]90      if (activeView != null) {
[2547]91        Save(activeView);
[2546]92      }
93    }
[2713]94    private static void Save(IContentView view) {
[3483]95      IStorableContent content = view.Content as IStorableContent;
[4245]96      if (!view.Locked && content != null) {
[3483]97        if (string.IsNullOrEmpty(content.Filename))
[2961]98          SaveAs(view);
[3483]99        else {
[9849]100          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
[9851]101          SetSaveOperationProgressInContentViews(content, true);
[3483]102          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
[2961]103        }
[2546]104      }
105    }
106    public static void SaveAs() {
[2713]107      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]108      if (activeView != null) {
[2547]109        SaveAs(activeView);
[2546]110      }
111    }
[2713]112    public static void SaveAs(IContentView view) {
[3483]113      IStorableContent content = view.Content as IStorableContent;
[4245]114      if (!view.Locked && content != null) {
[2961]115        if (saveFileDialog == null) {
116          saveFileDialog = new SaveFileDialog();
117          saveFileDialog.Title = "Save Item";
118          saveFileDialog.DefaultExt = "hl";
119          saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
120          saveFileDialog.FilterIndex = 2;
121        }
[3483]122        saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename;
[2546]123
[2961]124        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
[9849]125          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
[9851]126          SetSaveOperationProgressInContentViews(content, true, saveFileDialog.FileName);
[2961]127          if (saveFileDialog.FilterIndex == 1) {
[3483]128            ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
[2961]129          } else {
[3483]130            ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
[2961]131          }
132        }
[2546]133      }
134    }
[3483]135    private static void SavingCompleted(IStorableContent content, Exception error) {
136      try {
137        if (error != null) throw error;
[6935]138        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
[3483]139      }
140      catch (Exception ex) {
[3758]141        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
[3483]142      }
143      finally {
[9851]144        SetSaveOperationProgressInContentViews(content, false);
[9849]145        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
[3483]146      }
[2547]147    }
[4435]148
[9851]149    private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null) {
[6935]150      HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
[8587]151      #region Mono Compatibility
152      // removed the InvokeRequired check because of Mono
153      mainForm.Invoke((Action)delegate {
[9851]154        if (showProgress) {
[9901]155          mainForm.AddOperationProgressToContent(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
[9851]156        } else
[9849]157          mainForm.RemoveOperationProgressFromContent(content);
[8587]158      });
159      #endregion
[4435]160    }
[2546]161  }
162}
Note: See TracBrowser for help on using the repository browser.