Skip to content

Commit

Permalink
Find and replace implementation (#14)
Browse files Browse the repository at this point in the history
* Fix regression where changes aren't highlighted

* Find and replace implementation
  • Loading branch information
Obbay2 authored Mar 30, 2023
1 parent e883b47 commit 4f25fb2
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
**/obj
**/packages
**/*.bak
**/*.dwl
**/*.dwl2
2 changes: 2 additions & 0 deletions FindAndReplaceCAD/FindAndReplaceCAD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xaml" />
Expand All @@ -73,6 +74,7 @@
<Compile Include="UserControl\DetailsDialog.xaml.cs">
<DependentUpon>DetailsDialog.xaml</DependentUpon>
</Compile>
<Compile Include="UserControl\EventArgs.cs" />
<Compile Include="UserControl\FindAndReplace.xaml.cs">
<DependentUpon>FindAndReplace.xaml</DependentUpon>
</Compile>
Expand Down
20 changes: 15 additions & 5 deletions FindAndReplaceCAD/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</TabItem>
<TabItem Header="Edit Text">
<StackPanel>
<local:FindAndReplace />
<local:FindAndReplace FindChanged="FindAndReplace_FindClicked" ReplaceClicked="FindAndReplace_ReplaceClicked" />
<Separator Height="2"/>
<StackPanel Orientation="Horizontal" Margin="0,5,0,5">
<Button Content="Strip New Lines" Click="btnStrip_Click"/>
Expand All @@ -63,13 +63,23 @@

</TabItem>
<TabItem Header="Help">
<StackPanel Orientation="Horizontal" Margin="10,10,10,10">
<TextBlock Margin="0,0,10,0" Text="AutoCAD Character Encoding Reference:" />
<TextBlock>
<StackPanel Margin="10,10,10,10">
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
<TextBlock Margin="0,0,10,0" Text="AutoCAD Character Encoding Reference:" />
<TextBlock>
<Hyperlink NavigateUri="{Binding Path=CharacterEncodingURL, ElementName=MainControlWindow}" RequestNavigate="Hyperlink_RequestNavigate">
Click Here
</Hyperlink>
</TextBlock>
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
<TextBlock Margin="0,0,10,0" Text="Regular Expression Reference:" />
<TextBlock>
<Hyperlink NavigateUri="{Binding Path=RegularExpressionURL, ElementName=MainControlWindow}" RequestNavigate="Hyperlink_RequestNavigate">
Click Here
</Hyperlink>
</TextBlock>
</StackPanel>
</StackPanel>
</TabItem>
</TabControl>
Expand Down
87 changes: 87 additions & 0 deletions FindAndReplaceCAD/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand Down Expand Up @@ -59,6 +61,14 @@ public string CharacterEncodingURL
}
}

public string RegularExpressionURL
{
get
{
return $"https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference";
}
}

public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
Expand Down Expand Up @@ -198,5 +208,82 @@ private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}

private void FindAndReplace_FindClicked(object sender, EventArgs.FindClickedArgs e)
{
string findText = e.FindText;
if (findText.Length == 0)
{
Test.ToList().ForEach(item => item.FoundInSearch = false);
return;
}

Test.ToList().ForEach(item => item.FoundInSearch = false);

if (e.IsRegex)
{
Regex r = null;
try
{
if (e.IsCaseInsensitive)
{
r = new Regex(findText, RegexOptions.IgnoreCase);
}
else
{
r = new Regex(findText);
}

}
catch (ArgumentException)
{
return;
}

Test.Where(item => r.IsMatch(item.NewText)).ToList().ForEach(item => item.FoundInSearch = true);
}
else
{

Test.Where(item => item.NewText.IndexOf(findText, e.IsCaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture) > -1)
.ToList().ForEach(item => item.FoundInSearch = true);
}
}

private void FindAndReplace_ReplaceClicked(object sender, EventArgs.ReplaceClickedArgs e)
{
string findText = e.FindText;
string replaceText = e.ReplaceText;
Test.ToList().ForEach(item => item.FoundInSearch = false);

if (e.IsRegex)
{
Regex r = null;
try
{
if (e.IsCaseInsensitive)
{
r = new Regex(findText, RegexOptions.IgnoreCase);
}
else
{
r = new Regex(findText);
}

}
catch (ArgumentException)
{
return;
}

Test.Where(item => r.IsMatch(item.NewText)).ToList().ForEach(item => item.NewText = r.Replace(item.NewText, replaceText));
}
else
{
Test.Where(item => item.NewText.Contains(findText)).ToList().ForEach(item => item.NewText = replaceText);
}

FindAndReplace_FindClicked(null, new EventArgs.FindClickedArgs() { FindText = findText, IsRegex = e.IsRegex, IsCaseInsensitive = e.IsCaseInsensitive });
}
}
}
33 changes: 31 additions & 2 deletions FindAndReplaceCAD/ObjectInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public string NewText
NotifyPropertyChanged(nameof(NewText));
NotifyEditableAttributeChanged(nameof(NewText));
NotifyPropertyChanged(nameof(HasTextChanged));
NotifyPropertyChanged(nameof(TextBackgroundColor));
}
}
}
Expand All @@ -56,6 +57,7 @@ public bool NewMask {
NotifyPropertyChanged(nameof(NewMask));
NotifyEditableAttributeChanged(nameof(NewMask));
NotifyPropertyChanged(nameof(HasMaskChanged));
NotifyPropertyChanged(nameof(MaskBackgroundColor));
}
}
}
Expand All @@ -80,7 +82,18 @@ public bool HasMaskChanged
public SolidColorBrush TextBackgroundColor {
get
{
return HasTextChanged ? new SolidColorBrush(Colors.Yellow) : new SolidColorBrush(Colors.Transparent);
if (FoundInSearch)
{
return new SolidColorBrush(Colors.Aqua);
}
else if (HasTextChanged)
{
return new SolidColorBrush(Colors.Yellow);
}
else
{
return new SolidColorBrush(Colors.Transparent);
}
}
}

Expand All @@ -92,7 +105,23 @@ public SolidColorBrush MaskBackgroundColor
}
}

public int AttributesChanged()
private bool _foundInSearch;
public bool FoundInSearch
{
get
{
return _foundInSearch;
}
set
{
if (_foundInSearch != value) {
_foundInSearch = value;
NotifyPropertyChanged(nameof(TextBackgroundColor));
}
}
}

public int AttributesChanged()
{
return (OriginalText != NewText ? 1 : 0) + (OriginalMask != NewMask ? 1 : 0);
}
Expand Down
20 changes: 20 additions & 0 deletions FindAndReplaceCAD/UserControl/EventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace CADApp
{
public class EventArgs
{
public class FindClickedArgs
{
public string FindText { get; set; }
public bool IsRegex { get; set; }
public bool IsCaseInsensitive { get; set; }
}

public class ReplaceClickedArgs
{
public string FindText { get; set; }
public string ReplaceText { get; set; }
public bool IsRegex { get; set; }
public bool IsCaseInsensitive { get; set; }
}
}
}
31 changes: 17 additions & 14 deletions FindAndReplaceCAD/UserControl/FindAndReplace.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,46 @@
xmlns:local="clr-namespace:CADApp"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10,0,10,0" />
<Setter Property="Padding" Value="10,5,10,5" />
<Setter Property="FontSize" Value="14" />
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Margin="10" Grid.Row="0">
<Grid Margin="10" Grid.Row="0" Height="35">
<Grid.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,0,10,0" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" VerticalContentAlignment="Center" FontSize="14" />
<Button Content="Find" Grid.Column="1" FontSize="14" Click="FindButton_Click"/>
<TextBox Grid.Column="0" VerticalContentAlignment="Center" Text="{Binding FindString, UpdateSourceTrigger=PropertyChanged, Delay=200}" />
</Grid>
<Grid Margin="10" Grid.Row="1">
<Grid Margin="10" Grid.Row="1" Height="35">
<Grid.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,0,10,0" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="125"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" VerticalContentAlignment="Center" FontSize="14" />
<Button Content="Replace" Grid.Column="1" FontSize="14" Click="ReplaceButton_Click"/>
</Grid>
<Grid Margin="10" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Content="Regex"/>
<TextBox Grid.Column="0" VerticalContentAlignment="Center" Text="{Binding ReplaceString, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Replace" Grid.Column="1" Click="ReplaceButton_Click"/>
</Grid>
<StackPanel Orientation="Horizontal" Margin="10" Grid.Row="2">
<CheckBox Content="Regular Expression" IsChecked="{Binding IsRegex}"/>
<CheckBox Margin="10,0,0,0" Content="Case Insensitive" IsChecked="{Binding IsCaseInsensitive}"/>
</StackPanel>
</Grid>
</UserControl>
63 changes: 57 additions & 6 deletions FindAndReplaceCAD/UserControl/FindAndReplace.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Windows;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace CADApp
{
Expand All @@ -8,19 +11,67 @@ namespace CADApp
/// </summary>
public partial class FindAndReplace : UserControl
{
public FindAndReplace()
public event EventHandler<EventArgs.FindClickedArgs> FindChanged;

public event EventHandler<EventArgs.ReplaceClickedArgs> ReplaceClicked;

private string _findString;
public string FindString
{
InitializeComponent();
get
{
return _findString;
}
set
{
if (_findString != value) {
_findString = value;
FindChanged?.Invoke(this, new EventArgs.FindClickedArgs() { FindText = FindString, IsRegex = IsRegex, IsCaseInsensitive = IsCaseInsensitive });
}
}
}

private void FindButton_Click(object sender, RoutedEventArgs e)
{
public string ReplaceString { get; set; }

private bool _isRegex;
public bool IsRegex {
get
{
return _isRegex;
}
set
{
if (_isRegex != value) {
_isRegex = value;
FindChanged?.Invoke(this, new EventArgs.FindClickedArgs() { FindText = FindString, IsRegex = IsRegex, IsCaseInsensitive = IsCaseInsensitive });
}
}
}

private void ReplaceButton_Click(object sender, RoutedEventArgs e)
private bool _isCaseInsensitive;
public bool IsCaseInsensitive {
get
{
return _isCaseInsensitive;
}
set
{
if (_isCaseInsensitive != value) {
_isCaseInsensitive = value;
FindChanged?.Invoke(this, new EventArgs.FindClickedArgs() { FindText = FindString, IsRegex = IsRegex, IsCaseInsensitive = IsCaseInsensitive });
}
}
}

public FindAndReplace()
{
this.DataContext = this;
InitializeComponent();
}

private void ReplaceButton_Click(object sender, RoutedEventArgs e)
{
ReplaceClicked?.Invoke(this, new EventArgs.ReplaceClickedArgs() { FindText = FindString, ReplaceText = ReplaceString, IsRegex = IsRegex, IsCaseInsensitive = IsCaseInsensitive });
}
}
}
3 changes: 0 additions & 3 deletions FindAndReplaceCAD/config/Drawing1.dwl

This file was deleted.

Loading

0 comments on commit 4f25fb2

Please sign in to comment.