⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newfiledialog.cs

📁 c#源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
			}
		}
		
		FileTemplate SelectedTemplate {
			get {
				if (((ListView)ControlDictionary["templateListView"]).SelectedItems.Count == 1) {
					return ((TemplateItem)((ListView)ControlDictionary["templateListView"]).SelectedItems[0]).Template;
				}
				return null;
			}
		}
		string GenerateCurrentFileName()
		{
			if (SelectedTemplate.DefaultName.IndexOf("${Number}") >= 0) {
				try {
					int curNumber = 1;
					IFileService fileService = (IFileService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IFileService));
					while (true) {
						StringParserService.Properties["Number"] = curNumber.ToString();
						string fileName = StringParserService.Parse(SelectedTemplate.DefaultName);
						if (allowUntitledFiles) {
							if (!fileService.IsOpen(fileName)) {
								break;
							}
						} else if (!File.Exists(Path.Combine(basePath, fileName))) {
							break;
						}
						++curNumber;
					}
				} catch (Exception e) {
					Console.WriteLine(e);
				}
			}
			return StringParserService.Parse(SelectedTemplate.DefaultName);
		}
		
		// list view event handlers
		void SelectedIndexChange(object sender, EventArgs e)
		{
			if (((ListView)ControlDictionary["templateListView"]).SelectedItems.Count == 1) {
				ControlDictionary["descriptionLabel"].Text = StringParserService.Parse(SelectedTemplate.Description);
				ControlDictionary["openButton"].Enabled = true;
				if (SelectedTemplate.HasProperties) {
					ShowPropertyGrid();
				}
				if (!this.allowUntitledFiles) {
					ControlDictionary["fileNameTextBox"].Text = GenerateCurrentFileName();
				}
			} else {
				ControlDictionary["descriptionLabel"].Text = String.Empty;
				ControlDictionary["openButton"].Enabled = false;
				HidePropertyGrid();
			}
		}
		
		// button events
		
		void CheckedChange(object sender, EventArgs e)
		{
			((ListView)ControlDictionary["templateListView"]).View = ((RadioButton)ControlDictionary["smallIconsRadioButton"]).Checked ? View.List : View.LargeIcon;
		}
		
		public bool IsFilenameAvailable(string fileName)
		{
			return true;
		}
		
		public void SaveFile(string filename, string content, string languageName, bool showFile)
		{
			IFileService fileService = (IFileService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IFileService));
			string parsedFileName = StringParserService.Parse(filename);
			IViewContent viewContent = fileService.NewFile(parsedFileName, StringParserService.Parse(languageName), StringParserService.Parse(content));
			if (viewContent != null) {
				createdViewContents.Add(viewContent);
				DialogResult = DialogResult.OK;
			}
		}
		
		string GenerateValidClassName(string className)
		{
			int idx = 0;
			while (idx < className.Length && className[idx] != '_' && !Char.IsLetter(className[idx])) {
				++idx;
			}
			StringBuilder nameBuilder = new StringBuilder();
			for (; idx < className.Length; ++idx) {
				if (Char.IsLetterOrDigit(className[idx]) || className[idx] == '_') {
					nameBuilder.Append(className[idx]);
				}
				if (className[idx] == ' ' || className[idx] == '-' ) {
					nameBuilder.Append('_');
				}
			}
			
			return nameBuilder.ToString();
		}
		
		void OpenEvent(object sender, EventArgs e)
		{
			if (((TreeView)ControlDictionary["categoryTreeView"]).SelectedNode != null) {
				PropertyService propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));
				propertyService.SetProperty("Dialogs.NewProjectDialog.LargeImages", ((RadioButton)ControlDictionary["largeIconsRadioButton"]).Checked);
				propertyService.SetProperty("Dialogs.NewFileDialog.LastSelectedCategory", ((TreeView)ControlDictionary["categoryTreeView"]).SelectedNode.Text);
			}
			createdViewContents.Clear();
			if (((ListView)ControlDictionary["templateListView"]).SelectedItems.Count == 1) {
				if (!AllPropertiesHaveAValue) {
					IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
					messageService.ShowMessage("${res:Dialog.NewFile.FillOutFirstMessage}", "${res:Dialog.NewFile.FillOutFirstCaption}");
					return;
				}
				TemplateItem item = (TemplateItem)((ListView)ControlDictionary["templateListView"]).SelectedItems[0];
				string fileName;
				StringParserService.Properties["StandardNamespace"] = "DefaultNamespace";
				if (allowUntitledFiles) {
					fileName = GenerateCurrentFileName();
				} else {
					fileName = Path.Combine(basePath, ControlDictionary["fileNameTextBox"].Text);
					fileName = Path.GetFullPath(fileName);
					IProjectService projectService = (IProjectService)ServiceManager.Services.GetService(typeof(IProjectService));
					IProject project = projectService.CurrentSelectedProject;
					if (project != null) {
						FileUtilityService fileService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
						string relPath = fileService.AbsoluteToRelativePath(project.BaseDirectory, Path.GetDirectoryName(fileName));
						string[] subdirs = relPath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
						StringBuilder standardNameSpace = new StringBuilder(project.StandardNamespace);
						foreach(string subdir in subdirs) {
							if (subdir == "." || subdir == ".." || subdir == "")
								continue;
							standardNameSpace.Append('.');
							standardNameSpace.Append(GenerateValidClassName(subdir));
						}
						StringParserService.Properties["StandardNamespace"] = standardNameSpace.ToString();
					}
				}
				StringParserService.Properties["FullName"]                 = fileName;
				StringParserService.Properties["FileName"]                 = Path.GetFileName(fileName);
				StringParserService.Properties["FileNameWithoutExtension"] = Path.GetFileNameWithoutExtension(fileName);
				StringParserService.Properties["Extension"]                = Path.GetExtension(fileName);
				StringParserService.Properties["Path"]                     = Path.GetDirectoryName(fileName);
				
				StringParserService.Properties["ClassName"] = GenerateValidClassName(Path.GetFileNameWithoutExtension(fileName));
				
				
				if (item.Template.WizardPath != null) {
					IProperties customizer = new DefaultProperties();
					customizer.SetProperty("Template", item.Template);
					customizer.SetProperty("Creator",  this);
					WizardDialog wizard = new WizardDialog("File Wizard", customizer, item.Template.WizardPath);
					if (wizard.ShowDialog() == DialogResult.OK) {
						DialogResult = DialogResult.OK;
					}
				} else {
					ScriptRunner scriptRunner = new ScriptRunner();
					foreach (FileDescriptionTemplate newfile in item.Template.FileDescriptionTemplates) {
						SaveFile(newfile.Name, scriptRunner.CompileScript(item.Template, newfile), newfile.Language, true);
					}
					DialogResult = DialogResult.OK;
				}
			}
		}
		
		/// <summary>
		///  Represents a category
		/// </summary>
		internal class Category : TreeNode
		{
			ArrayList categories = new ArrayList();
			ArrayList templates  = new ArrayList();
			string name;
			public bool Selected = false;
			public bool HasSelectedTemplate = false;
			static StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
			
			public Category(string name) : base(stringParserService.Parse(name))
			{
				this.name = name;
				ImageIndex = 1;
			}
			
			public string Name {
				get {
					return name;
				}
			}
			public ArrayList Categories {
				get {
					return categories;
				}
			}
			public ArrayList Templates {
				get {
					return templates;
				}
			}
		}
		
		/// <summary>
		///  Represents a new file template
		/// </summary>
		class TemplateItem : ListViewItem
		{
			FileTemplate template;
			
			public TemplateItem(FileTemplate template) : base(((StringParserService)ServiceManager.Services.GetService(typeof(StringParserService))).Parse(template.Name))
			{
				this.template = template;
				ImageIndex    = 0;
			}
			
			public FileTemplate Template {
				get {
					return template;
				}
			}
		}
		
		void InitializeComponents()
		{
			if (allowUntitledFiles) {
				base.SetupFromXml(Path.Combine(PropertyService.DataDirectory, @"resources\dialogs\NewFileDialog.xfrm"));
			} else {
				base.SetupFromXml(Path.Combine(PropertyService.DataDirectory, @"resources\dialogs\NewFileWithNameDialog.xfrm"));
			}
			
			ImageList imglist = new ImageList();
			imglist.ColorDepth = ColorDepth.Depth32Bit;
			imglist.Images.Add(IconService.GetBitmap("Icons.16x16.OpenFolderBitmap"));
			imglist.Images.Add(IconService.GetBitmap("Icons.16x16.ClosedFolderBitmap"));
			((TreeView)ControlDictionary["categoryTreeView"]).ImageList = imglist;
		
			((TreeView)ControlDictionary["categoryTreeView"]).AfterSelect    += new TreeViewEventHandler(CategoryChange);
			((TreeView)ControlDictionary["categoryTreeView"]).BeforeSelect   += new TreeViewCancelEventHandler(OnBeforeExpand);
			((TreeView)ControlDictionary["categoryTreeView"]).BeforeExpand   += new TreeViewCancelEventHandler(OnBeforeExpand);
			((TreeView)ControlDictionary["categoryTreeView"]).BeforeCollapse += new TreeViewCancelEventHandler(OnBeforeCollapse);
			
			((ListView)ControlDictionary["templateListView"]).SelectedIndexChanged += new EventHandler(SelectedIndexChange);
			((ListView)ControlDictionary["templateListView"]).DoubleClick          += new EventHandler(OpenEvent);
			
			ControlDictionary["openButton"].Click += new EventHandler(OpenEvent);
			
			PropertyService propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));
			((RadioButton)ControlDictionary["largeIconsRadioButton"]).Checked = propertyService.GetProperty("Dialogs.NewProjectDialog.LargeImages", true);
			((RadioButton)ControlDictionary["largeIconsRadioButton"]).CheckedChanged += new EventHandler(CheckedChange);
			((RadioButton)ControlDictionary["largeIconsRadioButton"]).FlatStyle = FlatStyle.Standard;
			((RadioButton)ControlDictionary["largeIconsRadioButton"]).Image  = IconService.GetBitmap("Icons.16x16.LargeIconsIcon");
			
			((RadioButton)ControlDictionary["smallIconsRadioButton"]).Checked = !propertyService.GetProperty("Dialogs.NewProjectDialog.LargeImages", true);
			((RadioButton)ControlDictionary["smallIconsRadioButton"]).CheckedChanged += new EventHandler(CheckedChange);
			((RadioButton)ControlDictionary["smallIconsRadioButton"]).FlatStyle = FlatStyle.Standard;
			((RadioButton)ControlDictionary["smallIconsRadioButton"]).Image  = IconService.GetBitmap("Icons.16x16.SmallIconsIcon");
			
			ToolTip tooltip = new ToolTip();
			tooltip.SetToolTip(ControlDictionary["largeIconsRadioButton"], StringParserService.Parse("${res:Global.LargeIconToolTip}"));
			tooltip.SetToolTip(ControlDictionary["smallIconsRadioButton"], StringParserService.Parse("${res:Global.SmallIconToolTip}"));
			tooltip.Active = true;
			Owner         = (Form)WorkbenchSingleton.Workbench;
			StartPosition = FormStartPosition.CenterParent;
			Icon          = null;
			
			CheckedChange(this, EventArgs.Empty);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -