@@ -29,56 +29,18 @@ public string ParameterName
29
29
30
30
public Declaration TargetDeclaration { get ; set ; }
31
31
32
- public bool CanImplementLetSetterType
33
- {
34
- get { return LetSetterTypeCheckBox . Enabled ; }
35
- set
36
- {
37
- if ( ! value )
38
- {
39
- LetSetterTypeCheckBox . Checked = false ;
40
- }
41
- LetSetterTypeCheckBox . Enabled = value ;
42
- }
43
- }
32
+ public bool CanImplementLetSetterType { get ; set ; }
44
33
45
- public bool CanImplementSetSetterType
46
- {
47
- get { return SetSetterTypeCheckBox . Enabled ; }
48
- set
49
- {
50
- if ( ! value )
51
- {
52
- SetSetterTypeCheckBox . Checked = false ;
53
- }
54
- SetSetterTypeCheckBox . Enabled = value ;
55
- }
56
- }
34
+ public bool CanImplementSetSetterType { get ; set ; }
57
35
58
36
public bool MustImplementLetSetterType
59
37
{
60
- get { return LetSetterTypeCheckBox . Checked ; }
61
- set
62
- {
63
- if ( value )
64
- {
65
- LetSetterTypeCheckBox . Checked = true ;
66
- }
67
- LetSetterTypeCheckBox . Enabled = ! value ;
68
- }
38
+ get { return CanImplementLetSetterType && ! CanImplementSetSetterType ; }
69
39
}
70
40
71
41
public bool MustImplementSetSetterType
72
42
{
73
- get { return SetSetterTypeCheckBox . Checked ; }
74
- set
75
- {
76
- if ( value )
77
- {
78
- SetSetterTypeCheckBox . Checked = true ;
79
- }
80
- SetSetterTypeCheckBox . Enabled = ! value ;
81
- }
43
+ get { return CanImplementSetSetterType && ! CanImplementLetSetterType ; }
82
44
}
83
45
84
46
public EncapsulateFieldDialog ( RubberduckParserState state , IIndenter indenter )
@@ -90,10 +52,7 @@ public EncapsulateFieldDialog(RubberduckParserState state, IIndenter indenter)
90
52
91
53
PropertyNameTextBox . TextChanged += PropertyNameBox_TextChanged ;
92
54
ParameterNameTextBox . TextChanged += VariableNameBox_TextChanged ;
93
-
94
- LetSetterTypeCheckBox . CheckedChanged += EncapsulateFieldDialog_SetterTypeChanged ;
95
- SetSetterTypeCheckBox . CheckedChanged += EncapsulateFieldDialog_SetterTypeChanged ;
96
-
55
+
97
56
Shown += EncapsulateFieldDialog_Shown ;
98
57
}
99
58
@@ -116,11 +75,36 @@ private void LocalizeDialog()
116
75
117
76
void EncapsulateFieldDialog_Shown ( object sender , EventArgs e )
118
77
{
78
+ if ( MustImplementSetSetterType )
79
+ {
80
+ SetSetterTypeCheckBox . Checked = true ;
81
+ DisableAssignmentSelection ( ) ;
82
+ }
83
+ else
84
+ {
85
+ LetSetterTypeCheckBox . Checked = true ;
86
+ if ( MustImplementLetSetterType )
87
+ {
88
+ DisableAssignmentSelection ( ) ;
89
+ }
90
+ else
91
+ {
92
+ LetSetterTypeCheckBox . CheckedChanged += EncapsulateFieldDialog_SetterTypeChanged ;
93
+ SetSetterTypeCheckBox . CheckedChanged += EncapsulateFieldDialog_SetterTypeChanged ;
94
+ }
95
+ }
96
+
119
97
ValidatePropertyName ( ) ;
120
98
ValidateVariableName ( ) ;
121
99
UpdatePreview ( ) ;
122
100
}
123
101
102
+ private void DisableAssignmentSelection ( )
103
+ {
104
+ LetSetterTypeCheckBox . Enabled = false ;
105
+ SetSetterTypeCheckBox . Enabled = false ;
106
+ }
107
+
124
108
private void PropertyNameBox_TextChanged ( object sender , EventArgs e )
125
109
{
126
110
ValidatePropertyName ( ) ;
@@ -142,10 +126,7 @@ private string GetPropertyText()
142
126
{
143
127
if ( TargetDeclaration == null ) { return string . Empty ; }
144
128
145
- var getterText = string . Join ( Environment . NewLine ,
146
- string . Format ( "Public Property Get {0}() As {1}" , NewPropertyName , TargetDeclaration . AsTypeName ) ,
147
- string . Format ( " {0}{1} = {2}" , MustImplementSetSetterType || ! CanImplementLetSetterType ? "Set " : string . Empty , NewPropertyName , TargetDeclaration . IdentifierName ) ,
148
- "End Property" ) ;
129
+ var getterText = GenerateGetter ( ) ;
149
130
150
131
var letterText = string . Join ( Environment . NewLine ,
151
132
string . Format ( Environment . NewLine + Environment . NewLine + "Public Property Let {0}(ByVal {1} As {2})" ,
@@ -160,13 +141,32 @@ private string GetPropertyText()
160
141
"End Property" ) ;
161
142
162
143
var propertyText = getterText +
163
- ( MustImplementLetSetterType ? letterText : string . Empty ) +
164
- ( MustImplementSetSetterType ? setterText : string . Empty ) ;
144
+ ( LetSetterTypeCheckBox . Checked ? letterText : string . Empty ) +
145
+ ( SetSetterTypeCheckBox . Checked ? setterText : string . Empty ) ;
165
146
166
147
var propertyTextLines = propertyText . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
167
148
return string . Join ( Environment . NewLine , _indenter . Indent ( propertyTextLines ) ) ;
168
149
}
169
150
151
+ private string GenerateGetter ( )
152
+ {
153
+ if ( SetSetterTypeCheckBox . Checked && LetSetterTypeCheckBox . Checked )
154
+ {
155
+ return string . Join ( Environment . NewLine ,
156
+ string . Format ( "Public Property Get {0}() As {1}" , NewPropertyName , TargetDeclaration . AsTypeName ) ,
157
+ string . Format ( " If VarType({0}) = vbObject Then" , TargetDeclaration . IdentifierName ) ,
158
+ string . Format ( " Set {0} = {1}" , NewPropertyName , TargetDeclaration . IdentifierName ) ,
159
+ " Else" ,
160
+ string . Format ( " {0} = {1}" , NewPropertyName , TargetDeclaration . IdentifierName ) ,
161
+ " End If" ,
162
+ "End Property" ) ;
163
+ }
164
+ return string . Join ( Environment . NewLine ,
165
+ string . Format ( "Public Property Get {0}() As {1}" , NewPropertyName , TargetDeclaration . AsTypeName ) ,
166
+ string . Format ( " {0}{1} = {2}" , SetSetterTypeCheckBox . Checked ? "Set " : string . Empty , NewPropertyName , TargetDeclaration . IdentifierName ) ,
167
+ "End Property" ) ;
168
+ }
169
+
170
170
private void ValidatePropertyName ( )
171
171
{
172
172
InvalidPropertyNameIcon . Visible = ValidateName ( NewPropertyName , ParameterName ) ||
0 commit comments