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

📄 newfiledialog.cs

📁 全功能c#编译器
💻 CS
📖 第 1 页 / 共 2 页
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Diagnostics;
using System.Resources;
using System.Windows.Forms;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.IO;
using System.CodeDom.Compiler;

using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Services;
using ICSharpCode.SharpDevelop.Internal.Project;

using ICSharpCode.SharpDevelop.Gui.Components;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.Core.Properties;

using ICSharpCode.Core.AddIns;
using ICSharpCode.SharpDevelop.Internal.Templates;
using ICSharpCode.SharpDevelop.Gui.XmlForms;

namespace ICSharpCode.SharpDevelop.Gui.Dialogs
{
	/// <summary>
	///  This class is for creating a new "empty" file
	/// </summar
	public class NewFileDialog : BaseSharpDevelopForm, INewFileCreator
	{
		ArrayList alltemplates = new ArrayList();
		ArrayList categories   = new ArrayList();
		Hashtable icons        = new Hashtable();
		bool allowUntitledFiles;
		string basePath;
		StringCollection createdFiles = new StringCollection();
		
		public StringCollection CreatedFiles {
			get {
				return createdFiles;
			}
		}
		
		public NewFileDialog(string basePath)
		{
			StandardHeader.SetHeaders();
			this.basePath = basePath;
			this.allowUntitledFiles = basePath == null;
			try {
				InitializeComponents();
				InitializeTemplates();
				InitializeView();
				
				((TreeView)ControlDictionary["categoryTreeView"]).Select();
			} catch (Exception e) {
				Console.WriteLine(e.ToString());
			}
		}
		
