public IViewContent CreateContentForFile(OpenedFile file)
        {
            var codons = DisplayBindingService.GetCodonsPerFileName(file.FileName);
            DisplayBindingDescriptor bestMatch = null;
            double    max           = double.NegativeInfinity;
            const int BUFFER_LENGTH = 4 * 1024;

            using (var stream = file.OpenRead()) {
                string mime = "text/plain";
                if (stream.Length > 0)
                {
                    stream.Position = 0;
                    mime            = MimeTypeDetection.FindMimeType(new BinaryReader(stream).ReadBytes(BUFFER_LENGTH));
                }
                foreach (var codon in codons)
                {
                    stream.Position = 0;
                    double value = codon.Binding.AutoDetectFileContent(file.FileName, stream, mime);
                    if (value > max)
                    {
                        max       = value;
                        bestMatch = codon;
                    }
                }
            }

            if (bestMatch == null)
            {
                throw new InvalidOperationException();
            }

            return(bestMatch.Binding.CreateContentForFile(file));
        }
Example #2
0
        public static IWorkbenchWindow OpenFile(string fileName)
        {
            LoggingService.Info("Open file " + fileName);

            IWorkbenchWindow window = GetOpenFile(fileName);

            if (window != null)
            {
                window.SelectWindow();
                return(window);
            }

            IDisplayBinding binding = DisplayBindingService.GetBindingPerFileName(fileName);

            if (binding != null)
            {
                if (FileUtility.ObservedLoad(new NamedFileOperationDelegate(new LoadFileWrapper(binding).Invoke), fileName) == FileOperationResult.OK)
                {
                    FileService.RecentOpen.AddLastFile(fileName);
                }
            }
            else
            {
                throw new ApplicationException("Can't open " + fileName + ", no display codon found.");
            }
            return(GetOpenFile(fileName));
        }
Example #3
0
        /// <summary>
        /// Opens a new unsaved file.
        /// </summary>
        /// <param name="defaultName">The (unsaved) name of the to open</param>
        /// <param name="content">Content of the file to create</param>
        public static IViewContent NewFile(string defaultName, byte[] content)
        {
            if (defaultName == null)
            {
                throw new ArgumentNullException("defaultName");
            }
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            IDisplayBinding binding = DisplayBindingService.GetBindingPerFileName(defaultName);

            if (binding == null)
            {
                binding = new ErrorFallbackBinding("Can't create display binding for file " + defaultName);
            }
            OpenedFile file = CreateUntitledOpenedFile(defaultName, content);

            IViewContent newContent = binding.CreateContentForFile(file);

            if (newContent == null)
            {
                LoggingService.Warn("Created view content was null - DefaultName:" + defaultName);
                file.CloseIfAllViewsClosed();
                return(null);
            }

            DisplayBindingService.AttachSubWindows(newContent, false);

            WorkbenchSingleton.Workbench.ShowView(newContent);
            return(newContent);
        }
Example #4
0
        /// <summary>
        /// Opens a view content for the specified file
        /// or returns the existing view content for the file if it is already open.
        /// </summary>
        /// <param name="fileName">The name of the file to open.</param>
        /// <param name="switchToOpenedView">Specifies whether to switch to the view for the specified file.</param>
        /// <returns>The existing or opened <see cref="IViewContent"/> for the specified file.</returns>
        public static IViewContent OpenFile(string fileName, bool switchToOpenedView)
        {
            fileName = FileUtility.NormalizePath(fileName);
            LoggingService.Info("Open file " + fileName);

            IViewContent viewContent = GetOpenFile(fileName);

            if (viewContent != null)
            {
                if (switchToOpenedView)
                {
                    viewContent.WorkbenchWindow.SelectWindow();
                }
                return(viewContent);
            }

            IDisplayBinding binding = DisplayBindingService.GetBindingPerFileName(fileName);

            if (binding == null)
            {
                binding = new ErrorFallbackBinding("Could not find any display binding for " + Path.GetFileName(fileName));
            }
            if (FileUtility.ObservedLoad(new NamedFileOperationDelegate(new LoadFileWrapper(binding, switchToOpenedView).Invoke), fileName) == FileOperationResult.OK)
            {
                FileService.RecentOpen.AddLastFile(fileName);
            }
            return(GetOpenFile(fileName));
        }
