Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimizer/3.3/FileManager.cs @ 9840

Last change on this file since 9840 was 9456, checked in by swagner, 11 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.4 KB
RevLine 
[2790]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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;
[4245]24using System.Linq;
[2546]25using System.Windows.Forms;
[3483]26using HeuristicLab.Common;
[2546]27using HeuristicLab.MainForm;
[3758]28using HeuristicLab.PluginInfrastructure;
[2546]29
30namespace HeuristicLab.Optimizer {
31  internal static class FileManager {
[2547]32    private static NewItemDialog newItemDialog;
33    private static OpenFileDialog openFileDialog;
34    private static SaveFileDialog saveFileDialog;
[2546]35
[2547]36    static FileManager() {
37      newItemDialog = null;
38      openFileDialog = null;
39      saveFileDialog = null;
40    }
41
[2546]42    public static void New() {
43      if (newItemDialog == null) newItemDialog = new NewItemDialog();
44      if (newItemDialog.ShowDialog() == DialogResult.OK) {
[3557]45        IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
[4245]46        if (view == null)
47          ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
[2546]48      }
49    }
50
51    public static void Open() {
52      if (openFileDialog == null) {
53        openFileDialog = new OpenFileDialog();
54        openFileDialog.Title = "Open Item";
55        openFileDialog.FileName = "Item";
56        openFileDialog.Multiselect = true;
57        openFileDialog.DefaultExt = "hl";
58        openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
59      }
60
61      if (openFileDialog.ShowDialog() == DialogResult.OK) {
[8818]62        OpenFiles(openFileDialog.FileNames);
[2546]63      }
64    }
[8818]65
66    public static void OpenFiles(IEnumerable<string> fileNames) {
67      foreach (string filename in fileNames) {
68        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
69        ContentManager.LoadAsync(filename, LoadingCompleted);
70      }
71    }
72
[3483]73    private static void LoadingCompleted(IStorableContent content, Exception error) {
74      try {
75        if (error != null) throw error;
[4245]76        IView view = MainFormManager.MainForm.ShowContent(content);
77        if (view == null)
78          ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
[3483]79      }
80      catch (Exception ex) {
[3758]81        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
[3483]82      }
83      finally {
[6827]84        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
[3483]85      }
86    }
[2546]87
88    public static void Save() {
[2713]89      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]90      if (activeView != null) {
[2547]91        Save(activeView);
[2546]92      }
93    }
[2713]94    private static void Save(IContentView view) {
[3483]95      IStorableContent content = view.Content as IStorableContent;
[4245]96      if (!view.Locked && content != null) {
[3483]97        if (string.IsNullOrEmpty(content.Filename))
[2961]98          SaveAs(view);
[3483]99        else {
[6827]100          ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
[4435]101          SetEnabledStateOfContentViews(content, false);
[3483]102          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
[2961]103        }
[2546]104      }
105    }
106    public static void SaveAs() {
[2713]107      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]108      if (activeView != null) {
[2547]109        SaveAs(activeView);
[2546]110      }
111    }
[2713]112    public static void SaveAs(IContentView view) {
[3483]113      IStorableContent content = view.Content as IStorableContent;
[4245]114      if (!view.Locked && content != null) {
[2961]115        if (saveFileDialog == null) {
116          saveFileDialog = new SaveFileDialog();
117          saveFileDialog.Title = "Save Item";
118          saveFileDialog.DefaultExt = "hl";
119          saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
120          saveFileDialog.FilterIndex = 2;
121        }
[3483]122        saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename;
[2546]123
[2961]124        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
[6827]125          ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
[4435]126          SetEnabledStateOfContentViews(content, false);
[2961]127          if (saveFileDialog.FilterIndex == 1) {
[3483]128            ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
[2961]129          } else {
[3483]130            ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
[2961]131          }
132        }
[2546]133      }
134    }
[3483]135    private static void SavingCompleted(IStorableContent content, Exception error) {
136      try {
[4435]137        SetEnabledStateOfContentViews(content, true);
[3483]138        if (error != null) throw error;
[6935]139        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
[3483]140      }
141      catch (Exception ex) {
[3758]142        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
[3483]143      }
144      finally {
[6827]145        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
[3483]146      }
[2547]147    }
[4435]148
149    private static void SetEnabledStateOfContentViews(IStorableContent content, bool enabled) {
[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 {
[4435]154        var views = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v.Content == content).ToList();
155        views.ForEach(v => v.Enabled = enabled);
[8587]156      });
157      #endregion
[4435]158    }
[2546]159  }
160}
Note: See TracBrowser for help on using the repository browser.