Skip to content

Commit cae0d96

Browse files
committed
Fix several bugs in the new UI for AnnotateDeclaration
Also reorganizes it a bit internally because the original setup did not work as expected. In addition, default values are now set when switching the argument type.
1 parent 799b76a commit cae0d96

17 files changed

+226
-111
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows;
4+
using System.Windows.Data;
5+
using Rubberduck.Parsing.Annotations;
6+
7+
namespace Rubberduck.UI.Converters
8+
{
9+
public class AnnotationToCodeStringConverter : IValueConverter
10+
{
11+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12+
{
13+
if (value == null)
14+
{
15+
return null;
16+
}
17+
18+
if (!(value is IAnnotation annotation))
19+
{
20+
throw new ArgumentException("The value must be an instance of IAnnotation.", "value");
21+
}
22+
23+
return $"@{annotation.Name}";
24+
}
25+
26+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
27+
{
28+
return DependencyProperty.UnsetValue;
29+
}
30+
}
31+
}

Rubberduck.Core/UI/Converters/InspectionToLocalizedNameConverter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ public class InspectionToLocalizedNameConverter : IValueConverter
1212
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1313
{
1414
var inspectionName = value is IInspection inspection
15-
? inspection.Name
15+
? inspection.AnnotationName
1616
: value as string;
1717

1818
if (inspectionName == null)
1919
{
20-
2120
throw new ArgumentException("The value must be an instance of IInspection or a string containing the programmatic name of an inspection.", "value");
2221
}
2322

24-
return InspectionNames.ResourceManager.GetString(inspectionName, culture);
23+
return InspectionNames.ResourceManager.GetString($"{inspectionName}Inspection", culture);
2524
}
2625

