PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/DualityEditor/Forms/NewProjectDialog.cs

http://duality.googlecode.com/
C# | 252 lines | 213 code | 31 blank | 8 comment | 42 complexity | e75023fd009b811e893ddbc3d36ea17e MD5 | raw file
Possible License(s): BSD-2-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Xml;
  11. using Aga.Controls;
  12. using Aga.Controls.Tree;
  13. using Aga.Controls.Tree.NodeControls;
  14. using Duality;
  15. using DualityEditor.Controls.TreeModels.FileSystem;
  16. namespace DualityEditor.Forms
  17. {
  18. public partial class NewProjectDialog : Form
  19. {
  20. private FolderBrowserTreeModel folderModel = null;
  21. private string selectedTemplatePath = null;
  22. private ProjectTemplateInfo selectedTemplate = null;
  23. private ProjectTemplateInfo templateEmpty = null;
  24. private ProjectTemplateInfo templateCurrent = null;
  25. private string resultEditorBinary = null;
  26. public string ResultEditorBinary
  27. {
  28. get { return this.resultEditorBinary; }
  29. }
  30. public NewProjectDialog()
  31. {
  32. this.InitializeComponent();
  33. this.templateView_Resize(this, EventArgs.Empty); // Trigger update tile size
  34. this.folderModel = new FolderBrowserTreeModel(EditorHelper.GlobalProjectTemplateDirectory);
  35. this.folderModel.Filter = s => Directory.Exists(s); // Only show directories
  36. this.folderView.Model = this.folderModel;
  37. this.folderViewControlName.DrawText += this.folderViewControlName_DrawText;
  38. this.selectedTemplatePath = this.folderModel.BasePath;
  39. // Create hardcoded templates
  40. this.templateEmpty = new ProjectTemplateInfo();
  41. this.templateEmpty.Icon = EditorRes.GeneralRes.ImageTemplateEmpty;
  42. this.templateEmpty.Name = EditorRes.GeneralRes.Template_Empty_Name;
  43. this.templateEmpty.Description = EditorRes.GeneralRes.Template_Empty_Desc;
  44. this.templateEmpty.SpecialTag = ProjectTemplateInfo.SpecialInfo.Empty;
  45. this.templateCurrent = new ProjectTemplateInfo();
  46. this.templateCurrent.Icon = EditorRes.GeneralRes.ImageTemplateCurrent;
  47. this.templateCurrent.Name = EditorRes.GeneralRes.Template_Current_Name;
  48. this.templateCurrent.Description = EditorRes.GeneralRes.Template_Current_Desc;
  49. this.templateCurrent.SpecialTag = ProjectTemplateInfo.SpecialInfo.Current;
  50. // Hilde folder selector, if empty
  51. if (!Directory.Exists(this.folderModel.BasePath) || Directory.GetDirectories(this.folderModel.BasePath).Length == 0)
  52. {
  53. this.folderView.Enabled = false;
  54. this.splitFolderTemplate.Panel1Collapsed = true;
  55. }
  56. this.UpdateTemplateList();
  57. }
  58. protected void UpdateTemplateList()
  59. {
  60. this.templateView.BeginUpdate();
  61. this.templateView.Items.Clear();
  62. this.imageListTemplateView.Images.Clear();
  63. // Scan for template files
  64. string[] templateFiles = Directory.Exists(this.selectedTemplatePath) ? Directory.GetFiles(this.selectedTemplatePath, "*.zip", SearchOption.TopDirectoryOnly) : new string[0];
  65. List<ProjectTemplateInfo> templateEntries = new List<ProjectTemplateInfo>();
  66. foreach (string templateFile in templateFiles)
  67. {
  68. try
  69. {
  70. ProjectTemplateInfo entry = new ProjectTemplateInfo(templateFile);
  71. templateEntries.Add(entry);
  72. }
  73. catch (Exception e)
  74. {
  75. Log.Editor.WriteError("Can't load project template {0} because an error occured in the process: {1}", templateFile, Log.Exception(e));
  76. }
  77. }
  78. // Add hardcoded templates
  79. if (this.selectedTemplatePath == this.folderModel.BasePath)
  80. {
  81. templateEntries.Insert(0, this.templateCurrent);
  82. templateEntries.Insert(0, this.templateEmpty);
  83. }
  84. // Add template entries to view
  85. foreach (ProjectTemplateInfo entry in templateEntries)
  86. {
  87. Bitmap icon = entry.Icon;
  88. if (icon != null)
  89. {
  90. if (icon.Size != this.imageListTemplateView.ImageSize)
  91. icon = icon.Rescale(this.imageListTemplateView.ImageSize.Width, this.imageListTemplateView.ImageSize.Height);
  92. this.imageListTemplateView.Images.Add(entry.FilePath ?? entry.Name, icon);
  93. }
  94. ListViewItem item = new ListViewItem(new string[] { entry.Name, entry.Description }, entry.FilePath ?? entry.Name);
  95. item.Tag = entry;
  96. item.ToolTipText = entry.Description;
  97. this.templateView.Items.Add(item);
  98. }
  99. this.templateView.Sort();
  100. this.templateView.EndUpdate();
  101. }
  102. protected void UpdateInputValid()
  103. {
  104. bool validInput = true;
  105. validInput = validInput && !string.IsNullOrWhiteSpace(this.textBoxName.Text) && PathHelper.IsPathValid(this.textBoxFolder.Text);
  106. validInput = validInput && this.selectedTemplate != null;
  107. validInput = validInput && !string.IsNullOrWhiteSpace(this.textBoxName.Text) && PathHelper.IsPathValid(this.textBoxName.Text);
  108. if (validInput)
  109. {
  110. string targetDir = Path.Combine(this.textBoxFolder.Text, this.textBoxName.Text);
  111. validInput = validInput && !Directory.Exists(targetDir) && !File.Exists(targetDir);
  112. }
  113. this.buttonOk.Enabled = validInput;
  114. }
  115. private void folderViewControlName_DrawText(object sender, DrawTextEventArgs e)
  116. {
  117. e.TextColor = Color.Black;
  118. }
  119. private void templateView_Resize(object sender, EventArgs e)
  120. {
  121. this.templateView.TileSize = new Size(this.templateView.ClientSize.Width, this.templateView.TileSize.Height);
  122. }
  123. private void templateView_SelectedIndexChanged(object sender, EventArgs e)
  124. {
  125. ProjectTemplateInfo entry = this.templateView.SelectedItems.Count > 0 ? this.templateView.SelectedItems[0].Tag as ProjectTemplateInfo : null;
  126. if (entry == null) return;
  127. if (entry.FilePath == null)
  128. this.textBoxTemplate.Text = entry.Name;
  129. else
  130. this.textBoxTemplate.Text = entry.FilePath;
  131. }
  132. private void buttonCancel_Click(object sender, EventArgs e)
  133. {
  134. this.DialogResult = DialogResult.Cancel;
  135. this.Close();
  136. }
  137. private void buttonOk_Click(object sender, EventArgs e)
  138. {
  139. // Ask if the selected template should be copied to the template directory, if not located there (auto-install)
  140. if (this.selectedTemplate.SpecialTag == ProjectTemplateInfo.SpecialInfo.None &&
  141. !PathHelper.IsPathLocatedIn(this.selectedTemplate.FilePath, EditorHelper.GlobalProjectTemplateDirectory))
  142. {
  143. DialogResult result = MessageBox.Show(
  144. EditorRes.GeneralRes.Msg_InstallNewTemplate_Desc,
  145. EditorRes.GeneralRes.Msg_InstallNewTemplate_Caption,
  146. MessageBoxButtons.YesNoCancel,
  147. MessageBoxIcon.Question);
  148. if (result == System.Windows.Forms.DialogResult.Cancel) return;
  149. if (result == System.Windows.Forms.DialogResult.Yes)
  150. {
  151. if (!Directory.Exists(EditorHelper.GlobalProjectTemplateDirectory))
  152. Directory.CreateDirectory(EditorHelper.GlobalProjectTemplateDirectory);
  153. File.Copy(
  154. this.selectedTemplate.FilePath,
  155. Path.Combine(EditorHelper.GlobalProjectTemplateDirectory, Path.GetFileName(this.selectedTemplate.FilePath)));
  156. }
  157. }
  158. // Create a new project
  159. this.resultEditorBinary = EditorHelper.CreateNewProject(this.textBoxName.Text, this.textBoxFolder.Text, this.selectedTemplate);
  160. // Close successfully
  161. this.DialogResult = this.resultEditorBinary != null ? DialogResult.OK : DialogResult.Cancel;
  162. this.Close();
  163. }
  164. private void buttonBrowseTemplate_Click(object sender, EventArgs e)
  165. {
  166. OpenFileDialog fileDialog = new OpenFileDialog();
  167. fileDialog.CheckFileExists = true;
  168. fileDialog.CheckPathExists = true;
  169. fileDialog.Multiselect = false;
  170. fileDialog.Title = EditorRes.GeneralRes.OpenTemplateDialog_Title;
  171. fileDialog.RestoreDirectory = true;
  172. fileDialog.InitialDirectory = Environment.CurrentDirectory;
  173. fileDialog.AddExtension = true;
  174. fileDialog.Filter = EditorRes.GeneralRes.OpenTemplateDialog_Filters;
  175. DialogResult result = fileDialog.ShowDialog();
  176. if (result == DialogResult.OK)
  177. {
  178. this.textBoxTemplate.Text = Path.GetFullPath(fileDialog.FileName);
  179. }
  180. }
  181. private void buttonBrowseFolder_Click(object sender, EventArgs e)
  182. {
  183. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  184. folderDialog.ShowNewFolderButton = true;
  185. folderDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  186. folderDialog.Description =EditorRes.GeneralRes.SelectNewProjectFolderDialog_Desc;
  187. DialogResult result = folderDialog.ShowDialog();
  188. if (result == DialogResult.OK)
  189. {
  190. this.textBoxFolder.Text = Path.GetFullPath(folderDialog.SelectedPath);
  191. }
  192. }
  193. private void textBoxTemplate_TextChanged(object sender, EventArgs e)
  194. {
  195. if (this.textBoxTemplate.Text == this.templateEmpty.Name)
  196. this.selectedTemplate = this.templateEmpty;
  197. else if (this.textBoxTemplate.Text == this.templateCurrent.Name)
  198. this.selectedTemplate = this.templateCurrent;
  199. else
  200. {
  201. try { this.selectedTemplate = new ProjectTemplateInfo(this.textBoxTemplate.Text); }
  202. catch (Exception) { this.selectedTemplate = null; }
  203. }
  204. this.UpdateInputValid();
  205. }
  206. private void textBoxName_TextChanged(object sender, EventArgs e)
  207. {
  208. this.UpdateInputValid();
  209. }
  210. private void textBoxFolder_TextChanged(object sender, EventArgs e)
  211. {
  212. this.UpdateInputValid();
  213. }
  214. private void folderView_SelectionChanged(object sender, EventArgs e)
  215. {
  216. FolderItem folderItem = this.folderView.SelectedNode != null ? this.folderView.SelectedNode.Tag as FolderItem : null;
  217. this.selectedTemplatePath = folderItem != null ? folderItem.ItemPath : this.folderModel.BasePath;
  218. this.UpdateTemplateList();
  219. }
  220. }
  221. }