コード例 #1
0
ファイル: NewFileDialog.cs プロジェクト: hpsa/SharpDevelop
		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));
			}
			createdFiles.Clear();
			if (templateListView.SelectedItems.Count == 1) {
				if (!AllPropertiesHaveAValue) {
					MessageService.ShowMessage("${res:Dialog.NewFile.FillOutFirstMessage}", "${res:Dialog.NewFile.FillOutFirstCaption}");
					return;
				}
				TemplateItem item = (TemplateItem)templateListView.SelectedItems[0];
				
				PropertyService.Set("Dialogs.NewFileDialog.LastSelectedTemplate", item.Template.Name);
				
				string fileName;
				StringParserPropertyContainer.FileCreation["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 string[,] {{"FileName", fileName}}));
						return;
					}
					if (Path.GetExtension(fileName).Length == 0) {
						fileName += Path.GetExtension(item.Template.DefaultName);
					}
					fileName = Path.Combine(basePath, fileName);
					fileName = FileUtility.NormalizePath(fileName);
					IProject project = ProjectService.CurrentProject;
					if (project != null) {
						StringParserPropertyContainer.FileCreation["StandardNamespace"] = CustomToolsService.GetDefaultNamespace(project, fileName);
					}
				}
				StringParserPropertyContainer.FileCreation["FullName"]                 = fileName;
				StringParserPropertyContainer.FileCreation["FileName"]                 = Path.GetFileName(fileName);
				StringParserPropertyContainer.FileCreation["FileNameWithoutExtension"] = Path.GetFileNameWithoutExtension(fileName);
				StringParserPropertyContainer.FileCreation["Extension"]                = Path.GetExtension(fileName);
				StringParserPropertyContainer.FileCreation["Path"]                     = Path.GetDirectoryName(fileName);
				
				StringParserPropertyContainer.FileCreation["ClassName"] = GenerateValidClassOrNamespaceName(Path.GetFileNameWithoutExtension(fileName), false);
				
				// when adding a file to a project (but not when creating a standalone file while a project is open):
				if (ProjectService.CurrentProject != null && !this.allowUntitledFiles) {
					// add required assembly references to the project
					bool changes = false;
					foreach (ReferenceProjectItem reference in item.Template.RequiredAssemblyReferences) {
						IEnumerable<ProjectItem> refs = ProjectService.CurrentProject.GetItemsOfType(ItemType.Reference);
						if (!refs.Any(projItem => string.Equals(projItem.Include, reference.Include, StringComparison.OrdinalIgnoreCase))) {
							ReferenceProjectItem projItem = (ReferenceProjectItem)reference.CloneFor(ProjectService.CurrentProject);
							ProjectService.AddProjectItem(ProjectService.CurrentProject, projItem);
							changes = true;
						}
					}
					if (changes) {
						ProjectService.CurrentProject.Save();
					}
				}
				
				foreach (FileDescriptionTemplate newfile in item.Template.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;
					}
				}
				ScriptRunner scriptRunner = new ScriptRunner();
				foreach (FileDescriptionTemplate newFile in item.Template.FileDescriptionTemplates) {
					FileOperationResult result = FileUtility.ObservedSave(
						() => {
							if (!String.IsNullOrEmpty(newFile.BinaryFileName)) {
								SaveFile(newFile, null, newFile.BinaryFileName);
							} else {
								SaveFile(newFile, scriptRunner.CompileScript(item.Template, newFile), null);
							}
						}, StringParser.Parse(newFile.Name)
					);
					if (result != FileOperationResult.OK)
						return;
				}
				
				DialogResult = DialogResult.OK;
				
				// raise FileCreated event for the new files.
				foreach (KeyValuePair<string, FileDescriptionTemplate> entry in createdFiles) {
					FileService.FireFileCreated(entry.Key, false);
				}
			}
		}
