Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3438 was 3416, checked in by mkommend, 15 years ago

implemented ContentViews and propagation of view state changes (ticket #982)

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.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
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) {
106        Save(activeView);
107      }
108    }
109    private static void Save(IContentView view) {
110      if (!view.Locked) {
111        if ((!files.ContainsKey(view)) || (!File.Exists(files[view].Filename))) {
112          SaveAs(view);
113        } else {
114          if (files[view].Compressed)
115            SaveItemAsync(view, files[view].Filename, 9);
116          else
117            SaveItemAsync(view, files[view].Filename, 0);
118        }
119      }
120    }
121
122    public static void SaveAs() {
123      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
124      if (activeView != null) {
125        SaveAs(activeView);
126      }
127    }
128    public static void SaveAs(IContentView view) {
129      if (!view.Locked) {
130        if (saveFileDialog == null) {
131          saveFileDialog = new SaveFileDialog();
132          saveFileDialog.Title = "Save Item";
133          saveFileDialog.DefaultExt = "hl";
134          saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
135          saveFileDialog.FilterIndex = 2;
136        }
137
138        if (!files.ContainsKey(view)) {
139          files.Add(view, new FileInfo());
140          saveFileDialog.FileName = view.Caption;
141        } else {
142          saveFileDialog.FileName = files[view].Filename;
143        }
144        if (!files[view].Compressed)
145          saveFileDialog.FilterIndex = 1;
146        else
147          saveFileDialog.FilterIndex = 2;
148
149        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
150          if (saveFileDialog.FilterIndex == 1) {
151            SaveItemAsync(view, saveFileDialog.FileName, 0);
152          } else {
153            SaveItemAsync(view, saveFileDialog.FileName, 9);
154          }
155        }
156      }
157    }
158
159    public static void SaveAll() {
160      foreach (IContentView view in MainFormManager.MainForm.Views.OfType<IContentView>())
161        Save(view);
162    }
163
164    // NOTE: This event is fired by the main form. It is registered in HeuristicLabOptimizerApplication.
165    internal static void ViewClosed(object sender, ViewEventArgs e) {
166      IContentView view = e.View as IContentView;
167      if (view != null) files.Remove(view);
168    }
169
170    #region Asynchronous Save/Load Operations
171    private static void Invoke(Action a) {
172      Form form = MainFormManager.MainForm as Form;
173      if (form.InvokeRequired)
174        form.Invoke(a);
175      else
176        a.Invoke();
177    }
178
179    private static void SaveItemAsync(IContentView view, string filename, int compression) {
180      ThreadPool.QueueUserWorkItem(
181        new WaitCallback(
182          delegate(object arg) {
183            try {
184              DisableView(view);
185              SetWaitingCursor();
186              XmlGenerator.Serialize(view.Content, filename, compression);
187              Invoke(delegate() {
188                view.Caption = Path.GetFileName(filename);
189                files[view].Filename = filename;
190                files[view].Compressed = compression > 0;
191              });
192            }
193            catch (Exception ex) {
194              Auxiliary.ShowErrorMessageBox(ex);
195            } finally {
196              ResetWaitingCursor();
197              EnableView(view);
198            }
199          }
200        )
201      );
202    }
203    private static void LoadItemAsync(string filename) {
204      ThreadPool.QueueUserWorkItem(
205        new WaitCallback(
206          delegate(object arg) {
207            try {
208              SetWaitingCursor();
209              object content = XmlParser.Deserialize(filename);
210              Invoke(delegate() {
211                IContentView view = MainFormManager.CreateDefaultView(content) as IContentView;
212                if (view == null) {
213                  MessageBox.Show("There is no view for the loaded item. It cannot be displayed. ", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
214                } else {
215                  view.Caption = Path.GetFileName(filename);
216                  files.Add(view, new FileInfo(filename));
217                  view.Show();
218                }
219              });
220            } catch (Exception ex) {
221              Auxiliary.ShowErrorMessageBox(ex);
222            } finally {
223              ResetWaitingCursor();
224            }
225          }
226        )
227      );
228    }
229
230    private static void SetWaitingCursor() {
231      Invoke(delegate() {
232        waitingCursors++;
233        ((Form)MainFormManager.MainForm).Cursor = Cursors.AppStarting;
234      });
235    }
236    private static void ResetWaitingCursor() {
237      Invoke(delegate() {
238        waitingCursors--;
239        if (waitingCursors == 0) ((Form)MainFormManager.MainForm).Cursor = Cursors.Default;
240      });
241    }
242    private static void DisableView(IView view) {
243      Invoke(delegate() {
244        ((Control)view).SuspendRepaint();
245        ((Control)view).Enabled = false;
246        ((Control)view).ResumeRepaint(true);
247      });
248    }
249    private static void EnableView(IView view) {
250      Invoke(delegate() {
251        ((Control)view).SuspendRepaint();
252        ((Control)view).Enabled = true;
253        ((Control)view).ResumeRepaint(true);
254      });
255    }
256    #endregion
257  }
258}
Note: See TracBrowser for help on using the repository browser.