Ejemplo n.º 1
0
 public override void RunActions(FileTemplateResult result)
 {
     if (openActions != null)
     {
         openActions(result);
     }
 }
Ejemplo n.º 2
0
        public override FileTemplateResult Create(FileTemplateOptions options)
        {
            FileTemplateResult result = new FileTemplateResult(options);

            StandardHeader.SetHeaders();
            StringParserPropertyContainer.FileCreation["StandardNamespace"]        = options.Namespace;
            StringParserPropertyContainer.FileCreation["FullName"]                 = options.FileName;
            StringParserPropertyContainer.FileCreation["FileName"]                 = Path.GetFileName(options.FileName);
            StringParserPropertyContainer.FileCreation["FileNameWithoutExtension"] = Path.GetFileNameWithoutExtension(options.FileName);
            StringParserPropertyContainer.FileCreation["Extension"]                = Path.GetExtension(options.FileName);
            StringParserPropertyContainer.FileCreation["Path"] = Path.GetDirectoryName(options.FileName);

            StringParserPropertyContainer.FileCreation["ClassName"] = options.ClassName;

            // when adding a file to a project (but not when creating a standalone file while a project is open):
            var project = options.Project;

            if (project != null && !options.IsUntitled)
            {
                // add required assembly references to the project
                foreach (ReferenceProjectItem reference in RequiredAssemblyReferences)
                {
                    IEnumerable <ProjectItem> refs = project.GetItemsOfType(ItemType.Reference);
                    if (!refs.Any(projItem => string.Equals(projItem.Include, reference.Include, StringComparison.OrdinalIgnoreCase)))
                    {
                        ReferenceProjectItem projItem = (ReferenceProjectItem)reference.CloneFor(project);
                        ProjectService.AddProjectItem(project, projItem);
                        //ProjectBrowserPad.RefreshViewAsync();
                    }
                }
            }

            foreach (FileDescriptionTemplate newfile in FileDescriptionTemplates)
            {
                if (!IsFilenameAvailable(StringParser.Parse(newfile.Name)))
                {
                    MessageService.ShowError(string.Format("Filename {0} is in use.\nChoose another one", StringParser.Parse(newfile.Name)));                     // TODO : translate
                    return(null);
                }
            }
            try {
                var          filesToOpen  = new List <FileName>();
                ScriptRunner scriptRunner = new ScriptRunner();
                foreach (FileDescriptionTemplate newFile in FileDescriptionTemplates)
                {
                    FileOperationResult opresult = FileUtility.ObservedSave(
                        () => {
                        OpenedFile resultFile;
                        bool shouldOpen;
                        if (!String.IsNullOrEmpty(newFile.BinaryFileName))
                        {
                            resultFile = SaveFile(newFile, null, newFile.BinaryFileName, options, out shouldOpen);
                        }
                        else
                        {
                            resultFile = SaveFile(newFile, scriptRunner.CompileScript(this, newFile), null, options, out shouldOpen);
                        }
                        if (resultFile != null)
                        {
                            result.NewOpenedFiles.Add(resultFile);
                            result.NewFiles.Add(resultFile.FileName);
                            if (shouldOpen)
                            {
                                filesToOpen.Add(resultFile.FileName);
                            }
                        }
                    }, FileName.Create(StringParser.Parse(newFile.Name))
                        );
                    if (opresult != FileOperationResult.OK)
                    {
                        return(null);
                    }
                }

                // Run creation actions
                if (createActions != null)
                {
                    createActions(result);
                }

                foreach (var filename in filesToOpen.Intersect(result.NewFiles))
                {
                    SD.FileService.OpenFile(filename);
                }
            } finally {
                // Now that the view contents
                foreach (var file in result.NewOpenedFiles)
                {
                    file.CloseIfAllViewsClosed();
                }
                result.NewOpenedFiles.RemoveAll(f => f.RegisteredViewContents.Count == 0);
            }
            // raise FileCreated event for the new files.
            foreach (var fileName in result.NewFiles)
            {
                FileService.FireFileCreated(fileName, false);
            }

            if (project != null)
            {
                project.Save();
            }

            return(result);
        }
