void RenameOrCreateFileOrFolder() { var fileItem = TreeFolderBrowser.SelectedItem as PathItem; if (string.IsNullOrEmpty(fileItem?.EditName)) { return; } string oldFile = fileItem.FullPath; string oldPath = Path.GetDirectoryName(fileItem.FullPath); string newPath = Path.Combine(oldPath, fileItem.EditName); if (fileItem.IsFolder) { try { if (Directory.Exists(fileItem.FullPath)) { Directory.Move(fileItem.FullPath, newPath); } else { if (Directory.Exists(newPath)) { AppModel.Window.ShowStatusError($"Can't create folderOrFilePath {newPath} because it exists already."); return; } fileItem.IsEditing = false; var parent = fileItem.Parent; parent.Files.Remove(fileItem); fileItem.FullPath = newPath; FolderStructure.InsertPathItemInOrder(fileItem, parent); Dispatcher.Invoke(() => { Directory.CreateDirectory(newPath); fileItem.UpdateGitFileStatus(); }, DispatcherPriority.ApplicationIdle); } } catch { MessageBox.Show("Unable to rename or create folderOrFilePath:\r\n" + newPath, "Path Creation Error", MessageBoxButton.OK, MessageBoxImage.Warning); return; } } else { try { if (File.Exists(fileItem.FullPath)) { if (!File.Exists(newPath)) { File.Move(fileItem.FullPath, newPath); } else { File.Copy(fileItem.FullPath, newPath, true); } } else { if (File.Exists(newPath)) { AppModel.Window.ShowStatusError($"Can't create file {newPath} because it exists already."); return; } fileItem.IsEditing = false; fileItem.FullPath = newPath; // force assignment so file watcher doesn't add another File.WriteAllText(newPath, ""); fileItem.UpdateGitFileStatus(); var parent = fileItem.Parent; fileItem.Parent.Files.Remove(fileItem); FolderStructure.InsertPathItemInOrder(fileItem, parent); } // If tab was open - close it and re-open new file var tab = Window.GetTabFromFilename(oldFile); if (tab != null) { Window.CloseTab(oldFile); WindowUtilities.DoEvents(); Window.OpenFile(newPath, isPreview: true); WindowUtilities.DoEvents(); } } catch (Exception ex) { MessageBox.Show("Unable to rename or create file:\r\n" + newPath + "\r\n" + ex.Message, "File Creation Error", MessageBoxButton.OK, MessageBoxImage.Warning); } } fileItem.FullPath = newPath; fileItem.IsEditing = false; }
private void TreeViewItem_Drop(object sender, DragEventArgs e) { string rawFilename = null; // avoid double drop events? if (!IsDragging) { // Explorer Drag and Drop - just look for the filename var tkens = e.Data.GetData("FileName") as string[]; if (tkens == null) { return; } rawFilename = tkens[0] as string; } IsDragging = false; FavoriteItem targetItem; e.Handled = true; if (sender is TreeView) { // dropped into treeview open space return; //targetItem = ActivePathItem; } targetItem = (e.OriginalSource as FrameworkElement)?.DataContext as FavoriteItem; if (targetItem == null) { return; } // "path|title" var path = rawFilename ?? e.Data.GetData(DataFormats.UnicodeText) as string; if (string.IsNullOrEmpty(path)) { return; } FavoriteItem sourceItem = null; ObservableCollection <FavoriteItem> parentList = null; var tokens = path.Split('|'); if (tokens.Length == 1) { // just a filename var newItem = new FavoriteItem { File = path, Title = System.IO.Path.GetFileName(path) }; sourceItem = FavoritesModel.FindFavoriteByFilenameAndTitle(FavoritesModel.Favorites, newItem.Filename, newItem.File); if (sourceItem == null) { sourceItem = newItem; } } else { sourceItem = FavoritesModel.FindFavoriteByFilenameAndTitle(FavoritesModel.Favorites, tokens[0], tokens[1]); } if (sourceItem == null) { return; } parentList = sourceItem.Parent?.Items; if (parentList == null) { parentList = FavoritesModel.Favorites; } parentList.Remove(sourceItem); parentList = null; if (targetItem.IsFolder && !sourceItem.IsFolder) { // dropped on folder: Add below parentList = targetItem.Items; sourceItem.Parent = targetItem; } else { // Dropped on file: Add after parentList = targetItem.Parent?.Items; if (parentList == null) { parentList = FavoritesModel.Favorites; sourceItem.Parent = null; } else { sourceItem.Parent = targetItem.Parent; } } var index = parentList.IndexOf(targetItem); if (index < 0) { index = 0; } else { index++; } if (index >= parentList.Count) { parentList.Add(sourceItem); } else { parentList.Insert(index, sourceItem); } FavoritesModel.SaveFavorites(); WindowUtilities.DoEvents(); }
void RenameOrCreateFileOrFolder() { var fileItem = TreeFolderBrowser.SelectedItem as PathItem; if (string.IsNullOrEmpty(fileItem?.EditName)) { return; } string oldFilename = Path.GetFileName(fileItem.FullPath); string oldPath = Path.GetDirectoryName(fileItem.FullPath); string newPath = Path.Combine(oldPath, fileItem.EditName); if (newPath != fileItem.FullPath) { if (fileItem.IsFolder) { try { if (Directory.Exists(fileItem.FullPath)) { Directory.Move(fileItem.FullPath, newPath); } else { Directory.CreateDirectory(newPath); } } catch { MessageBox.Show("Unable to rename or create folder:\r\n" + newPath, "Path Creation Error", MessageBoxButton.OK, MessageBoxImage.Warning); } } else { try { if (File.Exists(fileItem.FullPath)) { if (!File.Exists(newPath)) { File.Move(fileItem.FullPath, newPath); } else { File.Copy(fileItem.FullPath, newPath, true); } } else { File.WriteAllText(newPath, ""); } // check if tab was open and if so rename the tab var tab = Window.GetTabFromFilename(fileItem.FullPath); if (tab != null) { Window.CloseTab(fileItem.FullPath); WindowUtilities.DoEvents(); Window.OpenTab(newPath); WindowUtilities.DoEvents(); //var doc = (MarkdownDocumentEditor) tab.Tag; //doc.MarkdownDocument.Load(newPath); //tab.Tag = doc; //Window.BindTabHeaders(); } } catch { MessageBox.Show("Unable to rename or create file:\r\n" + newPath, "File Creation Error", MessageBoxButton.OK, MessageBoxImage.Warning); } // Open the document // HandleItemSelection(); } } fileItem.FullPath = newPath; fileItem.IsEditing = false; }
private void TreeViewItem_Drop(object sender, DragEventArgs e) { // avoid double drop events? if (!IsDragging) { return; } IsDragging = false; FavoriteItem targetItem; e.Handled = true; if (sender is TreeView) { // dropped into treeview open space return; //targetItem = ActivePathItem; } targetItem = (e.OriginalSource as FrameworkElement)?.DataContext as FavoriteItem; if (targetItem == null) { return; } // "path|title" var path = e.Data.GetData(DataFormats.UnicodeText) as string; if (string.IsNullOrEmpty(path)) { return; } var tokens = path.Split('|'); var sourceItem = FavoritesModel.FindFavoriteByFilenameAndTitle(FavoritesModel.Favorites, tokens[0], tokens[1]); if (sourceItem == null) { return; } var parentList = sourceItem.Parent?.Items; if (parentList == null) { parentList = FavoritesModel.Favorites; } parentList.Remove(sourceItem); parentList = null; if (targetItem.IsFolder && !sourceItem.IsFolder) { // dropped on folder: Add below parentList = targetItem.Items; sourceItem.Parent = targetItem; } else { // Dropped on file: Add after parentList = targetItem.Parent?.Items; if (parentList == null) { parentList = FavoritesModel.Favorites; sourceItem.Parent = null; } else { sourceItem.Parent = targetItem.Parent; } } var index = parentList.IndexOf(targetItem); if (index < 0) { index = 0; } else { index++; } if (index >= parentList.Count) { parentList.Add(sourceItem); } else { parentList.Insert(index, sourceItem); } FavoritesModel.SaveFavorites(); WindowUtilities.DoEvents(); }
private void RepopulateChildren(ObservableCollection <ObservableCollection <CellContent> > data) { Debug.WriteLine("RepopulateChildren called."); bool origRecursiveUpdates = PreventRecursiveUpdates; PreventRecursiveUpdates = true; Children.Clear(); RowDefinitions.Clear(); ColumnDefinitions.Clear(); var contextMenu = ParentWindow.Resources["ColumnContextMenu"] as ContextMenu; //var rect = new Rectangle { Fill = Brushes.Gray }; //Grid.SetColumnSpan(rect, 10000); //Children.Add(rect); if (data.Count < 1) { return; } RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); var columnCounter = 0; foreach (var header in data[0]) { ColumnDefinitions.Add(new ColumnDefinition { }); var columnText = NewTextBox(); columnText.Background = new BrushConverter().ConvertFromString("#777") as Brush; columnText.Tag = new TablePosition { Column = columnCounter, Row = 0 }; columnText.ContextMenu = contextMenu; var binding = new Binding("Text") { Source = header }; columnText.SetBinding(TextBox.TextProperty, binding); Grid.SetColumn(columnText, columnCounter); Children.Add(columnText); columnCounter++; } if (data.Count > 50) { mmApp.Model.Window.ShowStatusProgress("Binding table data..."); } var rowCount = 1; foreach (var row in data.Skip(1)) { RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); columnCounter = 0; foreach (var column in row) { var columnText = NewTextBox(); columnText.Tag = new TablePosition { Column = columnCounter, Row = rowCount }; var binding = new Binding("Text") { Source = column, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Mode = BindingMode.TwoWay }; columnText.SetBinding(TextBox.TextProperty, binding); //columnText.Text = column.Text; columnText.ContextMenu = contextMenu; Grid.SetColumn(columnText, columnCounter); Grid.SetRow(columnText, rowCount); Children.Add(columnText); columnCounter++; } rowCount++; if (rowCount % 8 == 0) { PreventRecursiveUpdates = false; WindowUtilities.DoEvents(); PreventRecursiveUpdates = true; } } var lastText = Children.OfType <TextBox>().LastOrDefault(); if (lastText != null) { lastText.PreviewKeyDown += (s, e) => { if (e.Key == Key.Tab) { if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) { return; } var newRow = new ObservableCollection <CellContent>(); foreach (var header in TableSource[0]) { newRow.Add(new CellContent(string.Empty)); } var textBox = s as TextBox; var pos = textBox.Tag as TablePosition; TableSource.Add(newRow); SelectColumn(pos.Row + 1, 0); } }; } PreventRecursiveUpdates = origRecursiveUpdates; if (!PreventRecursiveUpdates) { InvalidateVisual(); } }