source: stable/HeuristicLab.Optimizer/3.3/FileManager.cs @ 17062

Last change on this file since 17062 was 17062, checked in by mkommend, 23 months ago

#2845: Merged 16430 into stable.

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