Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4245 was 4245, checked in by mkommend, 14 years ago

Removed cloning of saved contents and refactored FileManager (ticket #1143).

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