Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3087_Ceres_Integration/HeuristicLab.Optimizer/3.3/FileManager.cs @ 18078

Last change on this file since 18078 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Threading;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.MainForm;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Optimizer {
34  internal static class FileManager {
35    private static NewItemDialog newItemDialog;
36    private static OpenFileDialog openFileDialog;
37    private static SaveFileDialog saveFileDialog;
38
39    static FileManager() {
40      newItemDialog = null;
41      openFileDialog = null;
42      saveFileDialog = null;
43    }
44
45    public static void New() {
46      if (newItemDialog == null) newItemDialog = new NewItemDialog();
47      if (newItemDialog.ShowDialog() == DialogResult.OK) {
48        IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
49        if (view == null)
50          ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
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) {
65        OpenFiles(openFileDialog.FileNames);
66      }
67    }
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
76    private static void LoadingCompleted(IStorableContent content, Exception error, ContentManager.Info info) {
77      try {
78        if (error != null) throw error;
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        }
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"));
86      } catch (Exception ex) {
87        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
88      } finally {
89        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
90      }
91    }
92
93    public static void Save() {
94      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
95      if (activeView != null) {
96        Save(activeView);
97      }
98    }
99    private static void Save(IContentView view) {
100      IStorableContent content = view.Content as IStorableContent;
101      if (!view.Locked && content != null) {
102        if (string.IsNullOrEmpty(content.Filename))
103          SaveAs(view);
104        else {
105          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
106          var cancellationTokenSource = new CancellationTokenSource();
107          AddProgressInContentViews(content, cancellationTokenSource);
108          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted, cancellationTokenSource.Token);
109        }
110      }
111    }
112    public static void SaveAs() {
113      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
114      if (activeView != null) {
115        SaveAs(activeView);
116      }
117    }
118    public static void SaveAs(IContentView view) {
119      IStorableContent content = view.Content as IStorableContent;
120      if (!view.Locked && content != null) {
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        }
128
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
137        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
138          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
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);
144        }
145      }
146    }
147    private static void SavingCompleted(IStorableContent content, Exception error) {
148      try {
149        if (error != null) throw error;
150        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
151      } catch (OperationCanceledException) { // do nothing if canceled
152      } catch (Exception ex) {
153        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
154      } finally {
155        Progress.Hide(content);
156        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
157      }
158    }
159
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());
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.