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