[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;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Optimizer {
|
---|
| 30 | internal static class FileManager {
|
---|
[2547] | 31 | private static NewItemDialog newItemDialog;
|
---|
| 32 | private static OpenFileDialog openFileDialog;
|
---|
| 33 | private static SaveFileDialog saveFileDialog;
|
---|
[2546] | 34 |
|
---|
[2547] | 35 | static FileManager() {
|
---|
| 36 | newItemDialog = null;
|
---|
| 37 | openFileDialog = null;
|
---|
| 38 | saveFileDialog = null;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[2546] | 41 | public static void New() {
|
---|
| 42 | if (newItemDialog == null) newItemDialog = new NewItemDialog();
|
---|
| 43 | if (newItemDialog.ShowDialog() == DialogResult.OK) {
|
---|
[2555] | 44 | IView view = MainFormManager.CreateDefaultView(newItemDialog.Item);
|
---|
[3483] | 45 | if (view == null) MessageBox.Show("There is no view for the new item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 46 | else view.Show();
|
---|
[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() {
|
---|
| 71 | IView view = MainFormManager.CreateDefaultView(content);
|
---|
| 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 | else view.Show();
|
---|
| 74 | });
|
---|
| 75 | }
|
---|
| 76 | catch (Exception ex) {
|
---|
| 77 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 78 | }
|
---|
| 79 | finally {
|
---|
| 80 | ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
[2546] | 83 |
|
---|
| 84 | public static void Save() {
|
---|
[2713] | 85 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
[2960] | 86 | if (activeView != null) {
|
---|
[2547] | 87 | Save(activeView);
|
---|
[2546] | 88 | }
|
---|
| 89 | }
|
---|
[2713] | 90 | private static void Save(IContentView view) {
|
---|
[3483] | 91 | IStorableContent content = view.Content as IStorableContent;
|
---|
| 92 | if (!view.Locked && (content != null)) {
|
---|
| 93 | if (string.IsNullOrEmpty(content.Filename))
|
---|
[2961] | 94 | SaveAs(view);
|
---|
[3483] | 95 | else {
|
---|
| 96 | ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
| 97 | ((Form)MainFormManager.MainForm).Enabled = false;
|
---|
| 98 | ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted);
|
---|
[2961] | 99 | }
|
---|
[2546] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | public static void SaveAs() {
|
---|
[2713] | 103 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
[2960] | 104 | if (activeView != null) {
|
---|
[2547] | 105 | SaveAs(activeView);
|
---|
[2546] | 106 | }
|
---|
| 107 | }
|
---|
[2713] | 108 | public static void SaveAs(IContentView view) {
|
---|
[3483] | 109 | IStorableContent content = view.Content as IStorableContent;
|
---|
| 110 | if (!view.Locked && (content != null)) {
|
---|
[2961] | 111 | if (saveFileDialog == null) {
|
---|
| 112 | saveFileDialog = new SaveFileDialog();
|
---|
| 113 | saveFileDialog.Title = "Save Item";
|
---|
| 114 | saveFileDialog.DefaultExt = "hl";
|
---|
| 115 | saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
|
---|
| 116 | saveFileDialog.FilterIndex = 2;
|
---|
| 117 | }
|
---|
[3483] | 118 | saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename;
|
---|
[2546] | 119 |
|
---|
[2961] | 120 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
[3483] | 121 | ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor();
|
---|
| 122 | ((Form)MainFormManager.MainForm).Enabled = false;
|
---|
[2961] | 123 | if (saveFileDialog.FilterIndex == 1) {
|
---|
[3483] | 124 | ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted);
|
---|
[2961] | 125 | } else {
|
---|
[3483] | 126 | ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted);
|
---|
[2961] | 127 | }
|
---|
| 128 | }
|
---|
[2546] | 129 | }
|
---|
| 130 | }
|
---|
| 131 | public static void SaveAll() {
|
---|
[2961] | 132 | foreach (IContentView view in MainFormManager.MainForm.Views.OfType<IContentView>())
|
---|
| 133 | Save(view);
|
---|
[2546] | 134 | }
|
---|
[3483] | 135 | private static void SavingCompleted(IStorableContent content, Exception error) {
|
---|
| 136 | try {
|
---|
| 137 | if (error != null) throw error;
|
---|
| 138 | Invoke(delegate() {
|
---|
| 139 | ((OptimizerMainForm)MainFormManager.MainForm).UpdateTitle();
|
---|
| 140 | ((Form)MainFormManager.MainForm).Enabled = true;
|
---|
| 141 | });
|
---|
| 142 | }
|
---|
| 143 | catch (Exception ex) {
|
---|
| 144 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 145 | }
|
---|
| 146 | finally {
|
---|
| 147 | ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
| 148 | }
|
---|
[2547] | 149 | }
|
---|
| 150 |
|
---|
| 151 | private static void Invoke(Action a) {
|
---|
| 152 | Form form = MainFormManager.MainForm as Form;
|
---|
| 153 | if (form.InvokeRequired)
|
---|
| 154 | form.Invoke(a);
|
---|
| 155 | else
|
---|
| 156 | a.Invoke();
|
---|
| 157 | }
|
---|
[2546] | 158 | }
|
---|
| 159 | }
|
---|