Ejemplo n.º 3
0
		public override FileTemplateResult Create(FileTemplateOptions options)
		{
			FileTemplateResult result = new FileTemplateResult(options);
			
			StandardHeader.SetHeaders();
			StringParserPropertyContainer.FileCreation["StandardNamespace"] = options.Namespace;
			StringParserPropertyContainer.FileCreation["FullName"]                 = options.FileName;
			StringParserPropertyContainer.FileCreation["FileName"]                 = Path.GetFileName(options.FileName);
			StringParserPropertyContainer.FileCreation["FileNameWithoutExtension"] = Path.GetFileNameWithoutExtension(options.FileName);
			StringParserPropertyContainer.FileCreation["Extension"]                = Path.GetExtension(options.FileName);
			StringParserPropertyContainer.FileCreation["Path"]                     = Path.GetDirectoryName(options.FileName);
			
			StringParserPropertyContainer.FileCreation["ClassName"] = options.ClassName;
			
			// when adding a file to a project (but not when creating a standalone file while a project is open):
			var project = options.Project;
			if (project != null && !options.IsUntitled) {
				// add required assembly references to the project
				foreach (ReferenceProjectItem reference in RequiredAssemblyReferences) {
					IEnumerable<ProjectItem> refs = project.GetItemsOfType(ItemType.Reference);
					if (!refs.Any(projItem => string.Equals(projItem.Include, reference.Include, StringComparison.OrdinalIgnoreCase))) {
						ReferenceProjectItem projItem = (ReferenceProjectItem)reference.CloneFor(project);
						ProjectService.AddProjectItem(project, projItem);
						ProjectBrowserPad.RefreshViewAsync();
					}
				}
			}
			
			foreach (FileDescriptionTemplate newfile in FileDescriptionTemplates) {
				if (!IsFilenameAvailable(StringParser.Parse(newfile.Name))) {
					MessageService.ShowError(string.Format("Filename {0} is in use.\nChoose another one", StringParser.Parse(newfile.Name))); // TODO : translate
					return null;
				}
			}
			ScriptRunner scriptRunner = new ScriptRunner();
			foreach (FileDescriptionTemplate newFile in FileDescriptionTemplates) {
				FileOperationResult opresult = FileUtility.ObservedSave(
					() => {
						string resultFile;
						if (!String.IsNullOrEmpty(newFile.BinaryFileName)) {
							resultFile = SaveFile(newFile, null, newFile.BinaryFileName, options);
						} else {
							resultFile = SaveFile(newFile, scriptRunner.CompileScript(this, newFile), null, options);
						}
						if (resultFile != null) {
							result.NewFiles.Add(FileName.Create(resultFile));
						}
					}, FileName.Create(StringParser.Parse(newFile.Name))
				);
				if (opresult != FileOperationResult.OK)
					return null;
			}
			
			if (project != null) {
				project.Save();
			}
			
			// raise FileCreated event for the new files.
			foreach (var fileName in result.NewFiles) {
				FileService.FireFileCreated(fileName, false);
			}
			return result;
		}
Ejemplo n.º 4
0
		public override void RunActions(FileTemplateResult result)
		{
			if (actions != null)
				actions(result);
		}
Ejemplo n.º 5
0
 public virtual void RunActions(FileTemplateResult result)
 {
 }
Ejemplo n.º 6
0
		void OpenEvent(object sender, EventArgs e)
		{
			if (categoryTreeView.SelectedNode != null) {
				PropertyService.Set("Dialogs.NewProjectDialog.LargeImages", ((RadioButton)ControlDictionary["largeIconsRadioButton"]).Checked);
				PropertyService.Set("Dialogs.NewFileDialog.CategoryViewState", TreeViewHelper.GetViewStateString(categoryTreeView));
				PropertyService.Set("Dialogs.NewFileDialog.LastSelectedCategory", TreeViewHelper.GetPath(categoryTreeView.SelectedNode));
			}
			if (templateListView.SelectedItems.Count == 1) {
				TemplateItem item = (TemplateItem)templateListView.SelectedItems[0];
				
				PropertyService.Set("Dialogs.NewFileDialog.LastSelectedTemplate", item.Template.Name);
				
				string fileName;
				string standardNamespace = "DefaultNamespace";
				if (allowUntitledFiles) {
					fileName = GenerateCurrentFileName();
				} else {
					fileName = ControlDictionary["fileNameTextBox"].Text.Trim();
					if (!FileUtility.IsValidPath(fileName)
					    || fileName.IndexOf(Path.AltDirectorySeparatorChar) >= 0
					    || fileName.IndexOf(Path.DirectorySeparatorChar) >= 0)
					{
						MessageService.ShowError(StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.SaveFile.InvalidFileNameError}", new StringTagPair("FileName", fileName)));
						return;
					}
					if (Path.GetExtension(fileName).Length == 0) {
						fileName += Path.GetExtension(item.Template.SuggestFileName(null));
					}
					fileName = Path.Combine(basePath, fileName);
					fileName = FileUtility.NormalizePath(fileName);
					if (project != null) {
						standardNamespace = CustomToolsService.GetDefaultNamespace(project, fileName);
					}
				}
				
				options = new FileTemplateOptions();
				options.ClassName = GenerateValidClassOrNamespaceName(Path.GetFileNameWithoutExtension(fileName), false);
				options.FileName = FileName.Create(fileName);
				options.IsUntitled = allowUntitledFiles;
				options.Namespace = standardNamespace;
				options.CustomizationObject = localizedTypeDescriptor;
				options.Project = project;
				
				result = SelectedTemplate.Create(options);
				DialogResult = DialogResult.OK;
				if (result != null)
					SelectedTemplate.RunActions(result);
			}
		}
Ejemplo n.º 7
0
 public virtual void RunActions(FileTemplateResult result)
 {
 }