diff --git a/InputDialog.cs b/InputDialog.cs
new file mode 100644
index 0000000..7be725f
--- /dev/null
+++ b/InputDialog.cs
@@ -0,0 +1,100 @@
+using System.Windows;
+using System.Windows.Controls;
+
+namespace Microsoft.Windows.Controls
+{
+ public class InputDialogResult
+ {
+ public bool IsOk { get; set; }
+ public string Result { get; set; }
+ }
+
+ public class InputDialog
+ {
+ public static InputDialogResult Show(string title, string promptText, string defaultValue = "")
+ {
+ var dialog = new Window
+ {
+ Title = title,
+ Width = 400,
+ Height = 150,
+ WindowStartupLocation = WindowStartupLocation.CenterOwner,
+ Owner = Application.Current.MainWindow,
+ ResizeMode = ResizeMode.NoResize
+ };
+
+ var grid = new Grid { Margin = new Thickness(10) };
+ grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
+ grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(10) });
+ grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
+
+ var promptLabel = new TextBlock
+ {
+ Text = promptText,
+ Margin = new Thickness(0, 0, 0, 5)
+ };
+ Grid.SetRow(promptLabel, 0);
+
+ var inputBox = new TextBox
+ {
+ Text = defaultValue,
+ Margin = new Thickness(0)
+ };
+ Grid.SetRow(inputBox, 2);
+
+ var buttonPanel = new StackPanel
+ {
+ Orientation = Orientation.Horizontal,
+ HorizontalAlignment = HorizontalAlignment.Right,
+ Margin = new Thickness(0, 10, 0, 0)
+ };
+ Grid.SetRow(buttonPanel, 4);
+
+ var okButton = new Button
+ {
+ Content = "Aceptar",
+ Width = 75,
+ Height = 23,
+ Margin = new Thickness(0, 0, 10, 0),
+ IsDefault = true
+ };
+
+ var cancelButton = new Button
+ {
+ Content = "Cancelar",
+ Width = 75,
+ Height = 23,
+ IsCancel = true
+ };
+
+ buttonPanel.Children.Add(okButton);
+ buttonPanel.Children.Add(cancelButton);
+
+ grid.Children.Add(promptLabel);
+ grid.Children.Add(inputBox);
+ grid.Children.Add(buttonPanel);
+
+ dialog.Content = grid;
+
+ var result = new InputDialogResult();
+
+ okButton.Click += (s, e) =>
+ {
+ result.Result = inputBox.Text;
+ result.IsOk = true;
+ dialog.Close();
+ };
+
+ cancelButton.Click += (s, e) =>
+ {
+ result.IsOk = false;
+ dialog.Close();
+ };
+
+ inputBox.Focus();
+ dialog.ShowDialog();
+
+ return result;
+ }
+ }
+}
\ No newline at end of file
diff --git a/MainWindow.xaml b/MainWindow.xaml
index bcf5899..c16bc83 100644
--- a/MainWindow.xaml
+++ b/MainWindow.xaml
@@ -3,30 +3,14 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewmodels="clr-namespace:DirectoryCreator.ViewModels" mc:Ignorable="d" Title="Crear Directorios Base"
- Height="300" Width="500" WindowStartupLocation="CenterScreen">
+ Height="450" Width="700" WindowStartupLocation="CenterScreen" MinWidth="600" MinHeight="400">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -35,33 +19,72 @@
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
-
+
\ No newline at end of file
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 973ccff..9ecbcc7 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -111,16 +111,6 @@ namespace DirectoryCreator.ViewModels
}
}
- partial void OnSelectedConfigurationChanged(FolderConfig value)
- {
- if (value != null)
- {
- currentConfig = value;
- BasePath = value.BasePath;
- LoadLastProjectNumber();
- }
- }
-
[RelayCommand]
private void OpenBaseFolder()
{
@@ -241,6 +231,195 @@ namespace DirectoryCreator.ViewModels
Properties.Settings.Default.Save();
}
+ [ObservableProperty]
+ private ObservableCollection existingProjects = new();
+
+ [ObservableProperty]
+ private string selectedProject;
+
+ private void LoadExistingProjects()
+ {
+ try
+ {
+ if (string.IsNullOrEmpty(BasePath) || !Directory.Exists(BasePath))
+ {
+ ExistingProjects = new ObservableCollection();
+ return;
+ }
+
+ var directories = Directory.GetDirectories(BasePath)
+ .Select(d => new DirectoryInfo(d).Name)
+ .OrderByDescending(name => name)
+ .ToList();
+
+ ExistingProjects = new ObservableCollection(directories);
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show($"Error al cargar proyectos existentes: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+ ExistingProjects = new ObservableCollection();
+ }
+ }
+
+ partial void OnSelectedConfigurationChanged(FolderConfig value)
+ {
+ if (value != null)
+ {
+ currentConfig = value;
+ BasePath = value.BasePath;
+ LoadLastProjectNumber();
+ LoadExistingProjects(); // Cargar proyectos existentes cuando cambia la configuración
+ }
+ }
+
+ [RelayCommand]
+ private async Task RenameProject()
+ {
+ if (string.IsNullOrEmpty(SelectedProject))
+ {
+ MessageBox.Show("Por favor seleccione un proyecto para renombrar", "Advertencia", MessageBoxButton.OK, MessageBoxImage.Warning);
+ return;
+ }
+
+ var result = Microsoft.Windows.Controls.InputDialog.Show(
+ "Renombrar Proyecto",
+ "Ingrese el nuevo nombre del proyecto:",
+ SelectedProject);
+
+ if (result.IsOk)
+ {
+ string newName = result.Result;
+ if (string.IsNullOrWhiteSpace(newName))
+ {
+ MessageBox.Show("El nombre no puede estar vacío", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+ return;
+ }
+
+ try
+ {
+ foreach (var structure in currentConfig.FolderStructures)
+ {
+ var basePath = structure.BasePath.Replace("{year}", DateTime.Now.Year.ToString());
+ var oldPath = Path.Combine(basePath, SelectedProject);
+ var newPath = Path.Combine(basePath, newName);
+
+ if (Directory.Exists(oldPath))
+ {
+ if (Directory.Exists(newPath))
+ {
+ MessageBox.Show($"Ya existe un directorio con el nombre: {newName}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+ return;
+ }
+
+ Directory.Move(oldPath, newPath);
+ }
+ }
+
+ MessageBox.Show("Proyecto renombrado exitosamente!", "Éxito", MessageBoxButton.OK, MessageBoxImage.Information);
+ LoadExistingProjects(); // Recargar la lista de proyectos
+ SelectedProject = newName;
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show($"Error al renombrar el proyecto: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+ }
+ }
+ }
+
+ // Agregar este método en la clase MainWindowViewModel
+ [RelayCommand]
+ private void DeleteProject()
+ {
+ if (string.IsNullOrEmpty(SelectedProject))
+ {
+ MessageBox.Show("Por favor seleccione un proyecto para eliminar", "Advertencia", MessageBoxButton.OK, MessageBoxImage.Warning);
+ return;
+ }
+
+ try
+ {
+ // Recopilar todos los directorios a verificar
+ List directoriesToCheck = new List();
+ List nonEmptyDirectories = new List();
+
+ // Primero encontrar todos los directorios del proyecto
+ foreach (var structure in currentConfig.FolderStructures)
+ {
+ var basePath = structure.BasePath.Replace("{year}", DateTime.Now.Year.ToString());
+ var projectPath = Path.Combine(basePath, SelectedProject);
+
+ if (Directory.Exists(projectPath))
+ {
+ directoriesToCheck.Add(projectPath);
+ }
+ }
+
+ if (!directoriesToCheck.Any())
+ {
+ MessageBox.Show("No se encontraron directorios para eliminar.", "Información", MessageBoxButton.OK, MessageBoxImage.Information);
+ return;
+ }
+
+ // Verificar que todos los directorios estén vacíos
+ foreach (var directory in directoriesToCheck)
+ {
+ if (!IsDirectoryEmpty(directory))
+ {
+ nonEmptyDirectories.Add(directory);
+ }
+ }
+
+ // Si hay directorios con contenido, mostrar mensaje y cancelar
+ if (nonEmptyDirectories.Any())
+ {
+ string message = "Los siguientes directorios contienen archivos y no pueden ser eliminados:\n\n";
+ message += string.Join("\n", nonEmptyDirectories);
+ message += "\n\nDebe vaciar estos directorios antes de poder eliminar el proyecto.";
+ MessageBox.Show(message, "Directorios no vacíos", MessageBoxButton.OK, MessageBoxImage.Warning);
+ return;
+ }
+
+ // Si todos están vacíos, pedir confirmación
+ var result = MessageBox.Show(
+ $"Se eliminarán los siguientes directorios:\n\n{string.Join("\n", directoriesToCheck)}\n\n¿Está seguro que desea continuar?",
+ "Confirmar eliminación",
+ MessageBoxButton.YesNo,
+ MessageBoxImage.Warning);
+
+ if (result == MessageBoxResult.Yes)
+ {
+ // Eliminar todos los directorios
+ foreach (var directory in directoriesToCheck)
+ {
+ if (Directory.Exists(directory))
+ {
+ Directory.Delete(directory, false); // false porque ya sabemos que están vacíos
+ }
+ }
+
+ MessageBox.Show("Proyecto eliminado exitosamente!", "Éxito", MessageBoxButton.OK, MessageBoxImage.Information);
+ LoadExistingProjects(); // Recargar la lista de proyectos
+ SelectedProject = null;
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show($"Error al eliminar el proyecto: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+ }
+ }
+
+ private bool IsDirectoryEmpty(string path)
+ {
+ try
+ {
+ return !Directory.EnumerateFileSystemEntries(path, "*", SearchOption.AllDirectories).Any();
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+ }
+
[RelayCommand]
private void CreateDirectories()
{