📄 newfiledialog.cs
字号:
{
if (SelectedTemplate.DefaultName.IndexOf("${Number}") >= 0) {
try {
int curNumber = 1;
while (true) {
StringParser.Properties["Number"] = curNumber.ToString();
string fileName = StringParser.Parse(SelectedTemplate.DefaultName);
if (allowUntitledFiles) {
bool found = false;
foreach (string openFile in FileService.GetOpenFiles()) {
if (Path.GetFileName(openFile) == fileName) {
found = true;
break;
}
}
if (found == false)
break;
} else if (!File.Exists(Path.Combine(basePath, fileName))) {
break;
}
++curNumber;
}
} catch (Exception e) {
MessageService.ShowError(e);
}
}
return StringParser.Parse(SelectedTemplate.DefaultName);
}
bool isNameModified = false;
// list view event handlers
void SelectedIndexChange(object sender, EventArgs e)
{
if (((ListView)ControlDictionary["templateListView"]).SelectedItems.Count == 1) {
ControlDictionary["descriptionLabel"].Text = StringParser.Parse(SelectedTemplate.Description);
ControlDictionary["openButton"].Enabled = true;
if (SelectedTemplate.HasProperties) {
ShowPropertyGrid();
}
if (!this.allowUntitledFiles && !isNameModified) {
ControlDictionary["fileNameTextBox"].Text = GenerateCurrentFileName();
isNameModified = false;
}
} else {
ControlDictionary["descriptionLabel"].Text = String.Empty;
ControlDictionary["openButton"].Enabled = false;
HidePropertyGrid();
}
}
void FileNameChanged(object sender, EventArgs e)
{
isNameModified = true;
}
// 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)
{
if (Path.IsPathRooted(fileName)) {
return !File.Exists(fileName);
}
return true;
}
public void SaveFile(FileDescriptionTemplate newfile, string content)
{
string parsedFileName = StringParser.Parse(newfile.Name);
string parsedContent = StringParser.Parse(content);
if (parsedFileName.StartsWith("/") || parsedFileName.StartsWith("\\"))
parsedFileName = parsedFileName.Substring(1);
if (newfile.IsDependentFile && Path.IsPathRooted(parsedFileName)) {
Directory.CreateDirectory(Path.GetDirectoryName(parsedFileName));
File.WriteAllText(parsedFileName, parsedContent, ParserService.DefaultFileEncoding);
ParserService.ParseFile(parsedFileName, parsedContent);
} else {
IWorkbenchWindow window = FileService.NewFile(Path.GetFileName(parsedFileName), StringParser.Parse(newfile.Language), parsedContent);
if (window == null) {
return;
}
if (Path.IsPathRooted(parsedFileName)) {
Directory.CreateDirectory(Path.GetDirectoryName(parsedFileName));
window.ViewContent.Save(parsedFileName);
}
}
createdFiles.Add(new KeyValuePair<string, PropertyGroup>(parsedFileName, newfile.CreateMSBuildProperties()));
}
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.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;
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) {
string relPath = FileUtility.GetRelativePath(project.Directory, Path.GetDirectoryName(fileName));
string[] subdirs = relPath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
StringBuilder standardNameSpace = new StringBuilder(project.RootNamespace);
foreach(string subdir in subdirs) {
if (subdir == "." || subdir == ".." || subdir.Length == 0)
continue;
if (subdir.Equals("src", StringComparison.OrdinalIgnoreCase))
continue;
if (subdir.Equals("source", StringComparison.OrdinalIgnoreCase))
continue;
standardNameSpace.Append('.');
standardNameSpace.Append(GenerateValidClassName(subdir));
}
StringParser.Properties["StandardNamespace"] = standardNameSpace.ToString();
}
}
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) {
SaveFile(newfile, scriptRunner.CompileScript(item.Template, newfile));
}
DialogResult = DialogResult.OK;
}
}
}
/// <summary>
/// Represents a category
/// </summary>
public class Category : TreeNode, ICategory
{
ArrayList categories = new ArrayList();
ArrayList templates = new ArrayList();
int sortOrder = TemplateCategorySortOrderFile.UndefinedSortOrder;
public bool Selected = false;
public bool HasSelectedTemplate = false;
public Category(string name, int sortOrder) : base(StringParser.Parse(name))
{
this.Name = StringParser.Parse(name);
ImageIndex = 1;
this.sortOrder = sortOrder;
}
public Category(string name) : this(name, TemplateCategorySortOrderFile.UndefinedSortOrder)
{
}
public ArrayList Categories {
get {
return categories;
}
}
public ArrayList Templates {
get {
return templates;
}
}
public int SortOrder {
get {
return sortOrder;
}
set {
sortOrder = value;
}
}
}
/// <summary>
/// Represents a new file template
/// </summary>
class TemplateItem : ListViewItem
{
FileTemplate template;
public TemplateItem(FileTemplate template) : base(StringParser.Parse(template.Name))
{
this.template = template;
ImageIndex = 0;
}
public FileTemplate Template {
get {
return template;
}
}
}
void InitializeComponents()
{
if (allowUntitledFiles) {
SetupFromXmlStream(this.GetType().Assembly.GetManifestResourceStream("Resources.NewFileDialog.xfrm"));
} else {
SetupFromXmlStream(this.GetType().Assembly.GetManifestResourceStream("Resources.NewFileWithNameDialog.xfrm"));
ControlDictionary["fileNameTextBox"].TextChanged += new EventHandler(FileNameChanged);
}
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);
((RadioButton)ControlDictionary["largeIconsRadioButton"]).Checked = PropertyService.Get("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.Get("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"], StringParser.Parse("${res:Global.LargeIconToolTip}"));
tooltip.SetToolTip(ControlDictionary["smallIconsRadioButton"], StringParser.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 + -