@@ -162,6 +162,7 @@ public void ToggleRename(ControlDescriptionViewModel targetControl)
162
162
/// <param name="controlToRename"></param>
163
163
public async Task RenameControl ( ActionExecutionContext context , ControlDescriptionViewModel controlToRename )
164
164
{
165
+ string currentName = controlToRename . ControlName ;
165
166
try
166
167
{
167
168
var keyArgs = context . EventArgs as KeyEventArgs ;
@@ -180,7 +181,9 @@ public async Task RenameControl(ActionExecutionContext context, ControlDescripti
180
181
catch ( Exception ex )
181
182
{
182
183
logger . Error ( ex , ex . Message ) ;
184
+ controlToRename . ControlName = currentName ;
183
185
CanEdit = false ;
186
+ MessageBox . Show ( ex . Message , $ "Error while renaming control : { controlToRename . ControlName } ") ;
184
187
}
185
188
}
186
189
@@ -242,7 +245,11 @@ private IEnumerable<ControlDescriptionViewModel> LoadControlDetails(ApplicationD
242
245
var controls = this . applicationDataManager . GetControlsForScreen ( applicationDescriptionViewModel . Model , screenName ) . ToList ( ) ;
243
246
foreach ( var control in controls )
244
247
{
245
- var controlDescriptionViewModel = new ControlDescriptionViewModel ( control ) ;
248
+ if ( control . IsDeleted )
249
+ {
250
+ continue ;
251
+ }
252
+ var controlDescriptionViewModel = new ControlDescriptionViewModel ( control ) ;
246
253
applicationDescriptionViewModel . AddControl ( controlDescriptionViewModel , screenName ) ;
247
254
controlsList . Add ( controlDescriptionViewModel ) ;
248
255
}
@@ -276,21 +283,29 @@ public async Task MoveToScreen(ControlDescriptionViewModel controlDescription)
276
283
/// <returns></returns>
277
284
public async Task ConfigureControlAsync ( ControlDescriptionViewModel controlToEdit )
278
285
{
279
- Guard . Argument ( controlToEdit ) . NotNull ( ) ;
280
-
281
- //Make a copy of ControlDescription that is opened for edit
282
- var copyOfControlToEdit = controlToEdit . ControlDescription . Clone ( ) as ControlDescription ;
283
- copyOfControlToEdit . ControlId = controlToEdit . ControlId ;
284
- var controlEditor = controlEditorFactory . CreateControlEditor ( controlToEdit . ControlDetails ) ;
285
- controlEditor . Initialize ( copyOfControlToEdit ) ;
286
- var result = await windowManager . ShowDialogAsync ( controlEditor ) ;
287
- //if save was clicked, assign back changes in ControlDetails to controlToEdit.
288
- //Editor only allows editing ControlDetails. Description won't be modified.
289
- if ( result . HasValue && result . Value )
286
+ try
290
287
{
291
- controlToEdit . ControlDetails = copyOfControlToEdit . ControlDetails ;
292
- await SaveControlDetails ( controlToEdit , false ) ;
293
- await this . eventAggregator . PublishOnBackgroundThreadAsync ( new ControlUpdatedEventArgs ( controlToEdit . ControlId ) ) ;
288
+ Guard . Argument ( controlToEdit ) . NotNull ( ) ;
289
+
290
+ //Make a copy of ControlDescription that is opened for edit
291
+ var copyOfControlToEdit = controlToEdit . ControlDescription . Clone ( ) as ControlDescription ;
292
+ copyOfControlToEdit . ControlId = controlToEdit . ControlId ;
293
+ var controlEditor = controlEditorFactory . CreateControlEditor ( controlToEdit . ControlDetails ) ;
294
+ controlEditor . Initialize ( copyOfControlToEdit ) ;
295
+ var result = await windowManager . ShowDialogAsync ( controlEditor ) ;
296
+ //if save was clicked, assign back changes in ControlDetails to controlToEdit.
297
+ //Editor only allows editing ControlDetails. Description won't be modified.
298
+ if ( result . HasValue && result . Value )
299
+ {
300
+ controlToEdit . ControlDetails = copyOfControlToEdit . ControlDetails ;
301
+ await SaveControlDetails ( controlToEdit , false ) ;
302
+ await this . eventAggregator . PublishOnBackgroundThreadAsync ( new ControlUpdatedEventArgs ( controlToEdit . ControlId ) ) ;
303
+ }
304
+ }
305
+ catch ( Exception ex )
306
+ {
307
+ logger . Error ( ex , ex . Message ) ;
308
+ MessageBox . Show ( ex . Message , $ "Error while configuring control : { controlToEdit . ControlName } ") ;
294
309
}
295
310
}
296
311
@@ -301,9 +316,43 @@ public async Task ConfigureControlAsync(ControlDescriptionViewModel controlToEdi
301
316
/// <returns></returns>
302
317
public async Task EditControlAsync ( ControlDescriptionViewModel controlToEdit )
303
318
{
304
- await this . eventAggregator . PublishOnUIThreadAsync ( new PropertyGridObjectEventArgs ( controlToEdit , ( ) => { _ = SaveControlDetails ( controlToEdit , false ) ; } , ( ) => { return true ; } ) ) ;
319
+ await this . eventAggregator . PublishOnUIThreadAsync ( new PropertyGridObjectEventArgs ( controlToEdit ,
320
+ async ( ) => {
321
+ try
322
+ {
323
+ await SaveControlDetails ( controlToEdit , false ) ;
324
+ }
325
+ catch ( Exception ex )
326
+ {
327
+ logger . Error ( ex , "There was an error while trying to edit control : {0}" , controlToEdit . ControlName ) ;
328
+ MessageBox . Show ( ex . Message , $ "Error while editing control : { controlToEdit . ControlName } ") ;
329
+ }
330
+ } ,
331
+ ( ) => {
332
+ return true ;
333
+ } ) ) ;
305
334
}
306
335
336
+ /// <summary>
337
+ /// Delete the control
338
+ /// </summary>
339
+ /// <param name="controlToDelete"></param>
340
+ public async Task DeleteControlAsync ( ControlDescriptionViewModel controlToDelete )
341
+ {
342
+ try
343
+ {
344
+ Guard . Argument ( controlToDelete , nameof ( controlToDelete ) ) . NotNull ( ) ;
345
+ await this . applicationDataManager . DeleteControlAsync ( controlToDelete . ControlDescription ) ;
346
+ this . Controls . Remove ( controlToDelete ) ;
347
+ }
348
+ catch ( Exception ex )
349
+ {
350
+ logger . Error ( ex , "There was an error while trying to delete control : {0}" , controlToDelete . ControlName ) ;
351
+ MessageBox . Show ( ex . Message , $ "Error while deleting control : { controlToDelete . ControlName } ") ;
352
+ }
353
+ }
354
+
355
+
307
356
/// <summary>
308
357
/// Show file browse dialog and let user pick a new image for the control.
309
358
/// Existing image will be deleted and replaced with the new image picked by user.
@@ -312,25 +361,33 @@ public async Task EditControlAsync(ControlDescriptionViewModel controlToEdit)
312
361
/// <returns></returns>
313
362
public async Task ChangeImageFromExistingAsync ( ControlDescriptionViewModel selectedControl )
314
363
{
315
- OpenFileDialog openFileDialog = new OpenFileDialog ( ) ;
316
- openFileDialog . Filter = "PNG File (*.Png)|*.Png" ;
317
- openFileDialog . InitialDirectory = Environment . CurrentDirectory ;
318
- if ( openFileDialog . ShowDialog ( ) == true )
364
+ try
319
365
{
320
- string fileName = openFileDialog . FileName ;
321
- File . Delete ( selectedControl . ControlImage ) ;
322
- using ( FileStream fs = new FileStream ( fileName , FileMode . Open , FileAccess . Read ) )
366
+ OpenFileDialog openFileDialog = new OpenFileDialog ( ) ;
367
+ openFileDialog . Filter = "PNG File (*.Png)|*.Png" ;
368
+ openFileDialog . InitialDirectory = Environment . CurrentDirectory ;
369
+ if ( openFileDialog . ShowDialog ( ) == true )
323
370
{
324
- //we can't reuse the same control image name due to caching issues with bitmap which will keep using old file
325
- //unless file name changes
326
- selectedControl . ControlImage = await this . applicationDataManager . AddOrUpdateControlImageAsync ( selectedControl . ControlDescription , fs ) ;
327
- await SaveControlDetails ( selectedControl , false ) ;
328
- // This will force reload image on control explorer
329
- selectedControl . ImageSource = null ;
330
- // This will force reload image on process designer
331
- await this . eventAggregator . PublishOnBackgroundThreadAsync ( new ControlUpdatedEventArgs ( selectedControl . ControlId ) ) ;
371
+ string fileName = openFileDialog . FileName ;
372
+ using ( FileStream fs = new FileStream ( fileName , FileMode . Open , FileAccess . Read ) )
373
+ {
374
+ //we can't reuse the same control image name due to caching issues with bitmap which will keep using old file
375
+ //unless file name changes
376
+ selectedControl . ControlImage = await this . applicationDataManager . AddOrUpdateControlImageAsync ( selectedControl . ControlDescription , fs ) ;
377
+ await SaveControlDetails ( selectedControl , false ) ;
378
+ // This will force reload image on control explorer
379
+ selectedControl . ImageSource = null ;
380
+ // This will force reload image on process designer
381
+ await this . eventAggregator . PublishOnBackgroundThreadAsync ( new ControlUpdatedEventArgs ( selectedControl . ControlId ) ) ;
382
+ }
383
+ File . Delete ( selectedControl . ControlImage ) ;
332
384
}
333
385
}
386
+ catch ( Exception ex )
387
+ {
388
+ logger . Error ( ex , "There was an error while trying to change image for control : {0}" , selectedControl . ControlName ) ;
389
+ MessageBox . Show ( ex . Message , $ "Error while changing image for control : { selectedControl . ControlName } ") ;
390
+ }
334
391
}
335
392
336
393
/// <summary>
@@ -353,7 +410,8 @@ public async Task CloneControl(ControlDescriptionViewModel controlToClone)
353
410
}
354
411
catch ( Exception ex )
355
412
{
356
- logger . Error ( ex , "There was an error while trying to clone the control." ) ;
413
+ logger . Error ( ex , "There was an error while trying to clone control : {0}" , selectedControl . ControlName ) ;
414
+ MessageBox . Show ( ex . Message , $ "Error while cloning control : { selectedControl . ControlName } ") ;
357
415
}
358
416
}
359
417
@@ -363,18 +421,26 @@ public async Task CloneControl(ControlDescriptionViewModel controlToClone)
363
421
/// <param name="controlToRename"></param>
364
422
public async Task CreateRevision ( ControlDescriptionViewModel control )
365
423
{
366
- Guard . Argument ( control ) . NotNull ( ) ;
367
-
368
- var clonedControl = control . ControlDescription . Clone ( ) as ControlDescription ;
369
- clonedControl . ControlId = control . ControlId ;
370
- clonedControl . Version = new Version ( control . Version . Major + 1 , 0 ) ;
371
- var controlDescriptionViewModel = new ControlDescriptionViewModel ( clonedControl ) ;
372
- await SaveBitMapSource ( controlDescriptionViewModel . ControlDescription , controlDescriptionViewModel . ImageSource ) ;
373
- await SaveControlDetails ( controlDescriptionViewModel , true ) ;
374
- //we remove the visible version and add the new revised version in explorer.
375
- this . Controls . Remove ( control ) ;
376
- this . Controls . Add ( controlDescriptionViewModel ) ;
377
- logger . Information ( "Created a new revision for control : {0}" , control . ControlDescription ) ;
424
+ try
425
+ {
426
+ Guard . Argument ( control ) . NotNull ( ) ;
427
+
428
+ var clonedControl = control . ControlDescription . Clone ( ) as ControlDescription ;
429
+ clonedControl . ControlId = control . ControlId ;
430
+ clonedControl . Version = new Version ( control . Version . Major + 1 , 0 ) ;
431
+ var controlDescriptionViewModel = new ControlDescriptionViewModel ( clonedControl ) ;
432
+ await SaveBitMapSource ( controlDescriptionViewModel . ControlDescription , controlDescriptionViewModel . ImageSource ) ;
433
+ await SaveControlDetails ( controlDescriptionViewModel , true ) ;
434
+ //we remove the visible version and add the new revised version in explorer.
435
+ this . Controls . Remove ( control ) ;
436
+ this . Controls . Add ( controlDescriptionViewModel ) ;
437
+ logger . Information ( "Created a new revision for control : {0}" , control . ControlDescription ) ;
438
+ }
439
+ catch ( Exception ex )
440
+ {
441
+ logger . Error ( ex , "There was an error while creating revision of control : {0}" , selectedControl . ControlName ) ;
442
+ MessageBox . Show ( ex . Message , $ "Error while creating revision for control : { selectedControl . ControlName } ") ;
443
+ }
378
444
}
379
445
380
446
private readonly object locker = new object ( ) ;
0 commit comments