public Document NewDocument(string defaultName, string mimeType, Stream content) { IDisplayBinding binding = DisplayBindingService.GetDefaultBinding(null, mimeType); IViewContent newContent; if (binding != null) { try { newContent = binding.CreateContentForMimeType(mimeType, content); } finally { content.Close(); } if (newContent == null) { throw new ApplicationException(String.Format("Created view content was null{3}DefaultName:{0}{3}MimeType:{1}{3}Content:{2}", defaultName, mimeType, content, Environment.NewLine)); } newContent.UntitledName = defaultName; newContent.IsDirty = true; workbench.ShowView(newContent, true); DisplayBindingService.AttachSubWindows(newContent.WorkbenchWindow); } else { throw new ApplicationException("Can't create display binding for mime type: " + mimeType); } return(WrapDocument(newContent.WorkbenchWindow)); }
void RealOpenFile(FileOpenInformation openFileInfo) { FilePath fileName; IProgressMonitor monitor = openFileInfo.ProgressMonitor; using (monitor) { Counters.OpenDocumentTimer.Trace("Checking file"); string origName = openFileInfo.FileName; if (origName == null) { monitor.ReportError(GettextCatalog.GetString("Invalid file name"), null); return; } if (origName.StartsWith("file://")) { fileName = new Uri(origName).LocalPath; } else { fileName = origName; } if (!origName.StartsWith("http://")) { fileName = fileName.FullPath; } //Debug.Assert(FileService.IsValidPath(fileName)); if (FileService.IsDirectory(fileName)) { monitor.ReportError(GettextCatalog.GetString("{0} is a directory", fileName), null); return; } // test, if file fileName exists if (!origName.StartsWith("http://")) { // test, if an untitled file should be opened if (!System.IO.Path.IsPathRooted(origName)) { foreach (Document doc in Documents) { if (doc.Window.ViewContent.IsUntitled && doc.Window.ViewContent.UntitledName == origName) { doc.Select(); openFileInfo.NewContent = doc.Window.ViewContent; return; } } } if (!File.Exists(fileName)) { monitor.ReportError(GettextCatalog.GetString("File not found: {0}", fileName), null); return; } } foreach (Document doc in Documents) { if (doc.FileName == fileName) { if (openFileInfo.BringToFront) { doc.Select(); IEditableTextBuffer ipos = doc.GetContent <IEditableTextBuffer> (); if (openFileInfo.Line != -1 && ipos != null) { ipos.SetCaretTo(openFileInfo.Line, openFileInfo.Column != -1 ? openFileInfo.Column : 0, openFileInfo.HighlightCaretLine); } } openFileInfo.NewContent = doc.Window.ViewContent; return; } } Counters.OpenDocumentTimer.Trace("Looking for binding"); IDisplayBinding binding; if (openFileInfo.DisplayBinding != null) { binding = openFileInfo.DisplayBinding; } else { binding = DisplayBindingService.GetDefaultBinding(fileName, DesktopService.GetMimeTypeForUri(fileName)); } if (binding != null) { // When looking for the project to which the file belongs, look first // in the active project, then the active solution, and so on Project project = null; if (IdeApp.ProjectOperations.CurrentSelectedProject != null) { if (IdeApp.ProjectOperations.CurrentSelectedProject.Files.GetFile(fileName) != null) { project = IdeApp.ProjectOperations.CurrentSelectedProject; } } if (project == null && IdeApp.ProjectOperations.CurrentSelectedWorkspaceItem != null) { project = IdeApp.ProjectOperations.CurrentSelectedWorkspaceItem.GetProjectContainingFile(fileName); if (project == null) { WorkspaceItem it = IdeApp.ProjectOperations.CurrentSelectedWorkspaceItem.ParentWorkspace; while (it != null && project == null) { project = it.GetProjectContainingFile(fileName); it = it.ParentWorkspace; } } } if (project == null) { project = IdeApp.Workspace.GetProjectContainingFile(fileName); } LoadFileWrapper fw = new LoadFileWrapper(workbench, binding, project, openFileInfo); fw.Invoke(fileName); Counters.OpenDocumentTimer.Trace("Adding to recent files"); DesktopService.RecentFiles.AddFile(fileName, project); } else { try { Counters.OpenDocumentTimer.Trace("Showing in browser"); DesktopService.OpenFile(fileName); } catch (Exception ex) { LoggingService.LogError("Error opening file: " + fileName, ex); MessageService.ShowError(GettextCatalog.GetString("File '{0}' could not be opened", fileName)); } } } }