		void InitializeView()
		{
			ImageList smalllist  = new ImageList();
			ImageList imglist    = new ImageList();
			smalllist.ColorDepth = ColorDepth.Depth32Bit;
			imglist.ColorDepth   = ColorDepth.Depth32Bit;
			
			imglist.ImageSize    = new Size(32, 32);
			smalllist.ImageSize  = new Size(16, 16);
			
			smalllist.Images.Add(IconService.GetBitmap("Icons.32x32.EmptyFileIcon"));
			imglist.Images.Add(IconService.GetBitmap("Icons.32x32.EmptyFileIcon"));
			
			int i = 0;
			Hashtable tmp = new Hashtable(icons);
			IconService iconService = (IconService)ServiceManager.Services.GetService(typeof(IconService));
			foreach (DictionaryEntry entry in icons) {
				Bitmap bitmap = iconService.GetBitmap(entry.Key.ToString());
				if (bitmap != null) {
					smalllist.Images.Add(bitmap);
					imglist.Images.Add(bitmap);
					tmp[entry.Key] = ++i;
				} else {
					Console.WriteLine("can't load bitmap " + entry.Key.ToString() + " using default");
				}
			}
			
			icons = tmp;
			foreach (TemplateItem item in alltemplates) {
				if (item.Template.Icon == null) {
					item.ImageIndex = 0;
				} else {
					item.ImageIndex = (int)icons[item.Template.Icon];
				}
			}
			
			((ListView)ControlDictionary["templateListView"]).LargeImageList = imglist;
			((ListView)ControlDictionary["templateListView"]).SmallImageList = smalllist;
			
			InsertCategories(null, categories);
			PropertyService propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));
			for (int j = 0; j < categories.Count; ++j) {
				if (((Category)categories[j]).Name == propertyService.GetProperty("Dialogs.NewFileDialog.LastSelectedCategory", "C#")) {
					((TreeView)ControlDictionary["categoryTreeView"]).SelectedNode = (TreeNode)((TreeView)ControlDictionary["categoryTreeView"]).Nodes[j];
					break;
				}
			}
		}
		
		void InsertCategories(TreeNode node, ArrayList catarray)
		{
			foreach (Category cat in catarray) {
				if (node == null) {
					((TreeView)ControlDictionary["categoryTreeView"]).Nodes.Add(cat);
				} else {
					node.Nodes.Add(cat);
				}
				InsertCategories(cat, cat.Categories);
			}
		}
		
		// TODO : insert sub categories
		Category GetCategory(string categoryname)
		{
			foreach (Category category in categories) {
				if (category.Name == categoryname) {
					return category;
				}
			}
			Category newcategory = new Category(categoryname);
			categories.Add(newcategory);
			return newcategory;
		}
		
		void InitializeTemplates()
		{
			foreach (FileTemplate template in FileTemplate.FileTemplates) {
				TemplateItem titem = new TemplateItem(template);
				if (titem.Template.Icon != null) {
					icons[titem.Template.Icon] = 0; // "create template icon"
				}
				if (template.NewFileDialogVisible == true) {
					Category cat = GetCategory(titem.Template.Category);
					cat.Templates.Add(titem); 
				
					if (cat.Selected == false && template.WizardPath == null) {
						cat.Selected = true;
					}
					if (!cat.HasSelectedTemplate && titem.Template.FileDescriptionTemplates.Count == 1) {
						if (((FileDescriptionTemplate)titem.Template.FileDescriptionTemplates[0]).Name.StartsWith("Empty")) {
							titem.Selected = true;
							cat.HasSelectedTemplate = true;
						}
					}
				}
				alltemplates.Add(titem);
			}
		}
		
		// tree view event handlers
		void CategoryChange(object sender, TreeViewEventArgs e)
		{
			((ListView)ControlDictionary["templateListView"]).Items.Clear();
			if (((TreeView)ControlDictionary["categoryTreeView"]).SelectedNode != null) {
				foreach (TemplateItem item in ((Category)((TreeView)ControlDictionary["categoryTreeView"]).SelectedNode).Templates) {
					((ListView)ControlDictionary["templateListView"]).Items.Add(item);
				}
			}
		}
		
		void OnBeforeExpand(object sender, TreeViewCancelEventArgs e)
		{
			e.Node.ImageIndex = 1;
		}
		
		void OnBeforeCollapse(object sender, TreeViewCancelEventArgs e)
		{
			e.Node.ImageIndex = 0;
		}
		
		const int GridWidth = 256;
		const int GridMargin = 8;
		PropertyGrid propertyGrid = new PropertyGrid();
		LocalizedTypeDescriptor localizedTypeDescriptor = null;
		
		bool AllPropertiesHaveAValue {
			get {
				foreach (TemplateProperty property in SelectedTemplate.Properties) {
					string val = StringParserService.Properties["Properties." + property.Name];
					if (val == null || val.Length == 0) {
						return false;
					}
				}
				return true;
			}
		}
		
		void ShowPropertyGrid()
		{
			if (localizedTypeDescriptor == null) {
				localizedTypeDescriptor = new LocalizedTypeDescriptor();
			}
				
			if (!Controls.Contains(propertyGrid)) {
				this.SuspendLayout();
				propertyGrid.Location = new Point(Width - GridMargin, GridMargin);
				localizedTypeDescriptor.Properties.Clear();
				foreach (TemplateProperty property in SelectedTemplate.Properties) {
					LocalizedProperty localizedProperty;
					if (property.Type.StartsWith("Types:")) {
						localizedProperty = new LocalizedProperty(property.Name, "System.Enum", property.Category, property.Description);
						TemplateType type = null;
						foreach (TemplateType templateType in SelectedTemplate.CustomTypes) {
							if (templateType.Name == property.Type.Substring("Types:".Length)) {
								type = templateType;
								break;
							}
						}
						if (type == null) {
							throw new Exception("type : " + property.Type + " not found.");
						}
						localizedProperty.TypeConverterObject = new CustomTypeConverter(type);
						StringParserService.Properties["Properties." + localizedProperty.Name] = property.DefaultValue;
						localizedProperty.DefaultValue = property.DefaultValue; // localizedProperty.TypeConverterObject.ConvertFrom();
					} else {
						localizedProperty = new LocalizedProperty(property.Name, property.Type, property.Category, property.Description);
						if (property.Type == "System.Boolean") {
							localizedProperty.TypeConverterObject = new BooleanTypeConverter();
							string defVal = property.DefaultValue == null ? null : property.DefaultValue.ToString();
							if (defVal == null || defVal.Length == 0) {
								defVal = "True";
							}
							StringParserService.Properties["Properties." + localizedProperty.Name] = defVal;
							localizedProperty.DefaultValue = Boolean.Parse(defVal);
						}
					}
					localizedProperty.LocalizedName = property.LocalizedName;
					localizedTypeDescriptor.Properties.Add(localizedProperty);

⌨️ 快捷键说明

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