Example #1
0
        // 'Safe' in the sense that all exceptions are caught (and reported via OnOpenException).
        private IDocument SafeOpen(IDocumentClient client, Uri uri)
        {
            IDocument document = null;

            try
            {
                if (client.CanOpen(uri))
                {
                    if (OnDocumentOpening(uri))
                    {
                        document = client.Open(uri);
                        if (document != null)
                        {
                            OnDocumentOpened(document);
                            DocumentOpened.Raise(this, new DocumentEventArgs(document, DocumentEventType.Opened));

                            DocumentRegistry.ActiveDocument = document;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // DAN: Added this - if an exception occurs during line:
                // DocumentRegistry.ActiveDocument = document;
                // then we need to remove it
                if (DocumentRegistry.ActiveDocument == document)
                {
                    DocumentRegistry.Remove(document);
                }

                document = null;

                // Let's share the exception directly. We used to wrap it in another Exception
                //  object but this hides the actual exception in the error dialog.
                OnOpenException(ex);
            }

            return(document);
        }
Example #2
0
        /// <summary>
        /// Saves the document under a new name, chosen by the user</summary>
        /// <param name="document">Document to save</param>
        /// <returns>True if document was successfully saved and false if the user cancelled
        /// or there was some kind of problem</returns>
        /// <remarks>All exceptions are caught and reported via OnSaveException().</remarks>
        public virtual bool SaveAs(IDocument document)
        {
            IDocumentClient client = GetClient(document);

            string oldFilePath = document.Uri.LocalPath;
            string newFilePath = PromptUserForNewFilePath(client, oldFilePath, document);

            if (newFilePath != null)
            {
                // change file dialog service's initial directory
                FileDialogService.InitialDirectory = Path.GetDirectoryName(newFilePath);

                // restore old URI in case of failure
                Uri oldUri = document.Uri;
                Uri newUri = new Uri(newFilePath);
                document.Uri = newUri;

                bool success = SafeSave(document, DocumentEventType.SavedAs);

                if (success)
                {
                    // remove and replace the document to signal that this is a new document
                    DocumentRegistry.Remove(document);
                    DocumentRegistry.ActiveDocument = document;

                    m_untitledDocuments.Remove(document); // in case it was untitled
                    m_newDocumentPaths.Remove(oldFilePath);
                }
                else
                {
                    document.Uri = oldUri;
                }

                return(success);
            }

            return(false);
        }