Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4128 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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