/// <summary> /// Opens a project from a file. /// </summary> /// <param name="path"></param> public void Open(string path) { string appData = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\Stv233\\ItemCreator\\"; Name = path.Substring(path.LastIndexOf("\\") + 1, path.LastIndexOf(".") - path.LastIndexOf("\\") - 1); try { Directory.Delete(appData + "\\Projects\\" + Name, true); } catch { } ZipFile.ExtractToDirectory(path, appData + "\\Projects\\" + Name); ListOfItems.Clear(); string[] ProjectData = File.ReadAllLines(appData + "\\Projects\\" + Name + "\\ProjectData"); if (ProjectData[0] == "AIS") { Type = Types.AIS; } else if (ProjectData[0] == "Pro") { Type = Types.Pro; } else if (ProjectData[0] == "Simple") { Type = Types.Simple; } else { throw new System.Exception("Unknown project type"); } Array.Copy(ProjectData, 1, ProjectData, 0, ProjectData.Length - 1); Array.Resize <string>(ref ProjectData, ProjectData.Length - 1); foreach (string itemName in ProjectData) { if (Type == Types.Simple) { SimpleItem item = new SimpleItem(itemName); item.Import(appData + "\\Projects\\" + Name + "\\" + itemName); ListOfItems.Add(itemName, item); } else if (Type == Types.Pro) { ProItem item = new ProItem(itemName); item.Import(appData + "\\Projects\\" + Name + "\\" + itemName); ListOfItems.Add(itemName, item); } else if (Type == Types.AIS) { AISItem item = new AISItem(itemName); item.Import(appData + "\\Projects\\" + Name + "\\" + itemName); ListOfItems.Add(itemName, item); } } }
public ProjectControlPanel() { Project = new Project("New"); this.BackColor = System.Drawing.Color.Black; Changed = true; pnItemPanel = new Panel { AutoScroll = true, Width = this.Width / 4 * 3 - 5, Height = this.Height - 10, Left = this.Width / 4, Top = 5, BackColor = System.Drawing.Color.FromArgb(64, 64, 64), Parent = this }; btCreateItem = new Button { Text = "Create new item", BackColor = System.Drawing.Color.FromArgb(64, 64, 64), ForeColor = System.Drawing.Color.White, FlatStyle = FlatStyle.Popup, Cursor = Cursors.Hand, Width = this.Width / 4 - 10, Height = 50, Left = 5, Top = pnItemPanel.Top, Parent = this }; btCreateItem.Click += (s, e) => { if (Project.Type == Project.Types.Simple) { var item = new SimpleItem("My new item"); var creationDialog = new SimpleItemCreationDialog(item); while (true) { DialogResult result = creationDialog.ShowDialog(); if (result == DialogResult.OK) { if (!Project.ListOfItems.ContainsKey(creationDialog.Item.Name)) { Project.AddItem(creationDialog.Item); break; } else { MessageBox.Show("An item with the given name already exists.", "Item creation", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { break; } } } else if (Project.Type == Project.Types.Pro) { var item = new ProItem("My new item"); var creationDialog = new ProItemCreationDialog(item); while (true) { DialogResult result = creationDialog.ShowDialog(); if (result == DialogResult.OK) { if (!Project.ListOfItems.ContainsKey(creationDialog.Item.Name)) { Project.AddItem(creationDialog.Item); break; } else { MessageBox.Show("An item with the given name already exists.", "Item creation", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { break; } } } else if (Project.Type == Project.Types.AIS) { var item = new AISItem("My new item"); var creationDialog = new AISItemCreationDialog(item); while (true) { DialogResult result = creationDialog.ShowDialog(); if (result == DialogResult.OK) { if (!Project.ListOfItems.ContainsKey(creationDialog.Item.Name)) { Project.AddItem(creationDialog.Item); break; } else { MessageBox.Show("An item with the given name already exists.", "Item creation", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { break; } } } ReloadItems(); Changed = true; }; btImportItem = new Button { Text = "Import item", BackColor = System.Drawing.Color.FromArgb(64, 64, 64), ForeColor = System.Drawing.Color.White, FlatStyle = FlatStyle.Popup, Cursor = Cursors.Hand, Width = this.Width / 4 - 10, Height = 50, Left = 5, Top = btCreateItem.Top + btCreateItem.Height + 5, Parent = this }; btImportItem.Click += (s, e) => { try { IItem item; if (Project.Type == Project.Types.Simple) { item = new SimpleItem("My new item"); } else if (Project.Type == Project.Types.Pro) { item = new ProItem("My new item"); } else if (Project.Type == Project.Types.AIS) { item = new AISItem("My new item"); } else { MessageBox.Show("This project does not have any of the supported types.", "Import error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } using (var fbs = new FolderBrowserDialog()) { if (fbs.ShowDialog() == DialogResult.OK) { item.Import(fbs.SelectedPath); if (Project.ListOfItems.ContainsKey(item.Name)) { if (MessageBox.Show("The project already contains an item with that name. Overwrite the item?", "Import item", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Project.AddItem(item); } } else { Project.AddItem(item); } } } } catch (Exception err) { MessageBox.Show("Failed to import item.\n" + err.Message, "Import error", MessageBoxButtons.OK, MessageBoxIcon.Error); } ReloadItems(); Changed = true; }; btExportAllItems = new Button { Text = "Export all items", BackColor = System.Drawing.Color.FromArgb(64, 64, 64), ForeColor = System.Drawing.Color.White, FlatStyle = FlatStyle.Popup, Cursor = Cursors.Hand, Width = this.Width / 4 - 10, Height = 50, Left = 5, Top = btImportItem.Top + btImportItem.Height + 5, Parent = this }; btExportAllItems.Click += (s, e) => { using (var fbd = new FolderBrowserDialog()) { if (fbd.ShowDialog() == DialogResult.OK) { Project.ExportAllItems(fbd.SelectedPath); } } }; this.Resize += (s, e) => { pnItemPanel.Width = this.Width / 4 * 3 - 5; pnItemPanel.Height = this.Height - 10; pnItemPanel.Left = this.Width / 4; pnItemPanel.Top = 5; btCreateItem.Width = this.Width / 4 - 10; btCreateItem.Height = 50; btCreateItem.Left = 5; btCreateItem.Top = pnItemPanel.Top; btImportItem.Width = this.Width / 4 - 10; btImportItem.Height = 50; btImportItem.Left = 5; btImportItem.Top = btCreateItem.Top + btCreateItem.Height + 5; btExportAllItems.Width = this.Width / 4 - 10; btExportAllItems.Height = 50; btExportAllItems.Left = 5; btExportAllItems.Top = btImportItem.Top + btImportItem.Height + 5; ReloadItems(); }; ReloadItems(); }