| 0: | // NewProjectDialog.cs | |
| 1: | // Copyright (C) 2001 Mike Krueger | |
| 2: | // with contributions from : | |
| 3: | // Shankara Narayanan (J.Shankar@commerzbank.com) | |
| 4: | // This program is free software; you can redistribute it and/or modify | |
| 5: | // it under the terms of the GNU General Public License as published by | |
| 6: | // the Free Software Foundation; either version 2 of the License, or | |
| 7: | // (at your option) any later version. | |
| 8: | // | |
| 9: | // This program is distributed in the hope that it will be useful, | |
| 10: | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11: | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12: | // GNU General Public License for more details. | |
| 13: | // | |
| 14: | // You should have received a copy of the GNU General Public License | |
| 15: | // along with this program; if not, write to the Free Software | |
| 16: | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 17: | ||
| 18: | using System; | |
| 19: | using System.Collections; | |
| 20: | using System.IO; | |
| 21: | using System.ComponentModel; | |
| 22: | using System.Drawing; | |
| 23: | using System.Reflection; | |
| 24: | using System.Resources; | |
| 25: | using System.Windows.Forms; | |
| 26: | using System.Xml; | |
| 27: | ||
| 28: | using SharpDevelop.Gui; | |
| 29: | using SharpDevelop.Internal.Modules; | |
| 30: | using SharpDevelop.Internal.Project; | |
| 31: | using SharpDevelop.Internal.Templates; | |
| 32: | using SharpDevelop.Tool.Data; | |
| 33: | using SharpDevelop.Tool.Function; | |
| 34: | using SharpDevelop.Tool.Text; | |
| 35: | ||
| 36: | namespace SharpDevelop.Gui.Dialogs { | |
| 37: | ||
| 38: | /// <summary> | |
| 39: | /// This class displays a new project dialog and sets up and creates a a new project, | |
| 40: | /// the project types are described in an XML options file | |
| 41: | /// </summary> | |
| 42: | public class NewProjectDialog : Form, SharpDevelop.Internal.Templates.INewProjectCreator | |
| 43: | { | |
| 44: | Container components = new System.ComponentModel.Container(); | |
| 45: | ||
| 46: | TextBox solutionnametextbox = new TextBox(); | |
| 47: | TextBox nametextbox = new TextBox(); | |
| 48: | ComboBox locationcombobox = new ComboBox(); | |
| 49: | ||
| 50: | Label label1 = new Label(); | |
| 51: | Label label2 = new Label(); | |
| 52: | Label label4 = new Label(); | |
| 53: | Label label5 = new Label(); | |
| 54: | Label label6 = new Label(); | |
| 55: | Label label7 = new Label(); | |
| 56: | ||
| 57: | Label descriptionlabel = new Label(); | |
| 58: | Label createdinlabel = new Label(); | |
| 59: | ||
| 60: | Button browsebutton = new Button(); | |
| 61: | Button helpbutton = new Button(); | |
| 62: | Button cancelbutton = new Button(); | |
| 63: | Button okbutton = new Button(); | |
| 64: | ||
| 65: | CheckBox checkBox1 = new CheckBox(); | |
| 66: | CheckBox checkBox2 = new CheckBox(); | |
| 67: | ||
| 68: | RadioButton smalliconbutton = new RadioButton(); | |
| 69: | RadioButton largeiconbutton = new RadioButton(); | |
| 70: | ||
| 71: | ListView templateview = new ListView(); | |
| 72: | TreeView projecttypetree = new TreeView(); | |
| 73: | ||
| 74: | ArrayList alltemplates = new ArrayList(); | |
| 75: | ArrayList categories = new ArrayList(); | |
| 76: | Hashtable icons = new Hashtable(); | |
| 77: | ||
| 78: | MainWindow mainwindow; | |
| 79: | ||
| 80: | ToolTip tooltip; | |
| 81: | ||
| 82: | public string NewProjectLocation; | |
| 83: | public string NewCombineLocation; | |
| 84: | ||
| 85: | public NewProjectDialog(MainWindow mainwindow) | |
| 86: | { | |
| 87: | ||
| 88: | InitializeComponent(); | |
| 89: | ||
| 90: | this.mainwindow = mainwindow; | |
| 91: | this.Owner = mainwindow; | |
| 92: | ||
| 93: | InitializeTemplates(); | |
| 94: | ||
| 95: | InitializeView(); | |
| 96: | projecttypetree.Select(); | |
| 97: | ||
| 98: | locationcombobox.Text = Options.GetProperty("SharpDevelop.Gui.Dialogs.NewProjectDialog.DefaultPath", System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\SharpDevelop Projects").ToString(); | |
| 99: | ||
| 100: | StartPosition = FormStartPosition.CenterParent; | |
| 101: | Icon = null; | |
| 102: | } | |
| 103: | ||
| 104: | void InitializeView() | |
| 105: | { | |
| 106: | ImageList smalllist = new ImageList(); | |
| 107: | ImageList imglist = new ImageList(); | |
| 108: | ||
| 109: | imglist.ImageSize = new Size(32, 32); | |
| 110: | smalllist.ImageSize = new Size(16, 16); | |
| 111: | ||
| 112: | smalllist.Images.Add(Resource.GetBitmap("Icons.32x32.EmptyProjectIcon")); | |
| 113: | ||
| 114: | imglist.Images.Add(Resource.GetBitmap("Icons.32x32.EmptyProjectIcon")); | |
| 115: | ||
| 116: | // load the icons and set their index from the image list in the hashtable | |
| 117: | int i = 0; | |
| 118: | Hashtable tmp = new Hashtable(icons); | |
| 119: | foreach (DictionaryEntry entry in icons) { | |
| 120: | Bitmap bitmap = FileUtility.GetBitmap(entry.Key.ToString()); | |
| 121: | if (bitmap != null) { | |
| 122: | smalllist.Images.Add(bitmap); | |
| 123: | imglist.Images.Add(bitmap); | |
| 124: | tmp[entry.Key] = ++i; | |
| 125: | } else { | |
| 126: | Console.WriteLine("can't load bitmap " + entry.Key.ToString() + " using default"); | |
| 127: | } | |
| 128: | } | |
| 129: | ||
| 130: | // set the correct imageindex for all templates | |
| 131: | icons = tmp; | |
| 132: | foreach (TemplateItem item in alltemplates) { | |
| 133: | if (item.Template.Icon == null) { | |
| 134: | item.ImageIndex = 0; | |
| 135: | } else { | |
| 136: | item.ImageIndex = (int)icons[item.Template.Icon]; | |
| 137: | } | |
| 138: | } | |
| 139: | ||
| 140: | templateview.LargeImageList = imglist; | |
| 141: | templateview.SmallImageList = smalllist; | |
| 142: | ||
| 143: | InsertCategories(null, categories); | |
| 144: | if (categories.Count > 0) | |
| 145: | projecttypetree.SelectedNode = (TreeNode)projecttypetree.Nodes[0]; | |
| 146: | } | |
| 147: | ||
| 148: | void InsertCategories(TreeNode node, ArrayList catarray) | |
| 149: | { | |
| 150: | foreach (Category cat in catarray) { | |
| 151: | if (node == null) { | |
| 152: | projecttypetree.Nodes.Add(cat); | |
| 153: | } else { | |
| 154: | node.Nodes.Add(cat); | |
| 155: | } | |
| 156: | InsertCategories(cat, cat.Categories); | |
| 157: | } | |
| 158: | } | |
| 159: | ||
| 160: | // TODO : insert sub categories | |
| 161: | Category GetCategory(string categoryname) | |
| 162: | { | |
| 163: | foreach (Category category in categories) { | |
| 164: | if (category.Text == categoryname) | |
| 165: | return category; | |
| 166: | } | |
| 167: | Category newcategory = new Category(categoryname); | |
| 168: | categories.Add(newcategory); | |
| 169: | return newcategory; | |
| 170: | } | |
| 171: | ||
| 172: | void InitializeTemplates() | |
| 173: | { | |
| 174: | foreach (ProjectTemplate template in ProjectTemplate.ProjectTemplates) { | |
| 175: | TemplateItem titem = new TemplateItem(template); | |
| 176: | if (titem.Template.Icon != null) | |
| 177: | icons[titem.Template.Icon] = 0; // "create template icon" | |
| 178: | Category cat = GetCategory(titem.Template.Category); | |
| 179: | cat.Templates.Add(titem); | |
| 180: | if (cat.Templates.Count == 1) | |
| 181: | titem.Selected = true; | |
| 182: | alltemplates.Add(titem); | |
| 183: | } | |
| 184: | } | |
| 185: | ||
| 186: | void CategoryChange(object sender, TreeViewEventArgs e) | |
| 187: | { | |
| 188: | templateview.Items.Clear(); | |
| 189: | if (projecttypetree.SelectedNode != null) { | |
| 190: | foreach (TemplateItem item in ((Category)projecttypetree.SelectedNode).Templates) { | |
| 191: | templateview.Items.Add(item); | |
| 192: | } | |
| 193: | } | |
| 194: | } | |
| 195: | ||
| 196: | void OnBeforeExpand(object sender, TreeViewCancelEventArgs e) | |
| 197: | { | |
| 198: | e.Node.ImageIndex = 1; | |
| 199: | } | |
| 200: | ||
| 201: | void OnBeforeCollapse(object sender, TreeViewCancelEventArgs e) | |
| 202: | { | |
| 203: | e.Node.ImageIndex = 0; | |
| 204: | } | |
| 205: | ||
| 206: | void CheckedChange(object sender, EventArgs e) | |
| 207: | { | |
| 208: | solutionnametextbox.ReadOnly = !checkBox1.Checked; | |
| 209: | ||
| 210: | if (solutionnametextbox.ReadOnly) { // unchecked created own directory for solution | |
| 211: | NameTextChanged(null, null); // set the value of the solutionnametextbox to nametextbox | |
| 212: | } | |
| 213: | } | |
| 214: | ||
| 215: | void NameTextChanged(object sender, EventArgs e) | |
| 216: | { | |
| 217: | if (!checkBox1.Checked) | |
| 218: | solutionnametextbox.Text = nametextbox.Text; | |
| 219: | } | |
| 220: | ||
| 221: | string ProjectSolution { | |
| 222: | get { | |
| 223: | string name = ""; | |
| 224: | if (checkBox1.Checked) { | |
| 225: | name += '\\' + solutionnametextbox.Text; | |
| 226: | } | |
| 227: | return ProjectLocation + name; | |
| 228: | } | |
| 229: | } | |
| 230: | ||
| 231: | string ProjectLocation { | |
| 232: | get { | |
| 233: | string location = locationcombobox.Text.TrimEnd( new char[] { '\\' }); | |
| 234: | string name = nametextbox.Text; | |
| 235: | return location + (checkBox2.Checked ? "\\" + name : ""); | |
| 236: | } | |
| 237: | } | |
| 238: | ||
| 239: | // TODO : Format the text | |
| 240: | void PathChanged(object sender, EventArgs e) | |
| 241: | { | |
| 242: | createdinlabel.Text = Resource.GetString("Dialog.NewProject.ProjectAtDescription")+ " " + ProjectSolution; | |
| 243: | } | |
| 244: | ||
| 245: | void IconSizeChange(object sender, EventArgs e) | |
| 246: | { | |
| 247: | templateview.View = smalliconbutton.Checked ? View.List : View.LargeIcon; | |
| 248: | } | |
| 249: | ||
| 250: | Combine cmb = new Combine(); | |
| 251: | public Combine Combine { | |
| 252: | get { | |
| 253: | return cmb; | |
| 254: | } | |
| 255: | } | |
| 256: | ||
| 257: | public bool IsFilenameAvailable(string fileName) | |
| 258: | { | |
| 259: | return true; | |
| 260: | } | |
| 261: | ||
| 262: | public void SaveFile(IProject project, string filename, string content, bool showFile) | |
| 263: | { | |
| 264: | project.Files.Add(new FileInformation(filename)); | |
| 265: | ||
| 266: | StreamWriter sr = File.CreateText(filename); | |
| 267: | sr.Write(StringParser.Parse(content, new string[,] { {"PROJECT", nametextbox.Text}, {"FILE", Path.GetFileName(filename)}})); | |
| 268: | sr.Close(); | |
| 269: | ||
| 270: | if (showFile) { | |
| 271: | string longfilename = ProjectSolution + "\\" + StringParser.Parse(filename, new string[,] { {"PROJECT", nametextbox.Text}}); | |
| 272: | mainwindow.OpenWindow(longfilename); | |
| 273: | } | |
| 274: | } | |
| 275: | ||
| 276: | void OpenEvent(object sender, EventArgs e) | |
| 277: | { | |
| 278: | Options.SetProperty("SharpDevelop.Gui.Dialogs.NewProjectDialog.DefaultPath", locationcombobox.Text); | |
| 279: | Options.SetProperty("SharpDevelop.Gui.Dialogs.NewProjectDialog.AutoCreateProjectSubdir", checkBox2.Checked); | |
| 280: | if (templateview.SelectedItems.Count == 1) { | |
| 281: | if (!locationcombobox.Text.Equals("") && !solutionnametextbox.Text.Equals("")) { | |
| 282: | ||
| 283: | TemplateItem item = (TemplateItem)templateview.SelectedItems[0]; | |
| 284: | ||
| 285: | ||
| 286: | System.IO.Directory.CreateDirectory(ProjectSolution); | |
| 287: | ||
| 288: | ||
| 289: | cmb.Name = nametextbox.Text; | |
| 290: | ||
| 291: | if (!(item.Template.LanguageName == null || item.Template.LanguageName.Length == 0)) { | |
| 292: | ILanguageModule languageinfo = ModuleManager.GetModulePerLanguageName(item.Template.LanguageName).LanguageModule; | |
| 293: | ||
| 294: | if (languageinfo == null || languageinfo.ProjectCreator == null) { | |
| 295: | MessageBox.Show("Can't create project with type :" + item.Template.LanguageName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | |
| 296: | return; | |
| 297: | } | |
| 298: | ||
| 299: | ProjectCreateInformation cinfo = new ProjectCreateInformation(); | |
| 300: | ||
| 301: | cinfo.Solution = ProjectSolution; | |
| 302: | cinfo.Location = ProjectLocation; | |
| 303: | cinfo.Description = item.Template.Description; | |
| 304: | cinfo.Name = nametextbox.Text; | |
| 305: | cinfo.ProjectTemplate = item.Template; | |
| 306: | ||
| 307: | IProject project = languageinfo.ProjectCreator.CreateProject(cinfo); | |
| 308: | ||
| 309: | foreach (FileDescriptionTemplate file in item.Template.ProjectFiles) { | |
| 310: | string filename = ProjectSolution + "\\" + StringParser.Parse(file.Name, new string[,] { {"PROJECT", nametextbox.Text}}); | |
| 311: | ||
| 312: | project.Files.Add(new FileInformation(filename)); | |
| 313: | ||
| 314: | StreamWriter sr = File.CreateText(filename); | |
| 315: | sr.Write(StringParser.Parse(file.Content, new string[,] { {"PROJECT", nametextbox.Text}, {"FILE", filename}})); | |
| 316: | sr.Close(); | |
| 317: | // TODO : CREATE TEXT FILE: | |
| 318: | } | |
| 319: | ||
| 320: | NewProjectLocation = ProjectSolution + "\\" + nametextbox.Text + ".prjx"; | |
| 321: | project.SaveProject(NewProjectLocation); | |
| 322: | ||
| 323: | cmb.AddEntry(ProjectSolution + "\\" + nametextbox.Text + ".prjx"); | |
| 324: | ||
| 325: | foreach (FileDescriptionTemplate file in item.Template.OpenFiles) { | |
| 326: | string longfilename = ProjectSolution + "\\" + StringParser.Parse(file.Name, new string[,] { {"PROJECT", nametextbox.Text}}); | |
| 327: | mainwindow.OpenWindow(longfilename); | |
| 328: | } | |
| 329: | } | |
| 330: | if (item.Template.Wizard != null) { | |
| 331: | INewProjectWizard wizard = (INewProjectWizard)item.Template.Wizard.CreateInstance(item.Template.WizardClass); | |
| 332: | if (wizard != null) { | |
| 333: | wizard.StartWizard(mainwindow, this); | |
| 334: | } | |
| 335: | } | |
| 336: | NewCombineLocation = ProjectLocation + "\\" + nametextbox.Text + ".cmbx"; | |
| 337: | ||
| 338: | cmb.SaveCombine(ProjectLocation + "\\" + nametextbox.Text + ".cmbx"); | |
| 339: | ||
| 340: | DialogResult = DialogResult.OK; | |
| 341: | } else { | |
| 342: | MessageBox.Show(Resource.GetString("Dialog.NewProject.EmptyProjectFieldWarning"), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); | |
| 343: | } | |
| 344: | } | |
| 345: | } | |
| 346: | ||
| 347: | &nbs |