forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionManager.cs
More file actions
262 lines (249 loc) · 8.83 KB
/
Copy pathSessionManager.cs
File metadata and controls
262 lines (249 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using WeifenLuo.WinFormsUI.Docking;
using PluginCore.Localization;
using FlashDevelop.Helpers;
using PluginCore.Utilities;
using PluginCore.Managers;
using PluginCore;
namespace FlashDevelop.Managers
{
class SessionManager
{
/// <summary>
/// Saves the current session to a file
/// </summary>
public static void SaveSession(String file)
{
Session session = GetCurrentSession();
SaveSession(file, session);
}
public static void SaveSession(String file, Session session)
{
try
{
ObjectSerializer.Serialize(file, session);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
/// <summary>
/// Loads and restores the saved session
/// </summary>
public static void RestoreSession(String file, SessionType type)
{
try
{
Session session = new Session();
session = (Session)ObjectSerializer.Deserialize(file, session);
if (session.Files == null) session.Files = new List<string>();
session.Type = type; // set the type here...
RestoreSession(file, session);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
public static void RestoreSession(String file, Session session)
{
try
{
Globals.MainForm.RestoringContents = true;
Globals.MainForm.CloseAllDocuments(false);
if (!Globals.MainForm.CloseAllCanceled)
{
DataEvent te = new DataEvent(EventType.RestoreSession, file, session);
EventManager.DispatchEvent(Globals.MainForm, te);
if (!te.Handled)
{
for (Int32 i = 0; i < session.Files.Count; i++)
{
String fileToOpen = session.Files[i];
if (File.Exists(fileToOpen)) Globals.MainForm.OpenEditableDocument(fileToOpen);
}
RestoreDocks(session);
if (Globals.MainForm.Documents.Length == 0)
{
NotifyEvent ne = new NotifyEvent(EventType.FileEmpty);
EventManager.DispatchEvent(Globals.MainForm, ne);
if (!ne.Handled) Globals.MainForm.New(null, null);
}
DocumentManager.ActivateDocument(session.Index);
}
}
Globals.MainForm.RestoringContents = false;
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
/// <summary>
/// Restores the previous document docks
/// </summary>
private static void RestoreDocks(Session session)
{
try
{
DockPane prevPane;
for (Int32 i = 0; i < session.Nested.Count; i++)
{
NestedDock nestedDock = session.Nested[i];
DockContent dockContent = DocumentManager.FindDocument(nestedDock.FileName) as DockContent;
if (dockContent != null && nestedDock.NestIndex > -1)
{
if (dockContent.DockPanel.Panes.Count > nestedDock.PaneIndex)
{
prevPane = dockContent.DockPanel.Panes[nestedDock.PaneIndex];
dockContent.DockTo(prevPane, DockStyle.Fill, -1);
}
else if (dockContent.DockPanel.Panes.Count > nestedDock.NestIndex)
{
DockStyle ds = DockStyle.Right;
prevPane = dockContent.DockPanel.Panes[nestedDock.NestIndex];
if (nestedDock.Alignment == DockAlignment.Top) ds = DockStyle.Top;
else if (nestedDock.Alignment == DockAlignment.Left) ds = DockStyle.Left;
else if (nestedDock.Alignment == DockAlignment.Bottom) ds = DockStyle.Bottom;
dockContent.DockTo(prevPane, ds, -1, nestedDock.Proportion);
}
}
}
}
catch { /* No errors please... */ }
}
/// <summary>
/// Gets a session from the current documents
/// </summary>
public static Session GetCurrentSession()
{
Session session = new Session();
ITabbedDocument[] documents = Globals.MainForm.Documents;
for (Int32 i = 0; i < documents.Length; i++)
{
ITabbedDocument document = documents[i];
if (document.IsEditable && !document.IsUntitled)
{
if (document == Globals.CurrentDocument)
{
session.Index = i;
}
session.Files.Add(document.FileName);
AddDocumentDock(document, session);
}
else session.Files.Add(document.Text);
}
return session;
}
/// <summary>
/// Adds the document's dock state to the session
/// </summary>
public static void AddDocumentDock(ITabbedDocument document, Session session)
{
try
{
DockContent content = document as DockContent;
double prop = content.Pane.NestedDockingStatus.Proportion;
DockAlignment align = content.Pane.NestedDockingStatus.Alignment;
Int32 paneIndex = content.DockPanel.Panes.IndexOf(content.Pane);
Int32 nestIndex = content.DockPanel.Panes.IndexOf(content.Pane.NestedDockingStatus.PreviousPane);
if (nestIndex > -1)
{
NestedDock dock = new NestedDock(document.FileName, nestIndex, paneIndex, align, prop);
session.Nested.Add(dock);
}
}
catch { /* No errors please... */ }
}
}
[Serializable]
public class Session : ISession
{
private Int32 index = 0;
private List<String> files = new List<String>();
private List<NestedDock> nested = new List<NestedDock>();
private SessionType type = SessionType.Startup;
public Session() {}
public Session(Int32 index, List<String> files)
{
this.index = index;
this.files = files;
}
public Session(Int32 index, List<String> files, SessionType type)
{
this.index = index;
this.files = files;
this.type = type;
}
public Int32 Index
{
get { return this.index; }
set { this.index = value; }
}
public SessionType Type
{
get { return this.type; }
set { this.type = value; }
}
public List<String> Files
{
get { return this.files; }
set { this.files = value; }
}
public List<NestedDock> Nested
{
get { return this.nested; }
set { this.nested = value; }
}
}
[Serializable]
public class NestedDock
{
private Int32 nest = -1;
private Int32 index = -1;
private String file = "";
private double prop = 0.5;
private DockAlignment align = DockAlignment.Right;
public NestedDock() { }
public NestedDock(String file, Int32 nest, Int32 index, DockAlignment align, double prop)
{
this.file = file;
this.nest = nest;
this.index = index;
this.align = align;
this.prop = prop;
}
public String FileName
{
get { return this.file; }
set { this.file = value; }
}
public Int32 PaneIndex
{
get { return this.index; }
set { this.index = value; }
}
public Int32 NestIndex
{
get { return this.nest; }
set { this.nest = value; }
}
public DockAlignment Alignment
{
get { return this.align; }
set { this.align = value; }
}
public double Proportion
{
get { return this.prop; }
set { this.prop = value; }
}
}
}