Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Optimizer/3.3/FileManager.cs @ 12283

Last change on this file since 12283 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.MainForm;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Optimizer {
31  internal static class FileManager {
32    private static NewItemDialog newItemDialog;
33    private static OpenFileDialog openFileDialog;
34    private static SaveFileDialog saveFileDialog;
35
36    static FileManager() {
37      newItemDialog = null;
38      openFileDialog = null;
39      saveFileDialog = null;
40    }
41
42    public static void New() {
43      if (newItemDialog == null) newItemDialog = new NewItemDialog();
44      if (newItemDialog.ShowDialog() == DialogResult.OK) {
45        IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
46        if (view == null)
47          ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
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) {
62        OpenFiles(openFileDialog.FileNames);
63      }
64    }
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
73    private static void LoadingCompleted(IStorableContent content, Exception error) {
74      try {
75        if (error != null) throw error;
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"));
79      }
80      catch (Exception ex) {
81        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
82      }
83      finally {
84        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
85      }
86    }
87
88    public static void Save() {
89      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
90      if (activeView != null) {
91        Save(activeView);
92      }
93    }
94    private static void Save(IContentView view) {
95      IStorableContent content = view.Content as IStorableContent;
96      if (!view.Locked && content != null) {
97        if (string.IsNullOrEmpty(content.Filename))
98          SaveAs(view);
99        else {
100          ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
101          SetEnabledStateOfContentViews(content, false);
102          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
103        }
104      }
105    }
106    public static void SaveAs() {
107      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
108      if (activeView != null) {
109        SaveAs(activeView);
110      }
111    }
112    public static void SaveAs(IContentView view) {
113      IStorableContent content = view.Content as IStorableContent;
114      if (!view.Locked && content != null) {
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        }
122        saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename;
123
124        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
125          ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
126          SetEnabledStateOfContentViews(content, false);
127          if (saveFileDialog.FilterIndex == 1) {
128            ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
129          } else {
130            ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
131          }
132        }
133      }
134    }
135    private static void SavingCompleted(IStorableContent content, Exception error) {
136      try {
137        SetEnabledStateOfContentViews(content, true);
138        if (error != null) throw error;
139        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
140      }
141      catch (Exception ex) {
142        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
143      }
144      finally {
145        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
146      }
147    }
148
149    private static void SetEnabledStateOfContentViews(IStorableContent content, bool enabled) {
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        var views = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v.Content == content).ToList();
155        views.ForEach(v => v.Enabled = enabled);
156      });
157      #endregion
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.