示例#1
0
        public void Invoke(string fileName)
        {
            try {
                Counters.OpenDocumentTimer.Trace("Creating content");
                string mimeType = DesktopService.GetMimeTypeForUri(fileName);
                if (binding.CanHandle(fileName, mimeType, project))
                {
                    newContent = binding.CreateContent(fileName, mimeType, project);
                }
                else
                {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null);
                }
                if (newContent == null)
                {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null);
                    return;
                }

                if (project != null)
                {
                    newContent.Project = project;
                }

                Counters.OpenDocumentTimer.Trace("Loading file");

                IEncodedTextContent etc = newContent.GetContent <IEncodedTextContent> ();
                try {
                    if (fileInfo.Encoding != null && etc != null)
                    {
                        etc.Load(fileName, fileInfo.Encoding);
                    }
                    else
                    {
                        newContent.Load(fileName);
                    }
                } catch (InvalidEncodingException iex) {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not opened. {1}", fileName, iex.Message), null);
                    return;
                } catch (OverflowException oex) {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not opened. File too large.", fileName), null);
                    return;
                }
            } catch (Exception ex) {
                fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), ex);
                return;
            }
            // content got re-used
            if (newContent.WorkbenchWindow != null)
            {
                newContent.WorkbenchWindow.SelectWindow();
                fileInfo.NewContent = newContent;
                return;
            }

            Counters.OpenDocumentTimer.Trace("Showing view");

            workbench.ShowView(newContent, fileInfo.Options.HasFlag(OpenDocumentOptions.BringToFront));
            DisplayBindingService.AttachSubWindows(newContent.WorkbenchWindow);

            newContent.WorkbenchWindow.DocumentType = binding.Name;

            IEditableTextBuffer ipos = newContent.GetContent <IEditableTextBuffer> ();

            if (fileInfo.Line > 0 && ipos != null)
            {
                if (newContent.Control.IsRealized)
                {
                    JumpToLine();
                }
                else
                {
                    newContent.Control.Realized += HandleNewContentControlRealized;
                }
            }
            fileInfo.NewContent = newContent;
        }
示例#2
0
        public void SaveAs(string filename)
        {
            if (Window.ViewContent.IsViewOnly || !Window.ViewContent.IsFile)
            {
                return;
            }


            Encoding encoding = null;

            IEncodedTextContent tbuffer = GetContent <IEncodedTextContent> ();

            if (tbuffer != null)
            {
                encoding = tbuffer.SourceEncoding;
                if (encoding == null)
                {
                    encoding = Encoding.Default;
                }
            }

            if (filename == null)
            {
                var dlg = new OpenFileDialog(GettextCatalog.GetString("Save as..."), FileChooserAction.Save)
                {
                    TransientFor         = IdeApp.Workbench.RootWindow,
                    Encoding             = encoding,
                    ShowEncodingSelector = (tbuffer != null),
                };

                if (Window.ViewContent.IsUntitled)
                {
                    dlg.InitialFileName = Window.ViewContent.UntitledName;
                }
                else
                {
                    dlg.CurrentFolder   = Path.GetDirectoryName(Window.ViewContent.ContentName);
                    dlg.InitialFileName = Path.GetFileName(Window.ViewContent.ContentName);
                }

                if (!dlg.Run())
                {
                    return;
                }

                filename = dlg.SelectedFile;
                encoding = dlg.Encoding;
            }

            if (!FileService.IsValidPath(filename))
            {
                MessageService.ShowMessage(GettextCatalog.GetString("File name {0} is invalid", filename));
                return;
            }
            // detect preexisting file
            if (File.Exists(filename))
            {
                if (!MessageService.Confirm(GettextCatalog.GetString("File {0} already exists. Overwrite?", filename), AlertButton.OverwriteFile))
                {
                    return;
                }
            }

            // save backup first
            if ((bool)PropertyService.Get("SharpDevelop.CreateBackupCopy", false))
            {
                if (tbuffer != null && encoding != null)
                {
                    tbuffer.Save(filename + "~", encoding);
                }
                else
                {
                    Window.ViewContent.Save(filename + "~");
                }
            }
            TypeSystemService.RemoveSkippedfile(FileName);
            // do actual save
            if (tbuffer != null && encoding != null)
            {
                tbuffer.Save(filename, encoding);
            }
            else
            {
                Window.ViewContent.Save(filename);
            }

            FileService.NotifyFileChanged(filename);
            DesktopService.RecentFiles.AddFile(filename, (Project)null);

            OnSaved(EventArgs.Empty);
            UpdateParseDocument();
        }
示例#3
0
        public void Invoke(string fileName)
        {
            try {
                Counters.OpenDocumentTimer.Trace("Creating content");
                if (binding.CanCreateContentForUri(fileName))
                {
                    newContent = binding.CreateContentForUri(fileName);
                }
                else
                {
                    string mimeType = DesktopService.GetMimeTypeForUri(fileName);
                    if (!binding.CanCreateContentForMimeType(mimeType))
                    {
                        fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null);
                        return;
                    }
                    newContent = binding.CreateContentForMimeType(mimeType, null);
                }
                if (newContent == null)
                {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null);
                    return;
                }

                if (project != null)
                {
                    newContent.Project = project;
                }

                Counters.OpenDocumentTimer.Trace("Loading file");

                IEncodedTextContent etc = newContent.GetContent <IEncodedTextContent> ();
                if (fileInfo.Encoding != null && etc != null)
                {
                    etc.Load(fileName, fileInfo.Encoding);
                }
                else
                {
                    newContent.Load(fileName);
                }
            } catch (Exception ex) {
                fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), ex);
                return;
            }
            // content got re-used
            if (newContent.WorkbenchWindow != null)
            {
                newContent.WorkbenchWindow.SelectWindow();
                fileInfo.NewContent = newContent;
                return;
            }

            Counters.OpenDocumentTimer.Trace("Showing view");

            workbench.ShowView(newContent, fileInfo.BringToFront);
            DisplayBindingService.AttachSubWindows(newContent.WorkbenchWindow);

            newContent.WorkbenchWindow.DocumentType = binding.Name;

            IEditableTextBuffer ipos = newContent.GetContent <IEditableTextBuffer> ();

            if (fileInfo.Line != -1 && ipos != null)
            {
                GLib.Timeout.Add(10, new GLib.TimeoutHandler(JumpToLine));
            }
            fileInfo.NewContent = newContent;
        }