Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Optimizer/3.3/FileManager.cs @ 15728

Last change on this file since 15728 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 6.9 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      }
81      catch (Exception ex) {
82        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
83      }
84      finally {
85        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
86      }
87    }
88
89    public static void Save() {
90      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
91      if (activeView != null) {
92        Save(activeView);
93      }
94    }
95    private static void Save(IContentView view) {
96      IStorableContent content = view.Content as IStorableContent;
97      if (!view.Locked && content != null) {
98        if (string.IsNullOrEmpty(content.Filename))
99          SaveAs(view);
100        else {
101          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
102          SetSaveOperationProgressInContentViews(content, true);
103          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
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          SetSaveOperationProgressInContentViews(content, true, saveFileDialog.FileName);
135          if (saveFileDialog.FilterIndex == 1) {
136            ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
137          } else {
138            ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
139          }
140        }
141      }
142    }
143    private static void SavingCompleted(IStorableContent content, Exception error) {
144      try {
145        if (error != null) throw error;
146        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
147      }
148      catch (Exception ex) {
149        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
150      }
151      finally {
152        SetSaveOperationProgressInContentViews(content, false);
153        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
154      }
155    }
156
157    private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null) {
158      HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
159      #region Mono Compatibility
160      // removed the InvokeRequired check because of Mono
161      mainForm.Invoke((Action)delegate {
162        if (showProgress) {
163          mainForm.AddOperationProgressToContent(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
164        } else
165          mainForm.RemoveOperationProgressFromContent(content);
166      });
167      #endregion
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.