-
Notifications
You must be signed in to change notification settings - Fork 263
/
Editor.cs
262 lines (226 loc) · 10.5 KB
/
Editor.cs
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
262
//Copyright © 2014 Sony Computer Entertainment America LLC. See License.txt.
using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Forms;
using Sce.Atf;
using Sce.Atf.Adaptation;
using Sce.Atf.Applications;
using Sce.Atf.Dom;
namespace SimpleDomEditorSample
{
/// <summary>
/// Editor class that creates and saves event sequence documents. It also registers
/// the event sequence ListView controls with the hosting service.
/// This document client handles file operations, such as saving and closing a document, and
/// handles application data persistence.</summary>
[Export(typeof(IDocumentClient))]
[Export(typeof(Editor))]
[Export(typeof(IInitializable))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Editor : IDocumentClient, IControlHostClient, IInitializable
{
/// <summary>
/// Constructor</summary>
/// <param name="controlHostService">Control host service</param>
/// <param name="documentService">Document service</param>
/// <param name="contextRegistry">Context registry</param>
/// <param name="documentRegistry">Document registry</param>
/// <param name="schemaLoader">Schema loader</param>
[ImportingConstructor]
public Editor(
IControlHostService controlHostService,
IDocumentService documentService,
IContextRegistry contextRegistry,
IDocumentRegistry documentRegistry,
SchemaLoader schemaLoader)
{
m_controlHostService = controlHostService;
m_documentService = documentService;
m_contextRegistry = contextRegistry;
m_documentRegistry = documentRegistry;
m_schemaLoader = schemaLoader;
}
private IControlHostService m_controlHostService;
private IDocumentService m_documentService;
private IContextRegistry m_contextRegistry;
private IDocumentRegistry m_documentRegistry;
private SchemaLoader m_schemaLoader;
// scripting related members
[Import(AllowDefault = true)]
private ScriptingService m_scriptingService = null;
#region IInitializable Members
/// <summary>
/// Finishes initializing component by setting up scripting service</summary>
void IInitializable.Initialize()
{
if (m_scriptingService != null)
{
// load this assembly into script domain.
m_scriptingService.LoadAssembly(GetType().Assembly);
m_scriptingService.ImportAllTypes("SimpleDomEditorSample");
m_scriptingService.SetVariable("editor", this);
}
}
#endregion
#region IDocumentClient Members
/// <summary>
/// Gets information about the document client, such as the file type and file
/// extensions it supports, whether or not it allows multiple documents to be open, etc.</summary>
public DocumentClientInfo Info
{
get { return DocumentClientInfo; }
}
/// <summary>
/// Information about the document client</summary>
public static DocumentClientInfo DocumentClientInfo = new DocumentClientInfo(
"Event Sequence".Localize(),
new string[] { ".xml", ".esq" },
Sce.Atf.Resources.DocumentImage,
Sce.Atf.Resources.FolderImage,
true)
{
DefaultExtension = ".xml" //avoids requiring the user to choose a filename when creating a new doc
};
/// <summary>
/// Returns whether the client can open or create a document at the given URI</summary>
/// <param name="uri">Document URI</param>
/// <returns>True iff the client can open or create a document at the given URI</returns>
public bool CanOpen(Uri uri)
{
return DocumentClientInfo.IsCompatibleUri(uri);
}
/// <summary>
/// Opens or creates a document at the given URI</summary>
/// <param name="uri">Document URI</param>
/// <returns>Document, or null if the document couldn't be opened or created</returns>
public IDocument Open(Uri uri)
{
DomNode node = null;
string filePath = uri.LocalPath;
string fileName = Path.GetFileName(filePath);
if (File.Exists(filePath))
{
// read existing document using standard XML reader
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
DomXmlReader reader = new DomXmlReader(m_schemaLoader);
node = reader.Read(stream, uri);
}
}
else
{
// create new document by creating a Dom node of the root type defined by the schema
node = new DomNode(Schema.eventSequenceType.Type, Schema.eventSequenceRootElement);
}
EventSequenceDocument document = null;
if (node != null)
{
// Initialize Dom extensions now that the data is complete
node.InitializeExtensions();
EventSequenceContext context = node.As<EventSequenceContext>();
ControlInfo controlInfo = new ControlInfo(fileName, filePath, StandardControlGroup.Center);
context.ControlInfo = controlInfo;
// set document URI
document = node.As<EventSequenceDocument>();
document.Uri = uri;
// set document GUIs for search and replace
document.SearchUI = new DomNodeSearchToolStrip();
document.ReplaceUI = new DomNodeReplaceToolStrip();
document.ResultsUI = new DomNodeSearchResultsListView(m_contextRegistry);
context.ListView.Tag = document;
// show the ListView control
m_controlHostService.RegisterControl(context.ListView, controlInfo, this);
}
return document;
}
/// <summary>
/// Makes the document visible to the user</summary>
/// <param name="document">Document to show</param>
public void Show(IDocument document)
{
EventSequenceContext context = document.As<EventSequenceContext>();
m_controlHostService.Show(context.ListView);
}
/// <summary>
/// Saves the document at the given URI</summary>
/// <param name="document">Document to save</param>
/// <param name="uri">New document URI</param>
public void Save(IDocument document, Uri uri)
{
string filePath = uri.LocalPath;
FileMode fileMode = File.Exists(filePath) ? FileMode.Truncate : FileMode.OpenOrCreate;
using (FileStream stream = new FileStream(filePath, fileMode))
{
DomXmlWriter writer = new DomXmlWriter(m_schemaLoader.TypeCollection);
EventSequenceDocument eventSequenceDocument = (EventSequenceDocument)document;
writer.Write(eventSequenceDocument.DomNode, stream, uri);
}
}
/// <summary>
/// Closes the document and removes any views of it from the UI</summary>
/// <param name="document">Document to close</param>
public void Close(IDocument document)
{
EventSequenceContext context = document.As<EventSequenceContext>();
m_controlHostService.UnregisterControl(context.ListView);
context.ControlInfo = null;
// close all active EditingContexts in the document
foreach (DomNode node in context.DomNode.Subtree)
foreach (EditingContext editingContext in node.AsAll<EditingContext>())
m_contextRegistry.RemoveContext(editingContext);
// close the document
m_documentRegistry.Remove(document);
}
#endregion
#region IControlHostClient Members
/// <summary>
/// Notifies the client that its Control has been activated. Activation occurs when
/// the Control gets focus, or a parent "host" Control gets focus.</summary>
/// <param name="control">Client Control that was activated</param>
/// <remarks>This method is only called by IControlHostService if the Control was previously
/// registered for this IControlHostClient.</remarks>
void IControlHostClient.Activate(Control control)
{
EventSequenceDocument document = control.Tag as EventSequenceDocument;
if (document != null)
{
m_documentRegistry.ActiveDocument = document;
EventSequenceContext context = document.As<EventSequenceContext>();
m_contextRegistry.ActiveContext = context;
}
}
/// <summary>
/// Notifies the client that its Control has been deactivated. Deactivation occurs when
/// another Control or "host" Control gets focus.</summary>
/// <param name="control">Client Control that was deactivated</param>
/// <remarks>This method is only called by IControlHostService if the Control was previously
/// registered for this IControlHostClient.</remarks>
void IControlHostClient.Deactivate(Control control)
{
}
/// <summary>
/// Requests permission to close the client's Control. Allows user to save document before it closes.</summary>
/// <param name="control">Client Control to be closed</param>
/// <returns>True if the Control can close, or false to cancel</returns>
/// <remarks>
/// 1. This method is only called by IControlHostService if the Control was previously
/// registered for this IControlHostClient.
/// 2. If true is returned, the IControlHostService calls its own
/// UnregisterControl. The IControlHostClient has to call RegisterControl again
/// if it wants to re-register this Control.</remarks>
bool IControlHostClient.Close(Control control)
{
bool closed = true;
EventSequenceDocument document = control.Tag as EventSequenceDocument;
if (document != null)
{
closed = m_documentService.Close(document);
if (closed)
m_contextRegistry.RemoveContext(document);
}
return closed;
}
#endregion
}
}