Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4054 was 3758, checked in by swagner, 15 years ago

Implemented ErrorDialog and OperatorExecutionException (#1007)

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.Linq;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
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) MessageBox.Show("There is no view for the new item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
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        Invoke(delegate() {
71          IView view = MainFormManager.MainForm.ShowContent(content);
72          if (view == null) MessageBox.Show("There is no view for the loaded item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
73        });
74      }
75      catch (Exception ex) {
76        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
77      }
78      finally {
79        ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
80      }
81    }
82
83    public static void Save() {
84      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
85      if (activeView != null) {
86        Save(activeView);
87      }
88    }
89    private static void Save(IContentView view) {
90      IStorableContent content = view.Content as IStorableContent;
91      if (!view.Locked && (content != null)) {
92        if (string.IsNullOrEmpty(content.Filename))
93          SaveAs(view);
94        else {
95          ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
96          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
97        }
98      }
99    }
100    public static void SaveAs() {
101      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
102      if (activeView != null) {
103        SaveAs(activeView);
104      }
105    }
106    public static void SaveAs(IContentView view) {
107      IStorableContent content = view.Content as IStorableContent;
108      if (!view.Locked && (content != null)) {
109        if (saveFileDialog == null) {
110          saveFileDialog = new SaveFileDialog();
111          saveFileDialog.Title = "Save Item";
112          saveFileDialog.DefaultExt = "hl";
113          saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
114          saveFileDialog.FilterIndex = 2;
115        }
116        saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename;
117
118        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
119          ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
120          if (saveFileDialog.FilterIndex == 1) {
121            ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
122          } else {
123            ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
124          }
125        }
126      }
127    }
128    private static void SavingCompleted(IStorableContent content, Exception error) {
129      try {
130        if (error != null) throw error;
131        Invoke(delegate() {
132          ((OptimizerMainForm)MainFormManager.MainForm).UpdateTitle();
133        });
134      }
135      catch (Exception ex) {
136        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
137      }
138      finally {
139        ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
140      }
141    }
142
143    private static void Invoke(Action a) {
144      Form form = MainFormManager.MainForm as Form;
145      if (form.InvokeRequired)
146        form.Invoke(a);
147      else
148        a.Invoke();
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.