Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Optimizer/3.3/FileManager.cs @ 15428

Last change on this file since 15428 was 14930, checked in by gkronber, 8 years ago

#2520 changed optimizer to use new persistence per default

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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, 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 = "HeuristicLab Files|*.hl|All Files|*.*";
119          saveFileDialog.FilterIndex = 1;
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          ContentManager.SaveAsync(content, saveFileDialog.FileName, SavingCompleted);
134        }
135      }
136    }
137    private static void SavingCompleted(IStorableContent content, Exception error) {
138      try {
139        if (error != null) throw error;
140        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
141      } catch (Exception ex) {
142        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
143      } finally {
144        SetSaveOperationProgressInContentViews(content, false);
145        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
146      }
147    }
148
149    private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null) {
150      HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
151      #region Mono Compatibility
152      // removed the InvokeRequired check because of Mono
153      mainForm.Invoke((Action)delegate {
154        if (showProgress) {
155          mainForm.AddOperationProgressToContent(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
156        } else
157          mainForm.RemoveOperationProgressFromContent(content);
158      });
159      #endregion
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.