[2790] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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;
|
---|
[2546] | 25 | using System.Windows.Forms;
|
---|
[3483] | 26 | using HeuristicLab.Common;
|
---|
[11012] | 27 | using HeuristicLab.Core;
|
---|
[2546] | 28 | using HeuristicLab.MainForm;
|
---|
[3758] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
[2546] | 30 |
|
---|
| 31 | namespace HeuristicLab.Optimizer {
|
---|
| 32 | internal static class FileManager {
|
---|
[2547] | 33 | private static NewItemDialog newItemDialog;
|
---|
| 34 | private static OpenFileDialog openFileDialog;
|
---|
| 35 | private static SaveFileDialog saveFileDialog;
|
---|
[2546] | 36 |
|
---|
[2547] | 37 | static FileManager() {
|
---|
| 38 | newItemDialog = null;
|
---|
| 39 | openFileDialog = null;
|
---|
| 40 | saveFileDialog = null;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[2546] | 43 | public static void New() {
|
---|
| 44 | if (newItemDialog == null) newItemDialog = new NewItemDialog();
|
---|
| 45 | if (newItemDialog.ShowDialog() == DialogResult.OK) {
|
---|
[3557] | 46 | IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
|
---|
[4245] | 47 | if (view == null)
|
---|
| 48 | ErrorHandling.ShowErrorDialog("There is no view for the new item. It cannot be displayed.", new InvalidOperationException("No View Available"));
|
---|
[2546] | 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public static void Open() {
|
---|
| 53 | if (openFileDialog == null) {
|
---|
| 54 | openFileDialog = new OpenFileDialog();
|
---|
| 55 | openFileDialog.Title = "Open Item";
|
---|
| 56 | openFileDialog.FileName = "Item";
|
---|
| 57 | openFileDialog.Multiselect = true;
|
---|
| 58 | openFileDialog.DefaultExt = "hl";
|
---|
| 59 | openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
[8818] | 63 | OpenFiles(openFileDialog.FileNames);
|
---|
[2546] | 64 | }
|
---|
| 65 | }
|
---|
[8818] | 66 |
|
---|
| 67 | public static void OpenFiles(IEnumerable<string> fileNames) {
|
---|
| 68 | foreach (string filename in fileNames) {
|
---|
| 69 | ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
| 70 | ContentManager.LoadAsync(filename, LoadingCompleted);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[3483] | 74 | private static void LoadingCompleted(IStorableContent content, Exception error) {
|
---|
| 75 | try {
|
---|
| 76 | if (error != null) throw error;
|
---|
[4245] | 77 | IView view = MainFormManager.MainForm.ShowContent(content);
|
---|
| 78 | if (view == null)
|
---|
| 79 | ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
|
---|
[3483] | 80 | }
|
---|
| 81 | catch (Exception ex) {
|
---|
[3758] | 82 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
|
---|
[3483] | 83 | }
|
---|
| 84 | finally {
|
---|
[6827] | 85 | ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
[3483] | 86 | }
|
---|
| 87 | }
|
---|
[2546] | 88 |
|
---|
| 89 | public static void Save() {
|
---|
[2713] | 90 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
[2960] | 91 | if (activeView != null) {
|
---|
[2547] | 92 | Save(activeView);
|
---|
[2546] | 93 | }
|
---|
| 94 | }
|
---|
[2713] | 95 | private static void Save(IContentView view) {
|
---|
[3483] | 96 | IStorableContent content = view.Content as IStorableContent;
|
---|
[4245] | 97 | if (!view.Locked && content != null) {
|
---|
[3483] | 98 | if (string.IsNullOrEmpty(content.Filename))
|
---|
[2961] | 99 | SaveAs(view);
|
---|
[3483] | 100 | else {
|
---|
[9849] | 101 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().SetAppStartingCursor();
|
---|
[9851] | 102 | SetSaveOperationProgressInContentViews(content, true);
|
---|
[3483] | 103 | ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
|
---|
[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();
|
---|
[9851] | 134 | SetSaveOperationProgressInContentViews(content, true, saveFileDialog.FileName);
|
---|
[2961] | 135 | if (saveFileDialog.FilterIndex == 1) {
|
---|
[3483] | 136 | ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
|
---|
[2961] | 137 | } else {
|
---|
[3483] | 138 | ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
|
---|
[2961] | 139 | }
|
---|
| 140 | }
|
---|
[2546] | 141 | }
|
---|
| 142 | }
|
---|
[3483] | 143 | private static void SavingCompleted(IStorableContent content, Exception error) {
|
---|
| 144 | try {
|
---|
| 145 | if (error != null) throw error;
|
---|
[6935] | 146 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
|
---|
[3483] | 147 | }
|
---|
| 148 | catch (Exception ex) {
|
---|
[3758] | 149 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
|
---|
[3483] | 150 | }
|
---|
| 151 | finally {
|
---|
[9851] | 152 | SetSaveOperationProgressInContentViews(content, false);
|
---|
[9849] | 153 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().ResetAppStartingCursor();
|
---|
[3483] | 154 | }
|
---|
[2547] | 155 | }
|
---|
[4435] | 156 |
|
---|
[9851] | 157 | private static void SetSaveOperationProgressInContentViews(IStorableContent content, bool showProgress, string fileName = null) {
|
---|
[6935] | 158 | HeuristicLab.MainForm.WindowsForms.MainForm mainForm = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>();
|
---|
[8587] | 159 | #region Mono Compatibility
|
---|
| 160 | // removed the InvokeRequired check because of Mono
|
---|
| 161 | mainForm.Invoke((Action)delegate {
|
---|
[9851] | 162 | if (showProgress) {
|
---|
[9901] | 163 | mainForm.AddOperationProgressToContent(content, string.Format("Saving to file \"{0}\"...", Path.GetFileName(fileName ?? content.Filename)));
|
---|
[9851] | 164 | } else
|
---|
[9849] | 165 | mainForm.RemoveOperationProgressFromContent(content);
|
---|
[8587] | 166 | });
|
---|
| 167 | #endregion
|
---|
[4435] | 168 | }
|
---|
[2546] | 169 | }
|
---|
| 170 | }
|
---|