Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2990_VariableImpactBasedFeatureSelection/HeuristicLab.Optimizer/3.3/FileManager.cs @ 17187

Last change on this file since 17187 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

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