Esempio n. 1
0
        /// <summary>
        /// Confirms closing document</summary>
        /// <param name="document">document to confirm</param>
        /// <returns>true if document can be closed</returns>
        protected virtual bool ConfirmClose(IDocument document)
        {
            if (document == null)
            {
                return(true);
            }

            bool closeConfirmed = true;

            if (document.Dirty)
            {
                string message = "Save".Localize() + " " + document.Uri.LocalPath + "?";

                var result = FileDialogService.ConfirmFileClose(message);
                if (result == FileDialogResult.Yes)
                {
                    closeConfirmed = Save(document);
                }
                else if (result == FileDialogResult.No)
                {
                    // mark it clean, so user isn't prompted again (when the document's window
                    //  closes, for example).
                    document.Dirty = false;
                }
                else if (result == FileDialogResult.Cancel)
                {
                    closeConfirmed = false;
                }
            }
            return(closeConfirmed);
        }
Esempio n. 2
0
        /// <summary>
        /// Opens a new document for the given client</summary>
        /// <param name="client">Document client</param>
        /// <returns>Document, opened by the given client, or null if the user cancelled or
        /// there was a problem</returns>
        /// <remarks>Exceptions during opening are caught and reported via OnOpenException.</remarks>
        public virtual IDocument OpenNewDocument(IDocumentClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            IDocument result = null;
            Uri       uri    = GetNewDocumentUri(client);

            if (uri != null)
            {
                result = SafeOpen(client, uri);

                // Consider the document untitled unless its file has been created by the client or
                //  unless the user has already chosen a filename.
                if (result != null)
                {
                    if (!m_newDocumentPaths.Contains(result.Uri.LocalPath) &&
                        !FileDialogService.PathExists(result.Uri.LocalPath))
                    {
                        m_untitledDocuments.Add(result);
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a Uri under a specified directory, with a file name containing the specified string and extension,
        /// which currently doesn't exist on the file system</summary>
        /// <param name="directoryName">The base directory path under which the new Uri should reside</param>
        /// <param name="fileName">The base file name to be included in the new Uri, e.g., "Untitled".</param>
        /// <param name="extension">The file extension to be appended to the new Uri</param>
        /// <returns>A Uri under 'directoryName', with a file name containing 'fileName'</returns>
        /// <remarks>The default is to append "(#)" to 'fileName' if necessary, where '#' is a numeric suffix
        /// starting with 2 that is always incremented (independently for each file extension) for the
        /// life of this app.</remarks>
        protected virtual Uri GenerateUniqueUri(string directoryName, string fileName, string extension)
        {
            Uri uri;
            int suffix;

            m_extensionSuffixes.TryGetValue(extension, out suffix);

            // check the name to make sure there is no existing file with the same name
            while (true)
            {
                string fullFileName = fileName;
                if (suffix > 0)
                {
                    fullFileName += "(" + (suffix + 1) + ")";
                }

                suffix++;

                fullFileName += extension;

                string fullPath = Path.Combine(directoryName, fullFileName);
                if (!FileDialogService.PathExists(fullPath))
                {
                    uri = new Uri(fullPath, UriKind.RelativeOrAbsolute);
                    break;
                }
            }

            m_extensionSuffixes[extension] = suffix;
            return(uri);
        }
Esempio n. 4
0
        /// <summary>
        /// Opens one or more existing documents for the given client</summary>
        /// <param name="client">Document client</param>
        /// <param name="uri">Document URI, or null to present file dialog to user</param>
        /// <returns>Last document opened by the given client, or null</returns>
        /// <remarks>Exceptions during opening are caught and reported via OnOpenException.</remarks>
        public virtual IDocument OpenExistingDocument(IDocumentClient client, Uri uri)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            string filter = client.Info.GetFilterString();

            string[] pathNames = null;
            if (uri != null)
            {
                pathNames = new[] { uri.ToString() };
            }
            else
            {
                FileDialogService.InitialDirectory = client.Info.InitialDirectory;
                FileDialogService.OpenFileNames(ref pathNames, filter);
            }

            IDocument document = null;

            if (pathNames != null)
            {
                foreach (string pathName in pathNames)
                {
                    Uri       docUri       = new Uri(pathName, UriKind.RelativeOrAbsolute);
                    IDocument openDocument = FindOpenDocument(docUri);
                    if (openDocument != null)
                    {
                        // Simply show the document. http://tracker.ship.scea.com/jira/browse/CORETEXTEDITOR-403
                        document = openDocument;
                        client.Show(openDocument);
                    }
                    else
                    {
                        document = SafeOpen(client, docUri);
                    }
                }
            }

            return(document);
        }
Esempio n. 5
0
        private string PromptUserForNewFilePath(IDocumentClient client, string fileName, IDocument existingDocument)
        {
            string filePath = Path.GetFileName(fileName);

            FileFilterBuilder fb = new FileFilterBuilder();

            string[] extensions = client.Info.Extensions;
            foreach (string extension in extensions)
            {
                fb.AddFileType(client.Info.FileType, extension);
            }
            if (extensions.Length > 1)
            {
                fb.AddAllFilesWithExtensions();
            }
            string filter = fb.ToString();

            FileDialogService.InitialDirectory = client.Info.InitialDirectory;
            if (FileDialogService.SaveFileName(ref filePath, filter) != FileDialogResult.OK)
            {
                return(null);
            }

            if (!client.Info.IsCompatiblePath(filePath))
            {
                Outputs.WriteLine(OutputMessageType.Error, "File extension not supported".Localize());
                return(null);
            }

            // Check that the document isn't already open
            Uri       uri          = new Uri(filePath);
            IDocument openDocument = FindOpenDocument(uri);

            if (openDocument != null &&
                openDocument != existingDocument)
            {
                Outputs.WriteLine(OutputMessageType.Error, "A file with that name is already open".Localize());
                return(null);
            }

            return(filePath);
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the path name for a new document</summary>
        /// <param name="client">Document client</param>
        /// <returns>URI representing the new path, or null if the user cancelled</returns>
        protected virtual Uri GetNewDocumentUri(IDocumentClient client)
        {
            Uri    uri           = null;
            string fileName      = client.Info.NewDocumentName;
            string extension     = null;
            string directoryName = null;

            if (client.Info.Extensions.Length > 1)
            {
                if (string.IsNullOrEmpty(client.Info.DefaultExtension))
                {
                    // Since there are multiple possible extensions, ask the user to pick a filename.
                    string path = PromptUserForNewFilePath(client, fileName, null);
                    if (path != null)
                    {
                        try
                        {
                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }
                        }
                        catch (Exception e)
                        {
                            string message = string.Format(
                                "Failed to delete: {0}. Exception: {1}", path, e);
                            Outputs.WriteLine(OutputMessageType.Warning, message);
                        }

                        m_newDocumentPaths.Add(path);
                        uri = new Uri(path, UriKind.RelativeOrAbsolute);
                        return(uri);
                    }
                }
                else
                {
                    directoryName = client.Info.InitialDirectory;
                    if (directoryName == null)
                    {
                        directoryName = FileDialogService.InitialDirectory;
                    }
                    extension = client.Info.DefaultExtension;
                }
            }

            if (client.Info.Extensions.Length >= 1)
            {
                // Since there is only one possible extension, we can choose the new name (e.g., "Untitled.xml").
                directoryName = client.Info.InitialDirectory;
                if (directoryName == null)
                {
                    directoryName = FileDialogService.InitialDirectory;
                }

                extension = client.Info.Extensions[0];

                if (directoryName != null && extension != null)
                {
                    int suffix;
                    m_extensionSuffixes.TryGetValue(extension, out suffix);

                    // check the name to make sure there is no existing file with the same name
                    while (true)
                    {
                        string fullFileName = fileName;
                        if (suffix > 0)
                        {
                            fullFileName += "(" + (suffix + 1) + ")";
                        }

                        suffix++;

                        fullFileName += extension;

                        string fullPath = Path.Combine(directoryName, fullFileName);
                        if (!FileDialogService.PathExists(fullPath))
                        {
                            uri = new Uri(fullPath, UriKind.RelativeOrAbsolute);
                            break;
                        }
                    }

                    m_extensionSuffixes[extension] = suffix;
                }
            }

            return(uri);
        }