コード例 #2
0
		void OpenEvent(object sender, EventArgs e)
		{
			if (((TreeView)ControlDictionary["categoryTreeView"]).SelectedNode != null) {
				
				PropertyService.Set("Dialogs.NewProjectDialog.LargeImages", ((RadioButton)ControlDictionary["largeIconsRadioButton"]).Checked);
				PropertyService.Set("Dialogs.NewFileDialog.LastSelectedCategory", ((TreeView)ControlDictionary["categoryTreeView"]).SelectedNode.Text);
			}
			createdFiles.Clear();
			if (((ListView)ControlDictionary["templateListView"]).SelectedItems.Count == 1) {
				if (!AllPropertiesHaveAValue) {
					MessageService.ShowMessage("${res:Dialog.NewFile.FillOutFirstMessage}", "${res:Dialog.NewFile.FillOutFirstCaption}");
					return;
				}
				TemplateItem item = (TemplateItem)((ListView)ControlDictionary["templateListView"]).SelectedItems[0];
				string fileName;
				StringParser.Properties["StandardNamespace"] = "DefaultNamespace";
				if (allowUntitledFiles) {
					fileName = GenerateCurrentFileName();
				} else {
					fileName = ControlDictionary["fileNameTextBox"].Text.Trim();
					if (!FileUtility.IsValidFileName(fileName)
					    || fileName.IndexOf(Path.AltDirectorySeparatorChar) >= 0
					    || fileName.IndexOf(Path.DirectorySeparatorChar) >= 0)
					{
						MessageService.ShowError(StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.SaveFile.InvalidFileNameError}", new string[,] {{"FileName", fileName}}));
						return;
					}
					if (Path.GetExtension(fileName).Length == 0) {
						fileName += Path.GetExtension(item.Template.DefaultName);
					}
					fileName = Path.Combine(basePath, fileName);
					fileName = Path.GetFullPath(fileName);
					IProject project = ProjectService.CurrentProject;
					if (project != null) {
						StringParser.Properties["StandardNamespace"] = CustomToolsService.GetDefaultNamespace(project, fileName);
					}
				}
				StringParser.Properties["FullName"]                 = fileName;
				StringParser.Properties["FileName"]                 = Path.GetFileName(fileName);
				StringParser.Properties["FileNameWithoutExtension"] = Path.GetFileNameWithoutExtension(fileName);
				StringParser.Properties["Extension"]                = Path.GetExtension(fileName);
				StringParser.Properties["Path"]                     = Path.GetDirectoryName(fileName);
				
				StringParser.Properties["ClassName"] = GenerateValidClassName(Path.GetFileNameWithoutExtension(fileName));
				
				
				if (item.Template.WizardPath != null) {
					Properties customizer = new Properties();
					customizer.Set("Template", item.Template);
					customizer.Set("Creator",  this);
					WizardDialog wizard = new WizardDialog("File Wizard", customizer, item.Template.WizardPath);
					if (wizard.ShowDialog(ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.MainForm) == DialogResult.OK) {
						DialogResult = DialogResult.OK;
					}
				} else {
					foreach (FileDescriptionTemplate newfile in item.Template.FileDescriptionTemplates) {
						if (!IsFilenameAvailable(StringParser.Parse(newfile.Name))) {
							MessageService.ShowError("Filename " + StringParser.Parse(newfile.Name) + " is in use.\nChoose another one");
							return;
						}
					}
					ScriptRunner scriptRunner = new ScriptRunner();
					
					foreach (FileDescriptionTemplate newfile in item.Template.FileDescriptionTemplates) {
						if (newfile.ContentData != null) {
							SaveFile(newfile, null, newfile.ContentData);
						} else {
							SaveFile(newfile, scriptRunner.CompileScript(item.Template, newfile), null);
						}
					}
					DialogResult = DialogResult.OK;
					
					// raise FileCreated event for the new files
					foreach (KeyValuePair<string, FileDescriptionTemplate> entry in createdFiles) {
						FileService.FireFileCreated(entry.Key);
					}
				}
			}
		}