1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Threading;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.PluginInfrastructure;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimizer {
|
---|
33 | internal static class FileManager {
|
---|
34 | private static NewItemDialog newItemDialog;
|
---|
35 | private static OpenFileDialog openFileDialog;
|
---|
36 | private static SaveFileDialog saveFileDialog;
|
---|
37 |
|
---|
38 | static FileManager() {
|
---|
39 | newItemDialog = null;
|
---|
40 | openFileDialog = null;
|
---|
41 | saveFileDialog = null;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public static void New() {
|
---|
45 | if (newItemDialog == null) newItemDialog = new NewItemDialog();
|
---|
46 | if (newItemDialog.ShowDialog() == DialogResult.OK) {
|
---|
47 | IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
|
---|
48 | if (view == null)
|
---|
49 | ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
|
---|
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) {
|
---|
64 | OpenFiles(openFileDialog.FileNames);
|
---|
65 | }
|
---|
66 | }
|
---|
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 |
|
---|
75 | private static void LoadingCompleted(IStorableContent content, Exception error) {
|
---|
76 | try {
|
---|
77 | if (error != null) throw error;
|
---|
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"));
|
---|
81 | } catch (Exception ex) {
|
---|
82 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
|
---|
83 | } finally {
|
---|
84 | ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public static void Save() {
|
---|
89 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
90 | if (activeView != null) {
|
---|
91 | Save(activeView);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | private static void Save(IContentView view) {
|
---|
95 | IStorableContent content = view.Content as IStorableContent;
|
---|
96 | if (!view.Locked && content != null) {
|
---|
97 | if (string.IsNullOrEmpty(content.Filename))
|
---|
98 | SaveAs(view);
|
---|
99 | else {
|
---|
100 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
|
---|
101 | var cancellationTokenSource = new CancellationTokenSource();
|
---|
102 | SetSaveOperationProgressInContentViews(content, true);
|
---|
103 | ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted, cancellationTokenSource.Token);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | public static void SaveAs() {
|
---|
108 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
109 | if (activeView != null) {
|
---|
110 | SaveAs(activeView);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | public static void SaveAs(IContentView view) {
|
---|
114 | IStorableContent content = view.Content as IStorableContent;
|
---|
115 | if (!view.Locked && content != null) {
|
---|
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 | }
|
---|
123 |
|
---|
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 |
|
---|
132 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
133 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
|
---|
134 | bool compressed = saveFileDialog.FilterIndex != 1;
|
---|
135 | var cancellationTokenSource = new CancellationTokenSource();
|
---|
136 | SetSaveOperationProgressInContentViews(content, compressed, saveFileDialog.FileName);
|
---|
137 |
|
---|
138 | ContentManager.SaveAsync(content, saveFileDialog.FileName, compressed, SavingCompleted, cancellationTokenSource.Token);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | private static void SavingCompleted(IStorableContent content, Exception error) {
|
---|
143 | try {
|
---|
144 | if (error != null) throw error;
|
---|
145 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
|
---|
146 | } catch (Exception ex) {
|
---|
147 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
|
---|
148 | } finally {
|
---|
149 | SetSaveOperationProgressInContentViews(content, false);
|
---|
150 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null) {
|
---|
155 | HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
|
---|
156 | #region Mono Compatibility
|
---|
157 | // removed the InvokeRequired check because of Mono
|
---|
158 | mainForm.Invoke((Action)delegate {
|
---|
159 | if (showProgress) {
|
---|
160 | mainForm.AddOperationProgressToContent(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
|
---|
161 | } else
|
---|
162 | mainForm.RemoveOperationProgressFromContent(content);
|
---|
163 | });
|
---|
164 | #endregion
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* For later merge with #2845
|
---|
168 | private static void AddProgressInContentViews(IStorableContent content, CancellationTokenSource cancellationTokenSource, string fileName = null) {
|
---|
169 | string message = string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename));
|
---|
170 | Progress.Show(content, message, ProgressMode.Indeterminate, cancelRequestHandler: () => cancellationTokenSource.Cancel());
|
---|
171 | }*/
|
---|
172 | }
|
---|
173 | }
|
---|