Example #5
0
 static void ParserServiceLoadSolutionProjectsThreadEnded(object sender, EventArgs e)
 {
     foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection.ToArray())
     {
         DisplayBindingService.AttachSubWindows(content, true);
     }
 }
Example #6
0
        /// <summary>
        /// Opens a view content for the specified file
        /// or returns the existing view content for the file if it is already open.
        /// </summary>
        /// <param name="fileName">The name of the file to open.</param>
        /// <param name="switchToOpenedView">Specifies whether to switch to the view for the specified file.</param>
        /// <returns>The existing or opened <see cref="IViewContent"/> for the specified file.</returns>
        public static IViewContent OpenFile(string fileName, bool switchToOpenedView)
        {
            fileName = FileUtility.NormalizePath(fileName);
            LoggingService.Info("Open file " + fileName);

            IViewContent viewContent = GetOpenFile(fileName);

            if (viewContent != null)
            {
                if (switchToOpenedView)
                {
                    viewContent.WorkbenchWindow.SelectWindow();
                }
                return(viewContent);
            }

            IDisplayBinding binding = DisplayBindingService.GetBindingPerFileName(fileName);

            if (binding != null)
            {
                if (FileUtility.ObservedLoad(new NamedFileOperationDelegate(new LoadFileWrapper(binding, switchToOpenedView).Invoke), fileName) == FileOperationResult.OK)
                {
                    FileService.RecentOpen.AddLastFile(fileName);
                }
            }
            else
            {
                throw new ApplicationException("Cannot open " + fileName + ", no display codon found.");
            }
            return(GetOpenFile(fileName));
        }
Example #7
0
            public void Invoke(string fileName)
            {
                IViewContent newContent = binding.CreateContentForFile(fileName);

                if (newContent != null)
                {
                    DisplayBindingService.AttachSubWindows(newContent, false);
                    WorkbenchSingleton.Workbench.ShowView(newContent);
                }
            }
Example #8
0
            public void Invoke(string fileName)
            {
                OpenedFile   file       = FileService.GetOrCreateOpenedFile(FileName.Create(fileName));
                IViewContent newContent = binding.CreateContentForFile(file);

                if (newContent != null)
                {
                    DisplayBindingService.AttachSubWindows(newContent, false);
                    WorkbenchSingleton.Workbench.ShowView(newContent, switchToOpenedView);
                }
                file.CloseIfAllViewsClosed();
            }
Example #9
0
        /// <summary>
        /// Opens a new unsaved file.
        /// </summary>
        /// <param name="defaultName">The (unsaved) name of the to open</param>
        /// <param name="language">Name of the language used to choose the display binding.</param>
        /// <param name="content">Content of the file to create</param>
        public static IWorkbenchWindow NewFile(string defaultName, string language, string content)
        {
            IDisplayBinding binding = DisplayBindingService.GetBindingPerLanguageName(language);

            if (binding != null)
            {
                IViewContent newContent = binding.CreateContentForLanguage(language, content);
                if (newContent == null)
                {
                    LoggingService.Warn(String.Format("Created view content was null{3}DefaultName:{0}{3}Language:{1}{3}Content:{2}", defaultName, language, content, Environment.NewLine));
                    return(null);
                }
                newContent.UntitledName = newContent.GetHashCode() + "/" + defaultName;
                DisplayBindingService.AttachSubWindows(newContent, false);

                WorkbenchSingleton.Workbench.ShowView(newContent);
                return(newContent.WorkbenchWindow);
            }
            else
            {
                throw new ApplicationException("Can't create display binding for language " + language);
            }
        }