Added Add-Operation + UI

This commit is contained in:
adroslice 2019-11-22 19:34:21 +01:00
parent 04b18f8140
commit a1d76d1920
4 changed files with 110 additions and 0 deletions

View File

@ -260,5 +260,25 @@
</Grid> </Grid>
</Expander> </Expander>
</DataTemplate> </DataTemplate>
<DataTemplate DataType="{x:Type ops:Add}">
<Expander Classes="OperationExpander">
<Grid ColumnDefinitions="auto,*" RowDefinitions="auto,auto,auto,auto">
<TextBlock Grid.Row="0" Text="Mode:"/>
<ComboBox Grid.Row="0" Items="{Binding Modes}" Name="ModeSelector" SelectedIndex="{Binding Mode}" PropertyChanged="PreviewChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Grid.Row="1" Text="Position:" IsVisible="{Binding #ModeSelector.SelectedItem.IsInsert}"/>
<NumericUpDown Grid.Row="1" Value="{Binding InsertPosition}" IsVisible="{Binding #ModeSelector.SelectedItem.IsInsert}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="2" Text="Text:"/>
<TextBox Grid.Row="2" Text="{Binding Text}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="3" Text="Full Name:"/>
<CheckBox Grid.Row="3" IsChecked="{Binding FullName}" PropertyChanged="PreviewChanged"/>
</Grid>
</Expander>
</DataTemplate>
</Window.DataTemplates> </Window.DataTemplates>
</Window> </Window>

View File

@ -27,6 +27,7 @@ namespace BFR
OperationType.Make<Replace>(), OperationType.Make<Replace>(),
OperationType.Make<Remove>(), OperationType.Make<Remove>(),
OperationType.Make<Number>(), OperationType.Make<Number>(),
OperationType.Make<Add>(),
}; };
public static readonly AvaloniaProperty<bool> IsCommitButtonEnabledProperty = public static readonly AvaloniaProperty<bool> IsCommitButtonEnabledProperty =

52
BFR/Operations/Add.cs Normal file
View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Linq;
using Avalonia;
using BFR.DataModels;
namespace BFR.Operations
{
public class Add : Operation
{
public override string Name => nameof(Add);
public override string Description => "Adds the specified text to the file name.";
[XmlIgnore]
public static readonly AvaloniaProperty<AddMode> ModeProperty =
AvaloniaProperty.Register<MainWindow, AddMode>(nameof(Mode), AddMode.Prepend);
public AddMode Mode { get => GetValue(ModeProperty); set => SetValue(ModeProperty, value); }
public AddOperationMode[] Modes => AddOperationMode.Modes;
public int InsertPosition { get; set; } = 0;
public string Text { get; set; } = "";
public bool FullName { get; set; } = false;
protected override void ApplyToInternal(IList<FileModel> files)
{
// Fail conditions: Out of bounds insert
var shortestLength = files.Min(x => FullName ? x.FullName.Length : x.Name.Length);
if (Mode == AddMode.Insert && InsertPosition > shortestLength) throw new OperationException($"Index can't be greater than the shortest file name length ({shortestLength}).");
// Apply operation
foreach (var file in files)
{
var newName = FullName ? file.FullName : file.Name;
newName = Mode switch
{
AddMode.Prepend => $"{Text}{newName}",
AddMode.Append => $"{newName}{Text}",
AddMode.Insert => newName.Insert(InsertPosition, Text),
_ => newName
};
if (FullName) file.FullName = newName;
else file.Name = newName;
}
}
}
}

37
BFR/Operations/AddMode.cs Normal file
View File

@ -0,0 +1,37 @@
using System;
using System.Linq;
using System.Collections.Generic;
using BFR.Helpers;
namespace BFR.Operations
{
public class AddOperationMode : OperationMode<AddMode>
{
public bool IsPrepend => Index == AddMode.Prepend;
public bool IsAppend => Index == AddMode.Append;
public bool IsInsert => Index == AddMode.Insert;
public readonly static AddOperationMode[] Modes = All();
public AddOperationMode(AddMode index, string name, string description) :
base(index, name, description) { }
protected static AddOperationMode[] All() => ((IEnumerable<AddMode>)Enum.GetValues(typeof(AddMode))).Select(x =>
new AddOperationMode(
x,
x.GetAttribute<OperationModeAttribute>().DisplayName,
x.GetAttribute<OperationModeAttribute>().Description
)).ToArray();
}
public enum AddMode
{
[OperationMode(nameof(Prepend), "Inserts before the file name.")]
Prepend,
[OperationMode(nameof(Append), "Inserts after the file name.")]
Append,
[OperationMode(nameof(Insert), "Inserts at the specified index.")]
Insert
}
}