2726
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Windows;
6+
using System.Windows.Data;
7+
8+
namespace Rubberduck.UI.Converters
9+
{
10+
public class SpecificValuesToVisibilityConverter : IValueConverter
11+
{
12+
public IReadOnlyCollection<object> SpecialValues { get; set; }
13+
public bool CollapseSpecialValues { get; set; }
14+
15+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
16+
{
17+
if (value == null)
18+
{
19+
return null;
20+
}
21+
22+
return SpecialValues.Contains(value)
23+
? SpecialValueVisibility
24+
: OtherValueVisibility;
25+
}
26+
27+
private Visibility SpecialValueVisibility => CollapseSpecialValues ? Visibility.Collapsed : Visibility.Visible;
28+
private Visibility OtherValueVisibility => CollapseSpecialValues ? Visibility.Visible : Visibility.Collapsed;
29+
30+
31+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
32+
{
33+
return DependencyProperty.UnsetValue;
34+
}
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows;
4+
using System.Windows.Data;
5+
6+
namespace Rubberduck.UI.Converters
7+
{
8+
public class SpecificValueToVisibilityConverter : IValueConverter
9+
{
10+
public object SpecialValue { get; set; }
11+
public bool CollapseSpecialValue { get; set; }
12+
13+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
14+
{
15+
if (value == null)
16+
{
17+
return null;
18+
}
19+
20+
return value.Equals(SpecialValue)
21+
? SpecialValueVisibility
22+
: OtherValueVisibility;
23+
}
24+
25+
private Visibility SpecialValueVisibility => CollapseSpecialValue ? Visibility.Collapsed : Visibility.Visible;
26+
private Visibility OtherValueVisibility => CollapseSpecialValue ? Visibility.Visible : Visibility.Collapsed;
27+
28+
29+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
30+
{
31+
return DependencyProperty.UnsetValue;
32+
}
33+
}
34+
}

Rubberduck.Core/UI/Refactorings/AnnotateDeclaration/AnnotateDeclarationPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Rubberduck.UI.Refactorings.AnnotateDeclaration
55
{
66
internal class AnnotateDeclarationPresenter : RefactoringPresenterBase<AnnotateDeclarationModel>, IAnnotateDeclarationPresenter
77
{
8-
private static readonly DialogData DialogData = DialogData.Create(RubberduckUI.AnnotateDeclarationDialog_Caption, 164, 684);
8+
private static readonly DialogData DialogData = DialogData.Create(RubberduckUI.AnnotateDeclarationDialog_Caption, 400, 300);
99

1010
public AnnotateDeclarationPresenter(AnnotateDeclarationModel model, IRefactoringDialogFactory dialogFactory) :
1111
base(DialogData, model, dialogFactory)

Rubberduck.Core/UI/Refactorings/AnnotateDeclaration/AnnotateDeclarationView.xaml

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
xmlns:local="clr-namespace:Rubberduck.UI.Refactorings.AnnotateDeclaration"
88
xmlns:annotations="clr-namespace:Rubberduck.Parsing.Annotations;assembly=Rubberduck.Parsing"
99
mc:Ignorable="d"
10-
d:DesignHeight="300" d:DesignWidth="300">
10+
d:DesignHeight="400" d:DesignWidth="300">
1111
<UserControl.Resources>
1212
<ResourceDictionary>
1313
<ResourceDictionary.MergedDictionaries>
@@ -17,20 +17,24 @@
1717
<converters:EnumToLocalizedStringConverter ResourcePrefix="AnnotationArgumentType_" x:Key="AnnotationArgumentTypeToStringConverter"/>
1818
<converters:InspectionToLocalizedNameConverter x:Key="InspectionToLocalizedNameConverter"/>
1919
<converters:DeclarationToQualifiedNameConverter x:Key="DeclarationToQualifiedNameConverter"/>
20+
<converters:AnnotationToCodeStringConverter x:Key="AnnotationToCodeStringConverter"/>
2021

2122
<BitmapImage x:Key="InvalidTextImage" UriSource="pack://application:,,,/Rubberduck.Resources;component/Icons/Fugue/cross-circle.png" />
2223
<Style x:Key="TextBoxErrorStyle" TargetType="{x:Type TextBox}">
2324
<Setter Property="Validation.ErrorTemplate">
2425
<Setter.Value>
2526
<ControlTemplate x:Name="TextBoxErrorTemplate">
26-
<DockPanel LastChildFill="True">
27-
<AdornedElementPlaceholder/>
28-
<Image Source="{StaticResource InvalidTextImage}"
29-
Height="16"
30-
Margin="0,-8,-8,0"
31-
HorizontalAlignment="Right"
32-
VerticalAlignment="Top"/>
33-
</DockPanel>
27+
<Canvas>
28+
<DockPanel LastChildFill="True" ClipToBounds="False">
29+
<AdornedElementPlaceholder/>
30+
<Image Source="{StaticResource InvalidTextImage}"
31+
Height="16"
32+
Margin="0,-8,-8,0"
33+
HorizontalAlignment="Right"
34+
VerticalAlignment="Top"
35+
ClipToBounds="False"/>
36+
</DockPanel>
37+
</Canvas>
3438
</ControlTemplate>
3539
</Setter.Value>
3640
</Setter>
@@ -69,7 +73,7 @@
6973
Margin="5,0">
7074
<ComboBox.ItemTemplate>
7175
<DataTemplate DataType="{x:Type annotations:IAnnotation}">
72-
<TextBlock Text="{Binding Name, StringFormat=@{}{0}}"/>
76+
<TextBlock Text="{Binding Path=., Converter={StaticResource AnnotationToCodeStringConverter}}"/>
7377
</DataTemplate>
7478
</ComboBox.ItemTemplate>
7579
</ComboBox>
@@ -86,36 +90,47 @@
8690
</DataTemplate>
8791
<DataTemplate DataType="{x:Type local:IAnnotationArgumentViewModel}" x:Key="ArgumentMultiTypeTemplate">
8892
<ComboBox
89-
ItemsSource="{Binding Path=ApplicableArgumentTypes, Converter={StaticResource AnnotationArgumentTypeToStringConverter}}"
90-
SelectedItem="{Binding Path=ArgumentType, Converter={StaticResource AnnotationArgumentTypeToStringConverter}}"
91-
IsSynchronizedWithCurrentItem="True"/>
92-
</DataTemplate>
93-
<DataTemplate DataType="{x:Type local:IAnnotationArgumentViewModel}" x:Key="ArgumentValueTemplate">
94-
<TextBox
95-
Text="{Binding Path=ArgumentValue, ValidatesOnNotifyDataErrors=True}"
96-
Style="{StaticResource TextBoxErrorStyle}"/>
97-
</DataTemplate>
98-
<DataTemplate DataType="{x:Type local:IAnnotationArgumentViewModel}" x:Key="ArgumentValueBooleanTemplate">
99-
<ComboBox
100-
SelectedItem="{Binding Path=ArgumentValue}"
101-
IsSynchronizedWithCurrentItem="True">
102-
<TextBlock Text="True"/>
103-
<TextBlock Text="False"/>
104-
</ComboBox>
105-
</DataTemplate>
106-
<DataTemplate DataType="{x:Type local:IAnnotationArgumentViewModel}" x:Key="ArgumentValueInspectionTemplate">
107-
<ComboBox
108-
SelectedItem="{Binding Path=ArgumentValue}"
93+
ItemsSource="{Binding Path=ApplicableArgumentTypes}"
94+
SelectedItem="{Binding Path=ArgumentType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
10995
IsSynchronizedWithCurrentItem="True">
11096
<ComboBox.ItemTemplate>
111-
<DataTemplate>
112-
<TextBlock Text="{Binding Path=., Converter={StaticResource InspectionToLocalizedNameConverter}}"/>
97+
<DataTemplate DataType="{x:Type annotations:AnnotationArgumentType}">
98+
<TextBlock Text="{Binding Path=., Converter={StaticResource AnnotationArgumentTypeToStringConverter}}"/>
11399
</DataTemplate>
114100
</ComboBox.ItemTemplate>
115101
</ComboBox>
116102
</DataTemplate>
103+
<x:Array Type="sys:Object" xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Key="NonDefaultInputArgumentTypes">
104+
<annotations:AnnotationArgumentType>Boolean</annotations:AnnotationArgumentType>
105+
<annotations:AnnotationArgumentType>Inspection</annotations:AnnotationArgumentType>
106+
</x:Array>
107+
<converters:SpecificValuesToVisibilityConverter SpecialValues="{StaticResource NonDefaultInputArgumentTypes}" CollapseSpecialValues="True" x:Key="DefaultArgumentVisibilityConverter"/>
108+
<converters:SpecificValueToVisibilityConverter SpecialValue="{x:Static annotations:AnnotationArgumentType.Boolean}" CollapseSpecialValue="False" x:Key="BooleanArgumentVisibilityConverter"/>
109+
<converters:SpecificValueToVisibilityConverter SpecialValue="{x:Static annotations:AnnotationArgumentType.Inspection}" CollapseSpecialValue="False" x:Key="InspectionArgumentVisibilityConverter"/>
110+
<DataTemplate DataType="{x:Type local:IAnnotationArgumentViewModel}" x:Key="ArgumentValueTemplate">
111+
<StackPanel>
112+
<TextBox Text="{Binding ArgumentValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"
113+
Style="{StaticResource TextBoxErrorStyle}"
114+
Visibility="{Binding ArgumentType, Converter={StaticResource DefaultArgumentVisibilityConverter}}">
115+
</TextBox>
116+
<ComboBox
117+
Visibility="{Binding ArgumentType, Converter={StaticResource BooleanArgumentVisibilityConverter}}"
118+
ItemsSource="{Binding Path=BooleanValues}"
119+
SelectedItem="{Binding Path=ArgumentValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}">
120+
</ComboBox>
121+
<ComboBox
122+
Visibility="{Binding ArgumentType, Converter={StaticResource InspectionArgumentVisibilityConverter}}"
123+
ItemsSource="{Binding Path=InspectionNames}"
124+
SelectedItem="{Binding Path=ArgumentValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}">
125+
<ComboBox.ItemTemplate>
126+
<DataTemplate>
127+
<TextBlock Text="{Binding Path=., Converter={StaticResource InspectionToLocalizedNameConverter}, UpdateSourceTrigger=PropertyChanged}"/>
128+
</DataTemplate>
129+
</ComboBox.ItemTemplate>
130+
</ComboBox>
131+
</StackPanel>
132+
</DataTemplate>
117133
<local:AnnotationArgumentTypeCellDataTemplateSelector x:Key="AnnotationArgumentTypeCellDataTemplateSelector"/>
118-
<local:AnnotationArgumentValueCellDataTemplateSelector x:Key="AnnotationArgumentValueCellDataTemplateSelector"/>
119134
</Grid.Resources>
120135
<DataGrid
121136
Grid.Column="0"
@@ -128,13 +143,13 @@
128143
<DataGridTemplateColumn
129144
Header="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=AnnotateDeclarationDialog_ArgumentTypeHeader}"
130145
CellTemplateSelector="{StaticResource AnnotationArgumentTypeCellDataTemplateSelector}"
131-
CellEditingTemplateSelector="{StaticResource AnnotationArgumentTypeCellDataTemplateSelector}">
132-
</DataGridTemplateColumn>
146+
CellEditingTemplateSelector="{StaticResource AnnotationArgumentTypeCellDataTemplateSelector}"
147+
Width="Auto"/>
133148
<DataGridTemplateColumn
134149
Header="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=AnnotateDeclarationDialog_ArgumentValueHeader}"
135-
CellTemplateSelector="{StaticResource AnnotationArgumentValueCellDataTemplateSelector}"
136-
CellEditingTemplateSelector="{StaticResource AnnotationArgumentValueCellDataTemplateSelector}">
137-
</DataGridTemplateColumn>
150+
CellTemplate="{StaticResource ArgumentValueTemplate}"
151+
CellEditingTemplate="{StaticResource ArgumentValueTemplate}"
152+
Width="*"/>
138153
</DataGrid.Columns>
139154
</DataGrid>
140155
<StackPanel

Rubberduck.Core/UI/Refactorings/AnnotateDeclaration/AnnotateDeclarationViewModel.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.ComponentModel;
23
using System.Linq;
34
using NLog;
45
using Rubberduck.Parsing.Annotations;
@@ -75,6 +76,7 @@ public IAnnotation Annotation
7576
RefreshAnnotationArguments(value);
7677

7778
OnPropertyChanged();
79+
OnPropertyChanged("IsValidAnnotation");
7880
}
7981
}
8082

@@ -102,7 +104,14 @@ private IEnumerable<IAnnotationArgumentViewModel> InitialAnnotationArguments(IAn
102104

103105
private IAnnotationArgumentViewModel InitialArgumentViewModel(AnnotationArgumentType argumentType)
104106
{
105-
return _argumentFactory.Create(argumentType, string.Empty);
107+
var argumentModel = _argumentFactory.Create(argumentType, string.Empty);
108+
argumentModel.ErrorsChanged += ArgumentErrorStateChanged;
109+
return argumentModel;
110+
}
111+
112+
private void ArgumentErrorStateChanged(object requestor, DataErrorsChangedEventArgs e)
113+
{
114+
OnPropertyChanged("IsValidAnnotation");
106115
}
107116

108117
public CommandBase AddAnnotationArgument { get; }
@@ -147,6 +156,6 @@ protected override void DialogOk()
147156
}
148157

149158
public bool IsValidAnnotation => Annotation != null
150-
&& AnnotationArguments.All(argument => !argument.HasErrors);
159+
&& AnnotationArguments.All(argument => !argument.HasErrors);
151160
}
152161
}

Rubberduck.Core/UI/Refactorings/AnnotateDeclaration/AnnotationArgumentValueCelDataTemplateSelector.cs

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

0 commit comments

Comments
 (0)