📄 newfiledialog.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision: 1441 $</version>
// </file>
using System;
using System.Collections;
using System.Collections.Generic;
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;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Internal.Templates;
using ICSharpCode.SharpDevelop.Gui.XmlForms;
namespace ICSharpCode.SharpDevelop.Gui
{
/// <summary>
/// This class is for creating a new "empty" file
/// </summary>
public class NewFileDialog : BaseSharpDevelopForm
{
ArrayList alltemplates = new ArrayList();
ArrayList categories = new ArrayList();
Hashtable icons = new Hashtable();
bool allowUntitledFiles;
string basePath;
List<KeyValuePair<string, PropertyGroup>> createdFiles = new List<KeyValuePair<string, PropertyGroup>>();
public List<KeyValuePair<string, PropertyGroup>> 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) {
MessageService.ShowError(e);
}
}
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);
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 {
LoggingService.Warn("NewFileDialog: 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);
((TreeView)ControlDictionary["categoryTreeView"]).TreeViewNodeSorter = new TemplateCategoryComparer();
((TreeView)ControlDictionary["categoryTreeView"]).Sort();
SelectLastSelectedCategoryNode(((TreeView)ControlDictionary["categoryTreeView"]).Nodes, PropertyService.Get("Dialogs.NewFileDialog.LastSelectedCategory", "C#"));
}
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);
}
}
TreeNode SelectLastSelectedCategoryNode(TreeNodeCollection nodes, string name)
{
foreach (TreeNode node in nodes) {
if (node.Name == name) {
((TreeView)ControlDictionary["categoryTreeView"]).SelectedNode = node;
node.ExpandAll();
return node;
}
TreeNode selectedNode = SelectLastSelectedCategoryNode(node.Nodes, name);
if (selectedNode != null) {
return selectedNode;
}
}
return null;
}
Category GetCategory(string categoryname, string subcategoryname)
{
foreach (Category category in categories) {
if (category.Name == categoryname) {
if (subcategoryname == null) {
return category;
} else {
return GetSubcategory(category, subcategoryname);
}
}
}
Category newcategory = new Category(categoryname, TemplateCategorySortOrderFile.GetFileCategorySortOrder(categoryname));
categories.Add(newcategory);
if (subcategoryname != null) {
return GetSubcategory(newcategory, subcategoryname);
}
return newcategory;
}
Category GetSubcategory(Category parentCategory, string name)
{
foreach (Category subcategory in parentCategory.Categories) {
if (subcategory.Name == name)
return subcategory;
}
Category newsubcategory = new Category(name, TemplateCategorySortOrderFile.GetFileCategorySortOrder(parentCategory.Name, name));
parentCategory.Categories.Add(newsubcategory);
return newsubcategory;
}
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(StringParser.Parse(titem.Template.Category), StringParser.Parse(titem.Template.Subcategory));
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 = StringParser.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);
StringParser.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";
}
StringParser.Properties["Properties." + localizedProperty.Name] = defVal;
localizedProperty.DefaultValue = Boolean.Parse(defVal);
}
}
localizedProperty.LocalizedName = property.LocalizedName;
localizedTypeDescriptor.Properties.Add(localizedProperty);
}
propertyGrid.ToolbarVisible = false;
propertyGrid.SelectedObject = localizedTypeDescriptor;
propertyGrid.Size = new Size(GridWidth, Height - GridMargin * 4);
Width = Width + GridWidth;
Controls.Add(propertyGrid);
this.ResumeLayout(false);
}
}
void HidePropertyGrid()
{
if (Controls.Contains(propertyGrid)) {
this.SuspendLayout();
Controls.Remove(propertyGrid);
Width = Width - GridWidth;
this.ResumeLayout(false);
}
}
FileTemplate SelectedTemplate {
get {
if (((ListView)ControlDictionary["templateListView"]).SelectedItems.Count == 1) {
return ((TemplateItem)((ListView)ControlDictionary["templateListView"]).SelectedItems[0]).Template;
}
return null;
}
}
string GenerateCurrentFileName()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -