Exemplo n.º 1
0
        /// <summary>
        /// Opens a document in the viewer.
        ///
        /// If the document is already opened it is just activated.
        /// </summary>
        /// <param name="filePath">Path of the document, which should be opened in the viewer.</param>
        /// <param name="searchQuery">Search query for which the match occurrences have been returned.</param>
        /// <param name="occurrences">Match occurrences, which should be highlighted in the document viewer.</param>
        /// <returns>Opened document view model.</returns>
        internal DocumentViewModel OpenDocument(string filePath, string searchQuery, ReadOnlyCollection <IOccurrence> occurrences)
        {
            int docIndex = 0;

            if (Engine.FileStorageProviders.Exists(filePath))
            {
                try
                {
                    System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
                    foreach (var openDocument in OpenDocuments)
                    {
                        if (openDocument.FilePath.Equals(filePath, StringComparison.OrdinalIgnoreCase))
                        {
                            break;
                        }
                        ++docIndex;
                    }

                    if (docIndex >= OpenDocuments.Count || OpenDocuments[docIndex].OccurrenceModels != occurrences)
                    {
                        var newDocument = new DocumentViewModel(filePath, occurrences, searchQuery, fileActionCloseAllButThis: mCloseAllButThisFileAction, fileActionCloseAll: mCloseAllFilesAction);
                        newDocument.FileViewerTemplate = mFileViewerTemplate;
                        if (docIndex < OpenDocuments.Count)
                        {
                            OpenDocuments.RemoveAt(docIndex);
                        }
                        OpenDocuments.Insert(docIndex, newDocument);
                        CurrentDocument = newDocument;
                    }
                    else
                    {
                        CurrentDocument = OpenDocuments[docIndex];
                    }
                    return(CurrentDocument);
                }
                finally
                {
                    System.Windows.Input.Mouse.OverrideCursor = null;
                }
            }
            else
            {
                System.Windows.MessageBox.Show(string.Format("Cannot open file \"{0}\" because it does not exist!", filePath), "Open file...", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }

            return(null);
        }