[2790] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
| 22 | using System;
|
---|
[8818] | 23 | using System.Collections.Generic;
|
---|
[9849] | 24 | using System.IO;
|
---|
[16325] | 25 | using System.Threading;
|
---|
[2546] | 26 | using System.Windows.Forms;
|
---|
[3483] | 27 | using HeuristicLab.Common;
|
---|
[11012] | 28 | using HeuristicLab.Core;
|
---|
[2546] | 29 | using HeuristicLab.MainForm;
|
---|
[3758] | 30 | using HeuristicLab.PluginInfrastructure;
|
---|
[2546] | 31 |
|
---|
| 32 | namespace HeuristicLab.Optimizer {
|
---|
| 33 | internal static class FileManager {
|
---|
[2547] | 34 | private static NewItemDialog newItemDialog;
|
---|
| 35 | private static OpenFileDialog openFileDialog;
|
---|
| 36 | private static SaveFileDialog saveFileDialog;
|
---|
[2546] | 37 |
|
---|
[2547] | 38 | static FileManager() {
|
---|
| 39 | newItemDialog = null;
|
---|
| 40 | openFileDialog = null;
|
---|
| 41 | saveFileDialog = null;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[2546] | 44 | public static void New() {
|
---|
| 45 | if (newItemDialog == null) newItemDialog = new NewItemDialog();
|
---|
| 46 | if (newItemDialog.ShowDialog() == DialogResult.OK) {
|
---|
[3557] | 47 | IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
|
---|
[4245] | 48 | if (view == null)
|
---|
| 49 | ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
|
---|
[2546] | 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public static void Open() {
|
---|
| 54 | if (openFileDialog == null) {
|
---|
| 55 | openFileDialog = new OpenFileDialog();
|
---|
| 56 | openFileDialog.Title = "Open Item";
|
---|
| 57 | openFileDialog.FileName = "Item";
|
---|
| 58 | openFileDialog.Multiselect = true;
|
---|
| 59 | openFileDialog.DefaultExt = "hl";
|
---|
| 60 | openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
[8818] | 64 | OpenFiles(openFileDialog.FileNames);
|
---|
[2546] | 65 | }
|
---|
| 66 | }
|
---|
[8818] | 67 |
|
---|
| 68 | public static void OpenFiles(IEnumerable<string> fileNames) {
|
---|
| 69 | foreach (string filename in fileNames) {
|
---|
| 70 | ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
| 71 | ContentManager.LoadAsync(filename, LoadingCompleted);
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[3483] | 75 | private static void LoadingCompleted(IStorableContent content, Exception error) {
|
---|
| 76 | try {
|
---|
| 77 | if (error != null) throw error;
|
---|
[4245] | 78 | IView view = MainFormManager.MainForm.ShowContent(content);
|
---|
| 79 | if (view == null)
|
---|
| 80 | ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
|
---|
[16325] | 81 | } catch (Exception ex) {
|
---|
[3758] | 82 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
|
---|
[16325] | 83 | } finally {
|
---|
[6827] | 84 | ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
[3483] | 85 | }
|
---|
| 86 | }
|
---|
[2546] | 87 |
|
---|
| 88 | public static void Save() {
|
---|
[2713] | 89 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
[2960] | 90 | if (activeView != null) {
|
---|
[2547] | 91 | Save(activeView);
|
---|
[2546] | 92 | }
|
---|
| 93 | }
|
---|
[2713] | 94 | private static void Save(IContentView view) {
|
---|
[3483] | 95 | IStorableContent content = view.Content as IStorableContent;
|
---|
[4245] | 96 | if (!view.Locked && content != null) {
|
---|
[3483] | 97 | if (string.IsNullOrEmpty(content.Filename))
|
---|
[2961] | 98 | SaveAs(view);
|
---|
[3483] | 99 | else {
|
---|
[9849] | 100 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
|
---|
[16325] | 101 | var cancellationTokenSource = new CancellationTokenSource();
|
---|
[16433] | 102 | AddProgressInContentViews(content, cancellationTokenSource);
|
---|
[16325] | 103 | ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted, cancellationTokenSource.Token);
|
---|
[2961] | 104 | }
|
---|
[2546] | 105 | }
|
---|
| 106 | }
|
---|
| 107 | public static void SaveAs() {
|
---|
[2713] | 108 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
[2960] | 109 | if (activeView != null) {
|
---|
[2547] | 110 | SaveAs(activeView);
|
---|
[2546] | 111 | }
|
---|
| 112 | }
|
---|
[2713] | 113 | public static void SaveAs(IContentView view) {
|
---|
[3483] | 114 | IStorableContent content = view.Content as IStorableContent;
|
---|
[4245] | 115 | if (!view.Locked && content != null) {
|
---|
[2961] | 116 | if (saveFileDialog == null) {
|
---|
| 117 | saveFileDialog = new SaveFileDialog();
|
---|
| 118 | saveFileDialog.Title = "Save Item";
|
---|
| 119 | saveFileDialog.DefaultExt = "hl";
|
---|
| 120 | saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
|
---|
| 121 | saveFileDialog.FilterIndex = 2;
|
---|
| 122 | }
|
---|
[2546] | 123 |
|
---|
[11012] | 124 | INamedItem namedItem = content as INamedItem;
|
---|
| 125 | string suggestedFileName = string.Empty;
|
---|
| 126 | if (!string.IsNullOrEmpty(content.Filename)) suggestedFileName = content.Filename;
|
---|
| 127 | else if (namedItem != null) suggestedFileName = namedItem.Name;
|
---|
| 128 | else suggestedFileName = "Item";
|
---|
| 129 |
|
---|
| 130 | saveFileDialog.FileName = suggestedFileName;
|
---|
| 131 |
|
---|
[2961] | 132 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
[9849] | 133 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
|
---|
[16325] | 134 | bool compressed = saveFileDialog.FilterIndex != 1;
|
---|
| 135 | var cancellationTokenSource = new CancellationTokenSource();
|
---|
[16433] | 136 | AddProgressInContentViews(content, cancellationTokenSource, saveFileDialog.FileName);
|
---|
[16325] | 137 |
|
---|
| 138 | ContentManager.SaveAsync(content, saveFileDialog.FileName, compressed, SavingCompleted, cancellationTokenSource.Token);
|
---|
[2961] | 139 | }
|
---|
[2546] | 140 | }
|
---|
| 141 | }
|
---|
[3483] | 142 | private static void SavingCompleted(IStorableContent content, Exception error) {
|
---|
| 143 | try {
|
---|
| 144 | if (error != null) throw error;
|
---|
[6935] | 145 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
|
---|
[16433] | 146 | } catch (OperationCanceledException) { // do nothing if canceled
|
---|
[16325] | 147 | } catch (Exception ex) {
|
---|
[3758] | 148 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
|
---|
[16325] | 149 | } finally {
|
---|
[16433] | 150 | Progress.Hide(content);
|
---|
[9849] | 151 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
|
---|
[3483] | 152 | }
|
---|
[2547] | 153 | }
|
---|
[4435] | 154 |
|
---|
[16325] | 155 | private static void AddProgressInContentViews(IStorableContent content, CancellationTokenSource cancellationTokenSource, string fileName = null) {
|
---|
| 156 | string message = string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename));
|
---|
| 157 | Progress.Show(content, message, ProgressMode.Indeterminate, cancelRequestHandler: () => cancellationTokenSource.Cancel());
|
---|
[16433] | 158 | }
|
---|
[2546] | 159 | }
|
---|
| 160 | }
|
---|