comparison jface/Librarian.d @ 72:fc7a8f537871

Open/Save with experimental runner factory.
author Frank Benoit <benoit@tionex.de>
date Sun, 13 Apr 2008 17:19:38 +0200
parents 63a7769cce57
children 9ff9b8f7284b
comparison
equal deleted inserted replaced
71:63a7769cce57 72:fc7a8f537871
337 enableActions(false); 337 enableActions(false);
338 338
339 library = new Library(); 339 library = new Library();
340 try { 340 try {
341 // Launch the Open runnable 341 // Launch the Open runnable
342 ModalContext.run(new class(fileName) IRunnableWithProgress { 342 ModalContext.run( dgIRunnableWithProgress( &internalOpen, fileName ),
343 String fileName_; 343 true, getStatusLineManager().getProgressMonitor(), getShell().getDisplay());
344 this(String a){
345 fileName_=a;
346 }
347 public void run(IProgressMonitor progressMonitor) {
348 try {
349 progressMonitor.beginTask("Loading", IProgressMonitor.UNKNOWN);
350 library.load(fileName_);
351 progressMonitor.done();
352 viewer.setInput(library);
353 refreshView();
354 } catch (IOException e) {
355 showError( Format("Can't load file {}\r{}", fileName_, e.msg));
356 }
357 }
358 }, true, getStatusLineManager().getProgressMonitor(), getShell()
359 .getDisplay());
360 } catch (InterruptedException e) { 344 } catch (InterruptedException e) {
361 } catch (InvocationTargetException e) { 345 } catch (InvocationTargetException e) {
362 } finally { 346 } finally {
363 // Enable actions 347 // Enable actions
364 enableActions(true); 348 enableActions(true);
365 } 349 }
366 } 350 }
367 } 351 }
368 352 private void internalOpen( IProgressMonitor progressMonitor,String filename ){
353 try {
354 progressMonitor.beginTask("Loading", IProgressMonitor.UNKNOWN);
355 library.load(filename);
356 progressMonitor.done();
357 viewer.setInput(library);
358 refreshView();
359 } catch (IOException e) {
360 showError( Format("Can't load file {}\r{}", filename, e.msg));
361 }
362 }
369 /** 363 /**
370 * Creates a new file 364 * Creates a new file
371 */ 365 */
372 public void newFile() { 366 public void newFile() {
373 if (checkOverwrite()) { 367 if (checkOverwrite()) {
374 library = new Library(); 368 library = new Library();
375 viewer.setInput(library); 369 viewer.setInput(library);
376 } 370 }
377 } 371 }
378 372
379 /** 373 /**
380 * Saves the current file 374 * Saves the current file
381 */ 375 */
382 public void saveFile() { 376 public void saveFile() {
383 String fileName = library.getFileName(); 377 String fileName = library.getFileName();
384 if (fileName is null) { 378 if (fileName is null) {
385 fileName = (new SafeSaveDialog(getShell())).open(); 379 fileName = (new SafeSaveDialog(getShell())).open();
380 }
381 if (fileName is null) {
382 return;
386 } 383 }
387 saveFileAs(fileName); 384 saveFileAs(fileName);
388 } 385 }
389 386
390 /** 387 /**
396 // Disable the actions, so user can't change file while it's saving 393 // Disable the actions, so user can't change file while it's saving
397 enableActions(false); 394 enableActions(false);
398 try { 395 try {
399 auto pm = getStatusLineManager().getProgressMonitor(); 396 auto pm = getStatusLineManager().getProgressMonitor();
400 auto disp = getShell().getDisplay(); 397 auto disp = getShell().getDisplay();
401
402 // Launch the Save runnable 398 // Launch the Save runnable
403 ModalContext.run(new class(fileName) IRunnableWithProgress { 399 ModalContext.run( dgIRunnableWithProgress( &internalSave, fileName ), true, pm, disp);
404 String filename_; 400
405 this(String a){
406 filename_=a;
407 }
408 public void run(IProgressMonitor progressMonitor) {
409 try {
410 progressMonitor.beginTask("Saving", -1/+IProgressMonitor.UNKNOWN+/);
411 library.save(filename_);
412 progressMonitor.done();
413 } catch (IOException e) {
414 showError(Format("Can't save file {}\r{}", library.getFileName(), e.msg ));
415 }
416 }
417 }, true, pm, disp );
418 } catch (InterruptedException e) { 401 } catch (InterruptedException e) {
419 } catch (InvocationTargetException e) { 402 } catch (InvocationTargetException e) {
420 } finally { 403 } finally {
421 // Enable the actions 404 // Enable the actions
422 enableActions(true); 405 enableActions(true);
423 } 406 }
424 } 407 }
425 408
426 /** 409 private void internalSave(IProgressMonitor progressMonitor,String filename ){
427 * Shows an error 410 try {
428 * 411 progressMonitor.beginTask("Saving", IProgressMonitor.UNKNOWN );
429 * @param msg the error 412 library.save(filename);
430 */ 413 progressMonitor.done();
414 } catch (IOException e) {
415 showError(Format("Can't save file {}\r{}", library.getFileName(), e.msg ));
416 }
417 }
418 /**
419 * Shows an error
420 *
421 * @param msg the error
422 */
431 public void showError(String msg) { 423 public void showError(String msg) {
432 MessageDialog.openError(getShell(), "Error", msg); 424 MessageDialog.openError(getShell(), "Error", msg);
433 } 425 }
434 426
435 /** 427 /**
587 */ 579 */
588 public void run() { 580 public void run() {
589 SafeSaveDialog dlg = new SafeSaveDialog(Librarian.getApp().getShell()); 581 SafeSaveDialog dlg = new SafeSaveDialog(Librarian.getApp().getShell());
590 String fileName = dlg.open(); 582 String fileName = dlg.open();
591 if (fileName !is null) { 583 if (fileName !is null) {
592 Librarian.getApp().saveFileAs(fileName); 584 Librarian.getApp().saveFileAs(fileName);
593 } 585 }
594 } 586 }
595 } 587 }
596 588
597 589