@@ -15,7 +15,7 @@ public abstract class AppCommandBarBase : IAppCommandBar
15
15
private readonly string _name ;
16
16
private readonly CommandBarPosition _position ;
17
17
private readonly IDictionary < ICommandMenuItem , ICommandBarControl > _items ;
18
- private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
18
+ protected static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
19
19
20
20
protected AppCommandBarBase ( string name , CommandBarPosition position , IEnumerable < ICommandMenuItem > items )
21
21
{
@@ -26,10 +26,18 @@ protected AppCommandBarBase(string name, CommandBarPosition position, IEnumerabl
26
26
27
27
protected ICommandMenuItem FindChildByTag ( string tag )
28
28
{
29
- var child = _items . FirstOrDefault ( kvp => kvp . Value != null && kvp . Value . Tag == tag ) ;
30
- return Equals ( child , default ( KeyValuePair < ICommandMenuItem , ICommandBarControl > ) )
31
- ? null
32
- : child . Key ;
29
+ try
30
+ {
31
+ var child = _items . FirstOrDefault ( kvp => kvp . Value != null && kvp . Value . Tag == tag ) ;
32
+ return Equals ( child , default ( KeyValuePair < ICommandMenuItem , ICommandBarControl > ) )
33
+ ? null
34
+ : child . Key ;
35
+ }
36
+ catch ( COMException exception )
37
+ {
38
+ Logger . Error ( exception , $ "COMException while finding child with tag '{ tag } '.") ;
39
+ }
40
+ return null ;
33
41
}
34
42
35
43
public void Localize ( )
@@ -39,34 +47,52 @@ public void Localize()
39
47
return ;
40
48
}
41
49
42
- foreach ( var kvp in _items . Where ( kv => kv . Key != null && kv . Value != null ) )
50
+ foreach ( var kvp in _items . Where ( kv => kv . Key != null && kv . Value != null && ! kv . Value . IsWrappingNullReference ) )
43
51
{
44
52
try
45
53
{
46
54
var item = kvp ;
47
- UiDispatcher . Invoke ( ( ) =>
48
- {
49
- item . Value . Caption = item . Key . Caption . Invoke ( ) ;
50
- item . Value . TooltipText = item . Key . ToolTipText . Invoke ( ) ;
51
- } ) ;
55
+ UiDispatcher . Invoke ( ( ) => LocalizeInternal ( item , kvp ) ) ;
52
56
53
57
}
54
58
catch ( Exception e )
55
59
{
56
- Logger . Error ( e , $ "Assignment of { kvp . Value . GetType ( ) . Name } .Caption or .TooltipText for { kvp . Key . GetType ( ) . Name } threw an exception .") ;
60
+ Logger . Error ( e , $ "Failed to dispatch assignment of { kvp . Value . GetType ( ) . Name } .Caption and .TooltipText for { kvp . Key . GetType ( ) . Name } to the UI thread .") ;
57
61
}
58
62
}
59
63
}
60
64
65
+ private static void LocalizeInternal ( KeyValuePair < ICommandMenuItem , ICommandBarControl > item , KeyValuePair < ICommandMenuItem , ICommandBarControl > kvp )
66
+ {
67
+ try
68
+ {
69
+ item . Value . Caption = item . Key . Caption . Invoke ( ) ;
70
+ item . Value . TooltipText = item . Key . ToolTipText . Invoke ( ) ;
71
+ }
72
+ catch ( Exception e )
73
+ {
74
+ Logger . Error ( e ,
75
+ $ "Assignment of { kvp . Value . GetType ( ) . Name } .Caption or .TooltipText for { kvp . Key . GetType ( ) . Name } threw an exception.") ;
76
+ }
77
+ }
78
+
61
79
public virtual void Initialize ( )
62
80
{
63
- if ( Parent == null )
81
+ if ( Parent == null || Parent . IsWrappingNullReference )
64
82
{
65
83
return ;
66
84
}
67
85
68
- Item = Parent . Add ( _name , _position ) ;
69
- Item . IsVisible = true ;
86
+ try
87
+ {
88
+ Item = Parent . Add ( _name , _position ) ;
89
+ Item . IsVisible = true ;
90
+ }
91
+ catch ( COMException exception )
92
+ {
93
+ Logger . Error ( exception , $ "Failed to add the command bar { _name } .") ;
94
+ return ;
95
+ }
70
96
foreach ( var item in _items . Keys . OrderBy ( item => item . DisplayOrder ) )
71
97
{
72
98
try
@@ -109,7 +135,7 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
109
135
110
136
public void EvaluateCanExecute ( RubberduckParserState state )
111
137
{
112
- foreach ( var kvp in _items . Where ( kv => kv . Key != null && kv . Value != null ) )
138
+ foreach ( var kvp in _items . Where ( kv => kv . Key != null && kv . Value != null && ! kv . Value . IsWrappingNullReference ) )
113
139
{
114
140
var commandItem = kvp . Key ;
115
141
var canExecute = false ;
@@ -122,10 +148,17 @@ public void EvaluateCanExecute(RubberduckParserState state)
122
148
{
123
149
Logger . Error ( e , $ "{ commandItem ? . GetType ( ) . Name ?? nameof ( ICommandMenuItem ) } .EvaluateCanExecute(RubberduckParserState) threw an exception.") ;
124
150
}
125
- kvp . Value . IsEnabled = canExecute ;
126
- if ( commandItem ? . HiddenWhenDisabled ?? false )
151
+ try
152
+ {
153
+ kvp . Value . IsEnabled = canExecute ;
154
+ if ( commandItem ? . HiddenWhenDisabled ?? false )
155
+ {
156
+ kvp . Value . IsVisible = canExecute ;
157
+ }
158
+ }
159
+ catch ( COMException exception )
127
160
{
128
- kvp . Value . IsVisible = canExecute ;
161
+ Logger . Error ( exception , $ "COMException while trying to set IsEnabled and IsVisible on { commandItem ? . GetType ( ) . Name ?? nameof ( ICommandMenuItem ) } " ) ;
129
162
}
130
163
}
131
164
}
@@ -135,11 +168,18 @@ public void EvaluateCanExecute(RubberduckParserState state)
135
168
136
169
public void RemoveCommandBar ( )
137
170
{
138
- Logger . Debug ( "Removing commandbar." ) ;
139
- RemoveChildren ( ) ;
140
- Item ? . Delete ( ) ;
141
- Item = null ;
142
- Parent = null ;
171
+ try
172
+ {
173
+ Logger . Debug ( "Removing commandbar." ) ;
174
+ RemoveChildren ( ) ;
175
+ Item ? . Delete ( ) ;
176
+ Item = null ;
177
+ Parent = null ;
178
+ }
179
+ catch ( COMException exception )
180
+ {
181
+ Logger . Error ( exception , "COM exception while trying to delee the commandbar" ) ;
182
+ }
143
183
}
144
184
145
185
private void RemoveChildren ( )
@@ -169,7 +209,16 @@ private void RemoveChildren()
169
209
170
210
private void child_Click ( object sender , CommandBarButtonClickEventArgs e )
171
211
{
172
- var item = _items . Select ( kvp => kvp . Key ) . SingleOrDefault ( menu => menu . GetType ( ) . FullName == e . Control . Tag ) ;
212
+ ICommandMenuItem item ;
213
+ try
214
+ {
215
+ item = _items . Select ( kvp => kvp . Key ) . SingleOrDefault ( menu => menu . GetType ( ) . FullName == e . Control . Tag ) ;
216
+ }
217
+ catch ( COMException exception )
218
+ {
219
+ Logger . Error ( exception , "COM exception finding command for a control." ) ;
220
+ item = null ;
221
+ }
173
222
if ( item == null )
174
223
{
175
224
return ;
0 commit comments