private void MenuItem_ClickCreateFile(object sender, RoutedEventArgs e) { string docName; var dialog = new EnterFolderName(); dialog.Message.Text = "Enter document name"; if (dialog.ShowDialog() == true) { docName = dialog.ResponseText; var menuItem = sender as MenuItem; // Find the parant from OS TreeItem parant = FakeOS.Volume.GetTreeItem(menuItem.Tag.ToString()); string newTag = parant.Tag + '\\' + dialog.ResponseText; if (FakeOS.Volume.GetTreeItem(newTag) != null) // So item with this name already exists in the directory { MessageBox.Show(this, $"Item with name {dialog.ResponseText} already exists in the directory.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Warning); return; } // Create our new child and put it among the parants children TreeItem child = FakeOS.AddDirectory(docName, ItemType.file, parant.Id); // Find parant of item user want to delete in the Tree TreeViewItem parantTVI = (TreeViewItem)LogicalTreeHelper.FindLogicalNode(FolderView, "Id" + parant.Id); //FolderView.Items.GetItemAt(0); // Exit if the item contains data if (parantTVI.Items.Count == 1 && parantTVI.Items[0] == null) { // Remove the dummy child parantTVI.Items.Clear(); } // Create new Tree-ViewItem for our new child TreeViewItem childTVI = new TreeViewItem() { // Add the title Header = child.Name, // Add the path Tag = child.Tag, // Name for image Name = "Id" + child.Id.ToString() }; ContextMenu contextMenu = new ContextMenu(); MenuItem menuItemOpen = new MenuItem() { Header = "Open", Tag = child.Tag }; MenuItem menuItemRename = new MenuItem() { Header = "Rename", Tag = child.Tag }; menuItemRename.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickRename)); contextMenu.Items.Add(menuItemRename); menuItemOpen.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickOpenFile)); contextMenu.Items.Add(menuItemOpen); MenuItem menuItemDelete = new MenuItem() { Header = "Delete", Tag = child.Tag }; menuItemDelete.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickDelete)); contextMenu.Items.Add(menuItemDelete); childTVI.ContextMenu = contextMenu; // Add new child Tree-ViewItem to the parant parantTVI.Items.Add(childTVI); FolderView.UpdateLayout(); var notBad = new NotBad(); notBad.Title += " " + docName; if (notBad.ShowDialog() == true) { child.Document.Content = notBad.TextContent.Text; Drive.Write(parant, child, child.Document.Content, false); child.Document.LastModification = DateTime.UtcNow; } else { Drive.Write(parant, child, child.Name, false); } DrawGrid(); } }
private void MenuItem_ClickCreateFolder(object sender, RoutedEventArgs e) { var dialog = new EnterFolderName(); if (dialog.ShowDialog() == true) { var menuItem = sender as MenuItem; // Find the parant from OS TreeItem parant = FakeOS.Volume.GetTreeItem(menuItem.Tag.ToString()); string newTag = parant.Tag + '\\' + dialog.ResponseText; if (FakeOS.Volume.GetTreeItem(newTag) != null) // So item with this name already exists in the directory { MessageBox.Show(this, $"Item with name {dialog.ResponseText} already exists in the directory.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Warning); return; } // Create our new child and put it among the parants children TreeItem child = FakeOS.AddDirectory(dialog.ResponseText, ItemType.folder, parant.Id); // Find parant of item user want to delete in the Tree TreeViewItem parantTVI = (TreeViewItem)LogicalTreeHelper.FindLogicalNode(FolderView, "Id" + parant.Id); //FolderView.Items.GetItemAt(0); if (parantTVI.Items.Count == 1 && parantTVI.Items[0] == null) { // Remove the dummy child parantTVI.Items.Clear(); } // Create new Tree-ViewItem for our new child TreeViewItem childTVI = new TreeViewItem() { // Add the title Header = child.Name, // Add the path Tag = child.Tag, // Name for image Name = "Id" + child.Id.ToString() }; // Add a dummy child to the root Tree-View Item for ability to expand our new folder childTVI.Items.Add(null); ContextMenu contextMenu = new ContextMenu(); MenuItem menuItemCreate = new MenuItem() { Header = "Create new folder", Tag = child.Tag }; menuItemCreate.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickCreateFolder)); contextMenu.Items.Add(menuItemCreate); MenuItem menuItemRename = new MenuItem() { Header = "Rename", Tag = child.Tag }; menuItemRename.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickRename)); contextMenu.Items.Add(menuItemRename); MenuItem menuItemDelete = new MenuItem() { Header = "Delete", Tag = child.Tag }; menuItemDelete.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickDelete)); contextMenu.Items.Add(menuItemDelete); MenuItem menuItemCreateFile = new MenuItem() { Header = "Create file", Tag = child.Tag }; menuItemCreateFile.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickCreateFile)); contextMenu.Items.Add(menuItemCreateFile); childTVI.ContextMenu = contextMenu; // Listen out for item being expended childTVI.Expanded += TreeItem_Expanded; // Add new child Tree-ViewItem to the parant parantTVI.Items.Add(childTVI); FolderView.UpdateLayout(); DrawGrid(); } }
/// <summary> /// When a disk/folder is expanded find sub folders/files /// </summary> /// <param name="sender">TreeViewItem</param> /// <param name="e"></param> private void TreeItem_Expanded(object sender, RoutedEventArgs e) { var treeViewItem = (TreeViewItem)sender; // Exit if the item contains data if (treeViewItem.Items.Count != 1 || treeViewItem.Items[0] != null) { return; } // Remove the dummy child treeViewItem.Items.Clear(); // Get the full path var fullPath = (string)treeViewItem.Tag; // Create a blank list for directories var directories = new List <string>(); // Get nested directories foreach (var dir in FakeOS.GetDirectories(fullPath)) { var newChildTreeViewItem = new TreeViewItem { Header = dir.Name, // Let's puth the type here to conveert it into image later :) Name = "Id" + dir.Id.ToString(), Tag = dir.Tag }; ContextMenu contextMenu = new ContextMenu(); MenuItem menuItemDelete = new MenuItem() { Header = "Delete", Tag = dir.Tag }; MenuItem menuItemRename = new MenuItem() { Header = "Rename", Tag = FakeOS.Volume.Tag }; menuItemRename.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickRename)); contextMenu.Items.Add(menuItemRename); if (dir.Type == ItemType.folder || dir.Type == ItemType.drive) { newChildTreeViewItem.Items.Add(null); MenuItem menuItemCreateFolder = new MenuItem() { Header = "Create new folder", Tag = dir.Tag }; menuItemCreateFolder.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickCreateFolder)); contextMenu.Items.Add(menuItemCreateFolder); MenuItem menuItemCreateFile = new MenuItem() { Header = "Create new file", Tag = dir.Tag }; menuItemCreateFile.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickCreateFile)); contextMenu.Items.Add(menuItemCreateFile); newChildTreeViewItem.ContextMenu = contextMenu; newChildTreeViewItem.Expanded += TreeItem_Expanded; } else // If it is a file let's add an Open command { MenuItem menuItemOpenFile = new MenuItem() { Header = "Open", Tag = dir.Tag }; menuItemOpenFile.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickOpenFile)); contextMenu.Items.Add(menuItemOpenFile); } // Bind the handler of removing items menuItemDelete.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_ClickDelete)); contextMenu.Items.Add(menuItemDelete); newChildTreeViewItem.ContextMenu = contextMenu; treeViewItem.Items.Add(newChildTreeViewItem); } if (treeViewItem.Name == ItemType.folder.ToString()) { treeViewItem.Name = "folderOpen"; } }