Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimizer/3.3/FileManager.cs @ 2790

Last change on this file since 2790 was 2790, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 8.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Threading;
27using System.Windows.Forms;
28using HeuristicLab.Core;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31using HeuristicLab.Persistence.Default.Xml;
32
33namespace HeuristicLab.Optimizer {
34  internal static class FileManager {
35
36    #region Private Class FileInfo
37    private class FileInfo {
38      public string Filename { get; set; }
39      public bool Compressed { get; set; }
40
41      public FileInfo(string filename, bool compressed) {
42        Filename = filename;
43        Compressed = compressed;
44      }
45      public FileInfo(string filename)
46        : this(filename, true) {
47      }
48      public FileInfo()
49        : this(string.Empty, true) {
50      }
51    }
52    #endregion
53
54    private static Dictionary<IContentView, FileInfo> files;
55    private static NewItemDialog newItemDialog;
56    private static OpenFileDialog openFileDialog;
57    private static SaveFileDialog saveFileDialog;
58    private static int waitingCursors;
59    private static int newDocumentsCounter;
60
61    static FileManager() {
62      files = new Dictionary<IContentView, FileInfo>();
63      newItemDialog = null;
64      openFileDialog = null;
65      saveFileDialog = null;
66      waitingCursors = 0;
67      newDocumentsCounter = 1;
68      // NOTE: Events fired by the main form are registered in HeuristicLabOptimizerApplication.
69    }
70
71    public static void New() {
72      if (newItemDialog == null) newItemDialog = new NewItemDialog();
73      if (newItemDialog.ShowDialog() == DialogResult.OK) {
74        IView view = MainFormManager.CreateDefaultView(newItemDialog.Item);
75        if (view == null) {
76          MessageBox.Show("There is no view for the new item. It cannot be displayed. ", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
77        } else {
78          if (view is IContentView) {
79            view.Caption = "Item" + newDocumentsCounter.ToString() + ".hl";
80            newDocumentsCounter++;
81          }
82          view.Show();
83        }
84      }
85    }
86
87    public static void Open() {
88      if (openFileDialog == null) {
89        openFileDialog = new OpenFileDialog();
90        openFileDialog.Title = "Open Item";
91        openFileDialog.FileName = "Item";
92        openFileDialog.Multiselect = true;
93        openFileDialog.DefaultExt = "hl";
94        openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
95      }
96
97      if (openFileDialog.ShowDialog() == DialogResult.OK) {
98        foreach (string filename in openFileDialog.FileNames)
99          LoadItemAsync(filename);
100      }
101    }
102
103    public static void Save() {
104      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
105      if ((activeView != null) && (CreatableAttribute.IsCreatable(activeView.Content.GetType()))) {
106        Save(activeView);
107      }
108    }
109    private static void Save(IContentView view) {
110      if ((!files.ContainsKey(view)) || (!File.Exists(files[view].Filename))) {
111        SaveAs(view);
112      } else {
113        if (files[view].Compressed)
114          SaveItemAsync(view, files[view].Filename, 9);
115        else
116          SaveItemAsync(view, files[view].Filename, 0);
117      }
118    }
119
120    public static void SaveAs() {
121      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
122      if ((activeView != null) && (CreatableAttribute.IsCreatable(activeView.Content.GetType()))) {
123        SaveAs(activeView);
124      }
125    }
126    public static void SaveAs(IContentView view) {
127      if (saveFileDialog == null) {
128        saveFileDialog = new SaveFileDialog();
129        saveFileDialog.Title = "Save Item";
130        saveFileDialog.DefaultExt = "hl";
131        saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
132        saveFileDialog.FilterIndex = 2;
133      }
134
135      if (!files.ContainsKey(view)) {
136        files.Add(view, new FileInfo());
137        saveFileDialog.FileName = view.Caption;
138      } else {
139        saveFileDialog.FileName = files[view].Filename;
140      }
141      if (! files[view].Compressed)
142        saveFileDialog.FilterIndex = 1;
143      else
144        saveFileDialog.FilterIndex = 2;
145
146      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
147        if (saveFileDialog.FilterIndex == 1) {
148          SaveItemAsync(view, saveFileDialog.FileName, 0);
149        } else {
150          SaveItemAsync(view, saveFileDialog.FileName, 9);
151        }
152      }
153    }
154
155    public static void SaveAll() {
156      var views = from v in MainFormManager.MainForm.Views
157                  where v is IContentView
158                  where CreatableAttribute.IsCreatable(((IContentView)v).Content.GetType())
159                  select v as IContentView;
160
161      foreach (IContentView view in views) {
162        Save(view);
163      }
164    }
165
166    // NOTE: This event is fired by the main form. It is registered in HeuristicLabOptimizerApplication.
167    internal static void ViewClosed(object sender, ViewEventArgs e) {
168      IContentView view = e.View as IContentView;
169      if (view != null) files.Remove(view);
170    }
171
172    #region Asynchronous Save/Load Operations
173    private static void Invoke(Action a) {
174      Form form = MainFormManager.MainForm as Form;
175      if (form.InvokeRequired)
176        form.Invoke(a);
177      else
178        a.Invoke();
179    }
180
181    private static void SaveItemAsync(IContentView view, string filename, int compression) {
182      ThreadPool.QueueUserWorkItem(
183        new WaitCallback(
184          delegate(object arg) {
185            try {
186              DisableView(view);
187              SetWaitingCursor();
188              XmlGenerator.Serialize(view.Content, filename, compression);
189              Invoke(delegate() {
190                view.Caption = Path.GetFileName(filename);
191                files[view].Filename = filename;
192                files[view].Compressed = compression > 0;
193              });
194            }
195            catch (Exception ex) {
196              Auxiliary.ShowErrorMessageBox(ex);
197            } finally {
198              ResetWaitingCursor();
199              EnableView(view);
200            }
201          }
202        )
203      );
204    }
205    private static void LoadItemAsync(string filename) {
206      ThreadPool.QueueUserWorkItem(
207        new WaitCallback(
208          delegate(object arg) {
209            try {
210              SetWaitingCursor();
211              IItem item = (IItem)XmlParser.Deserialize(filename);
212              Invoke(delegate() {
213                IContentView view = MainFormManager.CreateDefaultView(item) as IContentView;
214                if (view == null) {
215                  MessageBox.Show("There is no view for the loaded item. It cannot be displayed. ", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
216                } else {
217                  view.Caption = Path.GetFileName(filename);
218                  files.Add(view, new FileInfo(filename));
219                  view.Show();
220                }
221              });
222            } catch (Exception ex) {
223              Auxiliary.ShowErrorMessageBox(ex);
224            } finally {
225              ResetWaitingCursor();
226            }
227          }
228        )
229      );
230    }
231
232    private static void SetWaitingCursor() {
233      Invoke(delegate() {
234        waitingCursors++;
235        ((Form)MainFormManager.MainForm).Cursor = Cursors.AppStarting;
236      });
237    }
238    private static void ResetWaitingCursor() {
239      Invoke(delegate() {
240        waitingCursors--;
241        if (waitingCursors == 0) ((Form)MainFormManager.MainForm).Cursor = Cursors.Default;
242      });
243    }
244    private static void DisableView(IView view) {
245      Invoke(delegate() {
246        ((UserControl)view).Enabled = false;
247      });
248    }
249    private static void EnableView(IView view) {
250      Invoke(delegate() {
251        ((UserControl)view).Enabled = true;
252      });
253    }
254    #endregion
255  }
256}
Note: See TracBrowser for help on using the repository browser.