Skip to content

Commit 4f25fb2

Browse files
authored
Find and replace implementation (#14)
* Fix regression where changes aren't highlighted * Find and replace implementation
1 parent e883b47 commit 4f25fb2

File tree

10 files changed

+231
-37
lines changed

10 files changed

+231
-37
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
**/obj
44
**/packages
55
**/*.bak
6+
**/*.dwl
7+
**/*.dwl2

FindAndReplaceCAD/FindAndReplaceCAD.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Reference Include="PresentationFramework" />
6363
<Reference Include="System" />
6464
<Reference Include="System.ComponentModel.DataAnnotations" />
65+
<Reference Include="System.Configuration" />
6566
<Reference Include="System.Data" />
6667
<Reference Include="System.Numerics" />
6768
<Reference Include="System.Xaml" />
@@ -73,6 +74,7 @@
7374
<Compile Include="UserControl\DetailsDialog.xaml.cs">
7475
<DependentUpon>DetailsDialog.xaml</DependentUpon>
7576
</Compile>
77+
<Compile Include="UserControl\EventArgs.cs" />
7678
<Compile Include="UserControl\FindAndReplace.xaml.cs">
7779
<DependentUpon>FindAndReplace.xaml</DependentUpon>
7880
</Compile>

FindAndReplaceCAD/MainWindow.xaml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</TabItem>
5353
<TabItem Header="Edit Text">
5454
<StackPanel>
55-
<local:FindAndReplace />
55+
<local:FindAndReplace FindChanged="FindAndReplace_FindClicked" ReplaceClicked="FindAndReplace_ReplaceClicked" />
5656
<Separator Height="2"/>
5757
<StackPanel Orientation="Horizontal" Margin="0,5,0,5">
5858
<Button Content="Strip New Lines" Click="btnStrip_Click"/>
@@ -63,13 +63,23 @@
6363

6464
</TabItem>
6565
<TabItem Header="Help">
66-
<StackPanel Orientation="Horizontal" Margin="10,10,10,10">
67-
<TextBlock Margin="0,0,10,0" Text="AutoCAD Character Encoding Reference:" />
68-
<TextBlock>
66+
<StackPanel Margin="10,10,10,10">
67+
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
68+
<TextBlock Margin="0,0,10,0" Text="AutoCAD Character Encoding Reference:" />
69+
<TextBlock>
6970
<Hyperlink NavigateUri="{Binding Path=CharacterEncodingURL, ElementName=MainControlWindow}" RequestNavigate="Hyperlink_RequestNavigate">
7071
Click Here
7172
</Hyperlink>
72-
</TextBlock>
73+
</TextBlock>
74+
</StackPanel>
75+
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
76+
<TextBlock Margin="0,0,10,0" Text="Regular Expression Reference:" />
77+
<TextBlock>
78+
<Hyperlink NavigateUri="{Binding Path=RegularExpressionURL, ElementName=MainControlWindow}" RequestNavigate="Hyperlink_RequestNavigate">
79+
Click Here
80+
</Hyperlink>
81+
</TextBlock>
82+
</StackPanel>
7383
</StackPanel>
7484
</TabItem>
7585
</TabControl>

FindAndReplaceCAD/MainWindow.xaml.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
using System;
33
using System.Collections.ObjectModel;
44
using System.ComponentModel;
5+
using System.Configuration;
56
using System.Diagnostics;
67
using System.Linq;
8+
using System.Text.RegularExpressions;
79
using System.Windows;
810
using System.Windows.Controls;
911
using System.Windows.Data;
@@ -59,6 +61,14 @@ public string CharacterEncodingURL
5961
}
6062
}
6163

64+
public string RegularExpressionURL
65+
{
66+
get
67+
{
68+
return $"https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference";
69+
}
70+
}
71+
6272
public event PropertyChangedEventHandler PropertyChanged;
6373
protected void NotifyPropertyChanged(string propertyName)
6474
{
@@ -198,5 +208,82 @@ private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e
198208
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
199209
e.Handled = true;
200210
}
211+
212+
private void FindAndReplace_FindClicked(object sender, EventArgs.FindClickedArgs e)
213+
{
214+
string findText = e.FindText;
215+
if (findText.Length == 0)
216+
{
217+
Test.ToList().ForEach(item => item.FoundInSearch = false);
218+
return;
219+
}
220+
221+
Test.ToList().ForEach(item => item.FoundInSearch = false);
222+
223+
if (e.IsRegex)
224+
{
225+
Regex r = null;
226+
try
227+
{
228+
if (e.IsCaseInsensitive)
229+
{
230+
r = new Regex(findText, RegexOptions.IgnoreCase);
231+
}
232+
else
233+
{
234+
r = new Regex(findText);
235+
}
236+
237+
}
238+
catch (ArgumentException)
239+
{
240+
return;
241+
}
242+
243+
Test.Where(item => r.IsMatch(item.NewText)).ToList().ForEach(item => item.FoundInSearch = true);
244+
}
245+
else
246+
{
247+
248+
Test.Where(item => item.NewText.IndexOf(findText, e.IsCaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture) > -1)
249+
.ToList().ForEach(item => item.FoundInSearch = true);
250+
}
251+
}
252+
253+
private void FindAndReplace_ReplaceClicked(object sender, EventArgs.ReplaceClickedArgs e)
254+
{
255+
string findText = e.FindText;
256+
string replaceText = e.ReplaceText;
257+
Test.ToList().ForEach(item => item.FoundInSearch = false);
258+
259+
if (e.IsRegex)
260+
{
261+
Regex r = null;
262+
try
263+
{
264+
if (e.IsCaseInsensitive)
265+
{
266+
r = new Regex(findText, RegexOptions.IgnoreCase);
267+
}
268+
else
269+
{
270+
r = new Regex(findText);
271+
}
272+
273+
}
274+
catch (ArgumentException)
275+
{
276+
return;
277+
}
278+
279+
Test.Where(item => r.IsMatch(item.NewText)).ToList().ForEach(item => item.NewText = r.Replace(item.NewText, replaceText));
280+
}
281+
else
282+
{
283+
Test.Where(item => item.NewText.Contains(findText)).ToList().ForEach(item => item.NewText = replaceText);
284+
}
285+
286+
FindAndReplace_FindClicked(null, new EventArgs.FindClickedArgs() { FindText = findText, IsRegex = e.IsRegex, IsCaseInsensitive = e.IsCaseInsensitive });
287+
}
201288
}
202289
}

FindAndReplaceCAD/ObjectInformation.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public string NewText
3636
NotifyPropertyChanged(nameof(NewText));
3737
NotifyEditableAttributeChanged(nameof(NewText));
3838
NotifyPropertyChanged(nameof(HasTextChanged));
39+
NotifyPropertyChanged(nameof(TextBackgroundColor));
3940
}
4041
}
4142
}
@@ -56,6 +57,7 @@ public bool NewMask {
5657
NotifyPropertyChanged(nameof(NewMask));
5758
NotifyEditableAttributeChanged(nameof(NewMask));
5859
NotifyPropertyChanged(nameof(HasMaskChanged));
60+
NotifyPropertyChanged(nameof(MaskBackgroundColor));
5961
}
6062
}
6163
}
@@ -80,7 +82,18 @@ public bool HasMaskChanged
8082
public SolidColorBrush TextBackgroundColor {
8183
get
8284
{
83-
return HasTextChanged ? new SolidColorBrush(Colors.Yellow) : new SolidColorBrush(Colors.Transparent);
85+
if (FoundInSearch)
86+
{
87+
return new SolidColorBrush(Colors.Aqua);
88+
}
89+
else if (HasTextChanged)
90+
{
91+
return new SolidColorBrush(Colors.Yellow);
92+
}
93+
else
94+
{
95+
return new SolidColorBrush(Colors.Transparent);
96+
}
8497
}
8598
}
8699

@@ -92,7 +105,23 @@ public SolidColorBrush MaskBackgroundColor
92105
}
93106
}
94107

95-
public int AttributesChanged()
108+
private bool _foundInSearch;
109+
public bool FoundInSearch
110+
{
111+
get
112+
{
113+
return _foundInSearch;
114+
}
115+
set
116+
{
117+
if (_foundInSearch != value) {
118+
_foundInSearch = value;
119+
NotifyPropertyChanged(nameof(TextBackgroundColor));
120+
}
121+
}
122+
}
123+
124+
public int AttributesChanged()
96125
{
97126
return (OriginalText != NewText ? 1 : 0) + (OriginalMask != NewMask ? 1 : 0);
98127
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace CADApp
2+
{
3+
public class EventArgs
4+
{
5+
public class FindClickedArgs
6+
{
7+
public string FindText { get; set; }
8+
public bool IsRegex { get; set; }
9+
public bool IsCaseInsensitive { get; set; }
10+
}
11+
12+
public class ReplaceClickedArgs
13+
{
14+
public string FindText { get; set; }
15+
public string ReplaceText { get; set; }
16+
public bool IsRegex { get; set; }
17+
public bool IsCaseInsensitive { get; set; }
18+
}
19+
}
20+
}

FindAndReplaceCAD/UserControl/FindAndReplace.xaml

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,46 @@
66
xmlns:local="clr-namespace:CADApp"
77
mc:Ignorable="d"
88
d:DesignHeight="450" d:DesignWidth="800">
9+
<UserControl.Resources>
10+
<Style TargetType="{x:Type Button}">
11+
<Setter Property="Margin" Value="10,0,10,0" />
12+
<Setter Property="Padding" Value="10,5,10,5" />
13+
<Setter Property="FontSize" Value="14" />
14+
</Style>
15+
</UserControl.Resources>
916
<Grid>
1017
<Grid.RowDefinitions>
1118
<RowDefinition Height="Auto" />
1219
<RowDefinition Height="Auto" />
1320
<RowDefinition Height="Auto" />
1421
</Grid.RowDefinitions>
15-
<Grid Margin="10" Grid.Row="0">
22+
<Grid Margin="10" Grid.Row="0" Height="35">
1623
<Grid.Resources>
1724
<Style TargetType="{x:Type TextBox}">
1825
<Setter Property="Margin" Value="0,0,10,0" />
1926
</Style>
2027
</Grid.Resources>
2128
<Grid.ColumnDefinitions>
2229
<ColumnDefinition Width="1*"/>
23-
<ColumnDefinition Width="100"/>
2430
</Grid.ColumnDefinitions>
25-
<TextBox Grid.Column="0" VerticalContentAlignment="Center" FontSize="14" />
26-
<Button Content="Find" Grid.Column="1" FontSize="14" Click="FindButton_Click"/>
31+
<TextBox Grid.Column="0" VerticalContentAlignment="Center" Text="{Binding FindString, UpdateSourceTrigger=PropertyChanged, Delay=200}" />
2732
</Grid>
28-
<Grid Margin="10" Grid.Row="1">
33+
<Grid Margin="10" Grid.Row="1" Height="35">
2934
<Grid.Resources>
3035
<Style TargetType="{x:Type TextBox}">
3136
<Setter Property="Margin" Value="0,0,10,0" />
3237
</Style>
3338
</Grid.Resources>
3439
<Grid.ColumnDefinitions>
3540
<ColumnDefinition Width="1*"/>
36-
<ColumnDefinition Width="100"/>
41+
<ColumnDefinition Width="125"/>
3742
</Grid.ColumnDefinitions>
38-
<TextBox Grid.Column="0" VerticalContentAlignment="Center" FontSize="14" />
39-
<Button Content="Replace" Grid.Column="1" FontSize="14" Click="ReplaceButton_Click"/>
40-
</Grid>
41-
<Grid Margin="10" Grid.Row="2">
42-
<Grid.ColumnDefinitions>
43-
<ColumnDefinition Width="1*"/>
44-
</Grid.ColumnDefinitions>
45-
<CheckBox Grid.Column="0" Content="Regex"/>
43+
<TextBox Grid.Column="0" VerticalContentAlignment="Center" Text="{Binding ReplaceString, UpdateSourceTrigger=PropertyChanged}" />
44+
<Button Content="Replace" Grid.Column="1" Click="ReplaceButton_Click"/>
4645
</Grid>
46+
<StackPanel Orientation="Horizontal" Margin="10" Grid.Row="2">
47+
<CheckBox Content="Regular Expression" IsChecked="{Binding IsRegex}"/>
48+
<CheckBox Margin="10,0,0,0" Content="Case Insensitive" IsChecked="{Binding IsCaseInsensitive}"/>
49+
</StackPanel>
4750
</Grid>
4851
</UserControl>
Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using System.Windows;
1+
using System;
2+
using System.Threading.Tasks;
3+
using System.Windows;
24
using System.Windows.Controls;
5+
using System.Windows.Threading;
36

47
namespace CADApp
58
{
@@ -8,19 +11,67 @@ namespace CADApp
811
/// </summary>
912
public partial class FindAndReplace : UserControl
1013
{
11-
public FindAndReplace()
14+
public event EventHandler<EventArgs.FindClickedArgs> FindChanged;
15+
16+
public event EventHandler<EventArgs.ReplaceClickedArgs> ReplaceClicked;
17+
18+
private string _findString;
19+
public string FindString
1220
{
13-
InitializeComponent();
21+
get
22+
{
23+
return _findString;
24+
}
25+
set
26+
{
27+
if (_findString != value) {
28+
_findString = value;
29+
FindChanged?.Invoke(this, new EventArgs.FindClickedArgs() { FindText = FindString, IsRegex = IsRegex, IsCaseInsensitive = IsCaseInsensitive });
30+
}
31+
}
1432
}
1533

16-
private void FindButton_Click(object sender, RoutedEventArgs e)
17-
{
34+
public string ReplaceString { get; set; }
1835

36+
private bool _isRegex;
37+
public bool IsRegex {
38+
get
39+
{
40+
return _isRegex;
41+
}
42+
set
43+
{
44+
if (_isRegex != value) {
45+
_isRegex = value;
46+
FindChanged?.Invoke(this, new EventArgs.FindClickedArgs() { FindText = FindString, IsRegex = IsRegex, IsCaseInsensitive = IsCaseInsensitive });
47+
}
48+
}
1949
}
2050

21-
private void ReplaceButton_Click(object sender, RoutedEventArgs e)
51+
private bool _isCaseInsensitive;
52+
public bool IsCaseInsensitive {
53+
get
54+
{
55+
return _isCaseInsensitive;
56+
}
57+
set
58+
{
59+
if (_isCaseInsensitive != value) {
60+
_isCaseInsensitive = value;
61+
FindChanged?.Invoke(this, new EventArgs.FindClickedArgs() { FindText = FindString, IsRegex = IsRegex, IsCaseInsensitive = IsCaseInsensitive });
62+
}
63+
}
64+
}
65+
66+
public FindAndReplace()
2267
{
68+
this.DataContext = this;
69+
InitializeComponent();
70+
}
2371

72+
private void ReplaceButton_Click(object sender, RoutedEventArgs e)
73+
{
74+
ReplaceClicked?.Invoke(this, new EventArgs.ReplaceClickedArgs() { FindText = FindString, ReplaceText = ReplaceString, IsRegex = IsRegex, IsCaseInsensitive = IsCaseInsensitive });
2475
}
2576
}
2677
}

FindAndReplaceCAD/config/Drawing1.dwl

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)