/// <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(Path.Combine(filePath, fileName), StandardControlGroup.Center, new DockContent(null, null), this); context.ControlInfo = controlInfo; // set document URI document = node.As <EventSequenceDocument>(); document.Uri = uri; context.Document = document; // show the document editor // This line requires references to System.Drawing and System.Windows.Forms. Would really like to remove those dependencies! m_controlHostService.RegisterControl(context.View, fileName, "Event sequence document", StandardControlGroup.Center, Path.Combine(filePath, fileName), this); } return(document); }
/// <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> /// 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(object control) { var view = control as EventSequenceView; if (view != null) { var context = view.DataContext as EventSequenceContext; if (context != null) { EventSequenceDocument document = context.Document; if (document != null) { m_documentRegistry.ActiveDocument = document; m_contextRegistry.ActiveContext = context; } } } }
/// <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> /// <param name="mainWindowClosing">True if the main window is closing</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(object control, bool mainWindowClosing) { bool closed = true; var view = control as EventSequenceView; if (view != null) { var context = view.DataContext as EventSequenceContext; if (context != null) { EventSequenceDocument document = context.Document; if (document != null) { closed = m_documentService.Close(document); if (closed) { m_contextRegistry.RemoveContext(document); } } } } return(closed); }