Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
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, string content)
        //{
        //    return NewFile(defaultName, ParserService.DefaultFileEncoding.GetBytesWithPreamble(content));
        //}

        /// <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);
        }
Exemplo n.º 3
0
 static void ParserServiceLoadSolutionProjectsThreadEnded(object sender, EventArgs e)
 {
     foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection.ToArray())
     {
         DisplayBindingService.AttachSubWindows(content, true);
     }
 }
Exemplo n.º 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));
        }
Exemplo n.º 5
0
            public void Invoke(string fileName)
            {
                OpenedFile file = FileService.GetOrCreateOpenedFile(FileName.Create(fileName));

                try {
                    IViewContent newContent = binding.CreateContentForFile(file);
                    if (newContent != null)
                    {
                        DisplayBindingService.AttachSubWindows(newContent, false);
                        WorkbenchSingleton.Workbench.ShowView(newContent, switchToOpenedView);
                    }
                } finally {
                    file.CloseIfAllViewsClosed();
                }
            }