[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 |
|
---|
| 22 | using System;
|
---|
[2546] | 23 | using System.Linq;
|
---|
| 24 | using System.Windows.Forms;
|
---|
[3483] | 25 | using HeuristicLab.Common;
|
---|
[2790] | 26 | using HeuristicLab.Core.Views;
|
---|
[2546] | 27 | using HeuristicLab.MainForm;
|
---|
[3758] | 28 | using HeuristicLab.PluginInfrastructure;
|
---|
[2546] | 29 |
|
---|
| 30 | namespace HeuristicLab.Optimizer {
|
---|
| 31 | internal static class FileManager {
|
---|
[2547] | 32 | private static NewItemDialog newItemDialog;
|
---|
| 33 | private static OpenFileDialog openFileDialog;
|
---|
| 34 | private static SaveFileDialog saveFileDialog;
|
---|
[2546] | 35 |
|
---|
[2547] | 36 | static FileManager() {
|
---|
| 37 | newItemDialog = null;
|
---|
| 38 | openFileDialog = null;
|
---|
| 39 | saveFileDialog = null;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[2546] | 42 | public static void New() {
|
---|
| 43 | if (newItemDialog == null) newItemDialog = new NewItemDialog();
|
---|
| 44 | if (newItemDialog.ShowDialog() == DialogResult.OK) {
|
---|
[3557] | 45 | IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
|
---|
[3483] | 46 | 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] | 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) {
|
---|
| 62 | ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
| 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;
|
---|
| 70 | Invoke(delegate() {
|
---|
[3557] | 71 | IView view = MainFormManager.MainForm.ShowContent(content);
|
---|
[3483] | 72 | if (view == null) MessageBox.Show("There is no view for the loaded item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 73 | });
|
---|
| 74 | }
|
---|
| 75 | catch (Exception ex) {
|
---|
[3758] | 76 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
|
---|
[3483] | 77 | }
|
---|
| 78 | finally {
|
---|
| 79 | ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[2546] | 82 |
|
---|
| 83 | public static void Save() {
|
---|
[2713] | 84 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
[2960] | 85 | if (activeView != null) {
|
---|
[2547] | 86 | Save(activeView);
|
---|
[2546] | 87 | }
|
---|
| 88 | }
|
---|
[2713] | 89 | private static void Save(IContentView view) {
|
---|
[3483] | 90 | IStorableContent content = view.Content as IStorableContent;
|
---|
| 91 | if (!view.Locked && (content != null)) {
|
---|
| 92 | if (string.IsNullOrEmpty(content.Filename))
|
---|
[2961] | 93 | SaveAs(view);
|
---|
[3483] | 94 | else {
|
---|
| 95 | ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
| 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;
|
---|
| 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) {
|
---|
[3483] | 119 | ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
[2961] | 120 | if (saveFileDialog.FilterIndex == 1) {
|
---|
[3483] | 121 | ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
|
---|
[2961] | 122 | } else {
|
---|
[3483] | 123 | ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
|
---|
[2961] | 124 | }
|
---|
| 125 | }
|
---|
[2546] | 126 | }
|
---|
| 127 | }
|
---|
[3483] | 128 | private static void SavingCompleted(IStorableContent content, Exception error) {
|
---|
| 129 | try {
|
---|
| 130 | if (error != null) throw error;
|
---|
| 131 | Invoke(delegate() {
|
---|
| 132 | ((OptimizerMainForm)MainFormManager.MainForm).UpdateTitle();
|
---|
| 133 | });
|
---|
| 134 | }
|
---|
| 135 | catch (Exception ex) {
|
---|
[3758] | 136 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
|
---|
[3483] | 137 | }
|
---|
| 138 | finally {
|
---|
| 139 | ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
| 140 | }
|
---|
[2547] | 141 | }
|
---|
| 142 |
|
---|
| 143 | private static void Invoke(Action a) {
|
---|
| 144 | Form form = MainFormManager.MainForm as Form;
|
---|
| 145 | if (form.InvokeRequired)
|
---|
| 146 | form.Invoke(a);
|
---|
| 147 | else
|
---|
| 148 | a.Invoke();
|
---|
| 149 | }
|
---|
[2546] | 150 | }
|
---|
| 151 | }
|
---|