[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.Windows.Forms;
|
---|
[3483] | 24 | using HeuristicLab.Common;
|
---|
[2546] | 25 | using HeuristicLab.MainForm;
|
---|
[3758] | 26 | using HeuristicLab.PluginInfrastructure;
|
---|
[2546] | 27 |
|
---|
| 28 | namespace HeuristicLab.Optimizer {
|
---|
| 29 | internal static class FileManager {
|
---|
[2547] | 30 | private static NewItemDialog newItemDialog;
|
---|
| 31 | private static OpenFileDialog openFileDialog;
|
---|
| 32 | private static SaveFileDialog saveFileDialog;
|
---|
[2546] | 33 |
|
---|
[2547] | 34 | static FileManager() {
|
---|
| 35 | newItemDialog = null;
|
---|
| 36 | openFileDialog = null;
|
---|
| 37 | saveFileDialog = null;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[2546] | 40 | public static void New() {
|
---|
| 41 | if (newItemDialog == null) newItemDialog = new NewItemDialog();
|
---|
| 42 | if (newItemDialog.ShowDialog() == DialogResult.OK) {
|
---|
[3557] | 43 | IView view = MainFormManager.MainForm.ShowContent(newItemDialog.Item);
|
---|
[3483] | 44 | 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] | 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public static void Open() {
|
---|
| 49 | if (openFileDialog == null) {
|
---|
| 50 | openFileDialog = new OpenFileDialog();
|
---|
| 51 | openFileDialog.Title = "Open Item";
|
---|
| 52 | openFileDialog.FileName = "Item";
|
---|
| 53 | openFileDialog.Multiselect = true;
|
---|
| 54 | openFileDialog.DefaultExt = "hl";
|
---|
| 55 | openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
[3483] | 59 | foreach (string filename in openFileDialog.FileNames) {
|
---|
| 60 | ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
| 61 | ContentManager.LoadAsync(filename, LoadingCompleted);
|
---|
| 62 | }
|
---|
[2546] | 63 | }
|
---|
| 64 | }
|
---|
[3483] | 65 | private static void LoadingCompleted(IStorableContent content, Exception error) {
|
---|
| 66 | try {
|
---|
| 67 | if (error != null) throw error;
|
---|
| 68 | Invoke(delegate() {
|
---|
[3557] | 69 | IView view = MainFormManager.MainForm.ShowContent(content);
|
---|
[3483] | 70 | if (view == null) MessageBox.Show("There is no view for the loaded item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 71 | });
|
---|
| 72 | }
|
---|
| 73 | catch (Exception ex) {
|
---|
[3758] | 74 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
|
---|
[3483] | 75 | }
|
---|
| 76 | finally {
|
---|
| 77 | ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[2546] | 80 |
|
---|
| 81 | public static void Save() {
|
---|
[2713] | 82 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
[2960] | 83 | if (activeView != null) {
|
---|
[2547] | 84 | Save(activeView);
|
---|
[2546] | 85 | }
|
---|
| 86 | }
|
---|
[2713] | 87 | private static void Save(IContentView view) {
|
---|
[3483] | 88 | IStorableContent content = view.Content as IStorableContent;
|
---|
| 89 | if (!view.Locked && (content != null)) {
|
---|
| 90 | if (string.IsNullOrEmpty(content.Filename))
|
---|
[2961] | 91 | SaveAs(view);
|
---|
[3483] | 92 | else {
|
---|
| 93 | ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
| 94 | ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
|
---|
[2961] | 95 | }
|
---|
[2546] | 96 | }
|
---|
| 97 | }
|
---|
| 98 | public static void SaveAs() {
|
---|
[2713] | 99 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
[2960] | 100 | if (activeView != null) {
|
---|
[2547] | 101 | SaveAs(activeView);
|
---|
[2546] | 102 | }
|
---|
| 103 | }
|
---|
[2713] | 104 | public static void SaveAs(IContentView view) {
|
---|
[3483] | 105 | IStorableContent content = view.Content as IStorableContent;
|
---|
| 106 | if (!view.Locked && (content != null)) {
|
---|
[2961] | 107 | if (saveFileDialog == null) {
|
---|
| 108 | saveFileDialog = new SaveFileDialog();
|
---|
| 109 | saveFileDialog.Title = "Save Item";
|
---|
| 110 | saveFileDialog.DefaultExt = "hl";
|
---|
| 111 | saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
|
---|
| 112 | saveFileDialog.FilterIndex = 2;
|
---|
| 113 | }
|
---|
[3483] | 114 | saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename;
|
---|
[2546] | 115 |
|
---|
[2961] | 116 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
[3483] | 117 | ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
[2961] | 118 | if (saveFileDialog.FilterIndex == 1) {
|
---|
[3483] | 119 | ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
|
---|
[2961] | 120 | } else {
|
---|
[3483] | 121 | ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
|
---|
[2961] | 122 | }
|
---|
| 123 | }
|
---|
[2546] | 124 | }
|
---|
| 125 | }
|
---|
[3483] | 126 | private static void SavingCompleted(IStorableContent content, Exception error) {
|
---|
| 127 | try {
|
---|
| 128 | if (error != null) throw error;
|
---|
| 129 | Invoke(delegate() {
|
---|
| 130 | ((OptimizerMainForm)MainFormManager.MainForm).UpdateTitle();
|
---|
| 131 | });
|
---|
| 132 | }
|
---|
| 133 | catch (Exception ex) {
|
---|
[3758] | 134 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot save file.", ex);
|
---|
[3483] | 135 | }
|
---|
| 136 | finally {
|
---|
| 137 | ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
| 138 | }
|
---|
[2547] | 139 | }
|
---|
| 140 |
|
---|
| 141 | private static void Invoke(Action a) {
|
---|
| 142 | Form form = MainFormManager.MainForm as Form;
|
---|
| 143 | if (form.InvokeRequired)
|
---|
| 144 | form.Invoke(a);
|
---|
| 145 | else
|
---|
| 146 | a.Invoke();
|
---|
| 147 | }
|
---|
[2546] | 148 | }
|
---|
| 149 | }
|
---|