Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Optimizer/3.3/FileManager.cs

Last change on this file was 17246, checked in by gkronber, 5 years ago

#2925: merged r17037:17242 from trunk to branch

File size: 7.1 KB
RevLine 
[2790]1#region License Information
2/* HeuristicLab
[17246]3 * Copyright (C) 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;
[17000]25using System.Linq;
[16662]26using System.Threading;
[2546]27using System.Windows.Forms;
[3483]28using HeuristicLab.Common;
[11012]29using HeuristicLab.Core;
[2546]30using HeuristicLab.MainForm;
[3758]31using HeuristicLab.PluginInfrastructure;
[2546]32
33namespace HeuristicLab.Optimizer {
34  internal static class FileManager {
[2547]35    private static NewItemDialog newItemDialog;
36    private static OpenFileDialog openFileDialog;
37    private static SaveFileDialog saveFileDialog;
[2546]38
[2547]39    static FileManager() {
40      newItemDialog = null;
41      openFileDialog = null;
42      saveFileDialog = null;
43    }
44
[2546]45    public static void New() {
46      if (newItemDialog == null) newItemDialog = new NewItemDialog();
47      if (newItemDialog.ShowDialog() == DialogResult.OK) {
[3557]48        IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
[4245]49        if (view == null)
50          ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
[2546]51      }
52    }
53
54    public static void Open() {
55      if (openFileDialog == null) {
56        openFileDialog = new OpenFileDialog();
57        openFileDialog.Title = "Open Item";
58        openFileDialog.FileName = "Item";
59        openFileDialog.Multiselect = true;
60        openFileDialog.DefaultExt = "hl";
61        openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
62      }
63
64      if (openFileDialog.ShowDialog() == DialogResult.OK) {
[8818]65        OpenFiles(openFileDialog.FileNames);
[2546]66      }
67    }
[8818]68
69    public static void OpenFiles(IEnumerable<string> fileNames) {
70      foreach (string filename in fileNames) {
71        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
72        ContentManager.LoadAsync(filename, LoadingCompleted);
73      }
74    }
75
[17000]76    private static void LoadingCompleted(IStorableContent content, Exception error, ContentManager.Info info) {
[3483]77      try {
78        if (error != null) throw error;
[17000]79        if (info!=null && info.UnknownTypeGuids.Any()) {
80          var message = "Unknown type guids: " + string.Join(Environment.NewLine, info.UnknownTypeGuids);
81          MessageBox.Show((Control)MainFormManager.MainForm, message, $"File {info.Filename} not restored completely", MessageBoxButtons.OK, MessageBoxIcon.Information);
82        }
[4245]83        IView view = MainFormManager.MainForm.ShowContent(content);
84        if (view == null)
85          ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
[16662]86      } catch (Exception ex) {
[3758]87        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
[16662]88      } finally {
[6827]89        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
[3483]90      }
91    }
[2546]92
93    public static void Save() {
[2713]94      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]95      if (activeView != null) {
[2547]96        Save(activeView);
[2546]97      }
98    }
[2713]99    private static void Save(IContentView view) {
[3483]100      IStorableContent content = view.Content as IStorableContent;
[4245]101      if (!view.Locked && content != null) {
[3483]102        if (string.IsNullOrEmpty(content.Filename))
[2961]103          SaveAs(view);
[3483]104        else {
[9849]105          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
[16662]106          var cancellationTokenSource = new CancellationTokenSource();
107          AddProgressInContentViews(content, cancellationTokenSource);
108          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted, cancellationTokenSource.Token);
[2961]109        }
[2546]110      }
111    }
112    public static void SaveAs() {
[2713]113      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]114      if (activeView != null) {
[2547]115        SaveAs(activeView);
[2546]116      }
117    }
[2713]118    public static void SaveAs(IContentView view) {
[3483]119      IStorableContent content = view.Content as IStorableContent;
[4245]120      if (!view.Locked && content != null) {
[2961]121        if (saveFileDialog == null) {
122          saveFileDialog = new SaveFileDialog();
123          saveFileDialog.Title = "Save Item";
124          saveFileDialog.DefaultExt = "hl";
125          saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
126          saveFileDialog.FilterIndex = 2;
127        }
[2546]128
[11012]129        INamedItem namedItem = content as INamedItem;
130        string suggestedFileName = string.Empty;
131        if (!string.IsNullOrEmpty(content.Filename)) suggestedFileName = content.Filename;
132        else if (namedItem != null) suggestedFileName = namedItem.Name;
133        else suggestedFileName = "Item";
134
135        saveFileDialog.FileName = suggestedFileName;
136
[2961]137        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
[9849]138          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
[16662]139          bool compressed = saveFileDialog.FilterIndex != 1;
140          var cancellationTokenSource = new CancellationTokenSource();
141          AddProgressInContentViews(content, cancellationTokenSource, saveFileDialog.FileName);
142
143          ContentManager.SaveAsync(content, saveFileDialog.FileName, compressed, SavingCompleted, cancellationTokenSource.Token);
[2961]144        }
[2546]145      }
146    }
[3483]147    private static void SavingCompleted(IStorableContent content, Exception error) {
148      try {
149        if (error != null) throw error;
[6935]150        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
[16662]151      } catch (OperationCanceledException) { // do nothing if canceled
152      } catch (Exception ex) {
[3758]153        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
[16662]154      } finally {
155        Progress.Hide(content);
[9849]156        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
[3483]157      }
[2547]158    }
[4435]159
[16662]160    private static void AddProgressInContentViews(IStorableContent content, CancellationTokenSource cancellationTokenSource, string fileName = null) {
161      string message = string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename));
162      Progress.Show(content, message, ProgressMode.Indeterminate, cancelRequestHandler: () => cancellationTokenSource.Cancel());
[4435]163    }
[2546]164  }
165}
Note: See TracBrowser for help on using the repository browser.