Free cookie consent management tool by TermsFeed Policy Generator

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

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

changed logic of showing new views (ticket #972)

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