Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2520 changed optimizer to use new persistence per default

File size: 6.6 KB
RevLine 
[2790]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2790]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;
[8818]23using System.Collections.Generic;
[9849]24using System.IO;
[2546]25using System.Windows.Forms;
[3483]26using HeuristicLab.Common;
[11012]27using HeuristicLab.Core;
[2546]28using HeuristicLab.MainForm;
[3758]29using HeuristicLab.PluginInfrastructure;
[2546]30
31namespace HeuristicLab.Optimizer {
32  internal static class FileManager {
[2547]33    private static NewItemDialog newItemDialog;
34    private static OpenFileDialog openFileDialog;
35    private static SaveFileDialog saveFileDialog;
[2546]36
[2547]37    static FileManager() {
38      newItemDialog = null;
39      openFileDialog = null;
40      saveFileDialog = null;
41    }
42
[2546]43    public static void New() {
44      if (newItemDialog == null) newItemDialog = new NewItemDialog();
45      if (newItemDialog.ShowDialog() == DialogResult.OK) {
[3557]46        IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
[4245]47        if (view == null)
48          ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
[2546]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) {
[8818]63        OpenFiles(openFileDialog.FileNames);
[2546]64      }
65    }
[8818]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
[3483]74    private static void LoadingCompleted(IStorableContent content, Exception error) {
75      try {
76        if (error != null) throw error;
[4245]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"));
[14930]80      } catch (Exception ex) {
[3758]81        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
[14930]82      } finally {
[6827]83        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
[3483]84      }
85    }
[2546]86
87    public static void Save() {
[2713]88      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]89      if (activeView != null) {
[2547]90        Save(activeView);
[2546]91      }
92    }
[2713]93    private static void Save(IContentView view) {
[3483]94      IStorableContent content = view.Content as IStorableContent;
[4245]95      if (!view.Locked && content != null) {
[3483]96        if (string.IsNullOrEmpty(content.Filename))
[2961]97          SaveAs(view);
[3483]98        else {
[9849]99          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
[9851]100          SetSaveOperationProgressInContentViews(content, true);
[14930]101          ContentManager.SaveAsync(content, content.Filename, SavingCompleted);
[2961]102        }
[2546]103      }
104    }
105    public static void SaveAs() {
[2713]106      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]107      if (activeView != null) {
[2547]108        SaveAs(activeView);
[2546]109      }
110    }
[2713]111    public static void SaveAs(IContentView view) {
[3483]112      IStorableContent content = view.Content as IStorableContent;
[4245]113      if (!view.Locked && content != null) {
[2961]114        if (saveFileDialog == null) {
115          saveFileDialog = new SaveFileDialog();
116          saveFileDialog.Title = "Save Item";
117          saveFileDialog.DefaultExt = "hl";
[14930]118          saveFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
119          saveFileDialog.FilterIndex = 1;
[2961]120        }
[2546]121
[11012]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
[2961]130        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
[9849]131          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
[9851]132          SetSaveOperationProgressInContentViews(content, true, saveFileDialog.FileName);
[14930]133          ContentManager.SaveAsync(content, saveFileDialog.FileName, SavingCompleted);
[2961]134        }
[2546]135      }
136    }
[3483]137    private static void SavingCompleted(IStorableContent content, Exception error) {
138      try {
139        if (error != null) throw error;
[6935]140        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
[14930]141      } catch (Exception ex) {
[3758]142        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
[14930]143      } finally {
[9851]144        SetSaveOperationProgressInContentViews(content, false);
[9849]145        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
[3483]146      }
[2547]147    }
[4435]148
[9851]149    private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null) {
[6935]150      HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
[8587]151      #region Mono Compatibility
152      // removed the InvokeRequired check because of Mono
153      mainForm.Invoke((Action)delegate {
[9851]154        if (showProgress) {
[9901]155          mainForm.AddOperationProgressToContent(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
[9851]156        } else
[9849]157          mainForm.RemoveOperationProgressFromContent(content);
[8587]158      });
159      #endregion
[4435]160    }
[2546]161  }
162}
Note: See TracBrowser for help on using the repository browser.