Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7317 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 6.2 KB
RevLine 
[2790]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2790]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;
[4245]23using System.Linq;
[2546]24using System.Windows.Forms;
[3483]25using HeuristicLab.Common;
[2546]26using HeuristicLab.MainForm;
[3758]27using HeuristicLab.PluginInfrastructure;
[2546]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);
[4245]45        if (view == null)
46          ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
[2546]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) {
[3483]61        foreach (string filename in openFileDialog.FileNames) {
[6827]62          ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
[3483]63          ContentManager.LoadAsync(filename, LoadingCompleted);
64        }
[2546]65      }
66    }
[3483]67    private static void LoadingCompleted(IStorableContent content, Exception error) {
68      try {
69        if (error != null) throw error;
[4245]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"));
[3483]73      }
74      catch (Exception ex) {
[3758]75        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
[3483]76      }
77      finally {
[6827]78        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
[3483]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;
[4245]90      if (!view.Locked && content != null) {
[3483]91        if (string.IsNullOrEmpty(content.Filename))
[2961]92          SaveAs(view);
[3483]93        else {
[6827]94          ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
[4435]95          SetEnabledStateOfContentViews(content, false);
[3483]96          ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
[2961]97        }
[2546]98      }
99    }
100    public static void SaveAs() {
[2713]101      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[2960]102      if (activeView != null) {
[2547]103        SaveAs(activeView);
[2546]104      }
105    }
[2713]106    public static void SaveAs(IContentView view) {
[3483]107      IStorableContent content = view.Content as IStorableContent;
[4245]108      if (!view.Locked && content != null) {
[2961]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        }
[3483]116        saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename;
[2546]117
[2961]118        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
[6827]119          ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
[4435]120          SetEnabledStateOfContentViews(content, false);
[2961]121          if (saveFileDialog.FilterIndex == 1) {
[3483]122            ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
[2961]123          } else {
[3483]124            ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
[2961]125          }
126        }
[2546]127      }
128    }
[3483]129    private static void SavingCompleted(IStorableContent content, Exception error) {
130      try {
[4435]131        SetEnabledStateOfContentViews(content, true);
[3483]132        if (error != null) throw error;
[6935]133        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
[3483]134      }
135      catch (Exception ex) {
[3758]136        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
[3483]137      }
138      finally {
[6827]139        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
[3483]140      }
[2547]141    }
[4435]142
143    private static void SetEnabledStateOfContentViews(IStorableContent content, bool enabled) {
[6935]144      HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
[4435]145      if (mainForm.InvokeRequired)
146        mainForm.Invoke((Action<IStorableContent, bool>)SetEnabledStateOfContentViews, content, enabled);
147      else {
148        var views = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v.Content == content).ToList();
149        views.ForEach(v => v.Enabled = enabled);
150      }
151    }
[2546]152  }
153}
Note: See TracBrowser for help on using the repository browser.