SafeRunner.java :  » Project-Management » Activity-Manager » jfb » tools » activitymgr » ui » util » Java Open Source

Java Open Source » Project Management » Activity Manager 
Activity Manager » jfb » tools » activitymgr » ui » util » SafeRunner.java
/*
 * Copyright (c) 2004-2006, Jean-Franois Brazeau. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 * 
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package jfb.tools.activitymgr.ui.util;

import jfb.tools.activitymgr.AbstractException;
import jfb.tools.activitymgr.ui.dialogs.ErrorDialog;

import org.apache.log4j.Logger;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Shell;

/**
 * Offre un contexte d'excution scuris.
 * 
 * <p>Si une exception est leve dans le traitement, elle est attrape
 * et un popup d'erreur est affich.</p>
 * 
 * <p>Exemple d'utilisation :<br>
 * <pre>
 * // Initialisation du contexte d'excution scuris
 * SafeRunner safeRunner = new SafeRunner() {
 * &nbsp;&nbsp;public Object runUnsafe() throws Exception {
 * &nbsp;&nbsp;&nbsp;&nbsp;// Declare unsafe code...
 * &nbsp;&nbsp;&nbsp;&nbsp;return result;
 * &nbsp;&nbsp;}
 * };
 * // Excution du traitement
 * Object result = safeRunner.run(parent.getShell(), "");
 * </pre>
 */
public abstract class SafeRunner {

  /** Logger */
  private static Logger log = Logger.getLogger(SafeRunner.class);

  /**
   * Classe permettant de stoker le rsultat du traitement.
   * (sans cet objet il n'est pas possible de rcuprer le rsultat
   * dans le traitement excut dans le Runnable puisqu'il faut
   * passer par une rfrence finale).
   */
  private static class Result {
    public Object value;
  }
  
  /**
   * Lance le traitement dans le contexte scuris.
   * @param parentShell shell parent (peut tre nul).
   * @return le rsultat du traitement.
   */
  public Object run(Shell parentShell) {
    return run(parentShell, null);
  }

  /**
   * Lance le traitement dans le contexte scuris.
   * @param parentShell shell parent (peut tre nul).
   * @param defaultValue la valeur  retourner par dfaut.
   * @return le rsultat du traitement.
   */
// TODO Supprimer ce bout de code si il n'y a pas de pb
//  public Object run(Shell parentShell, Object defaultValue) {
//    log.debug("ParentShell : " + parentShell);
//    Object result = defaultValue;
//    // Changement du curseur
//    Cursor waitCursor = parentShell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT);
//    parentShell.setCursor(waitCursor);
//    // Excution du traitement
//    try { result = runUnsafe(); }
//    catch (AbstractException e) {
//      log.info("UI Exception", e);
//      Shell parent = Display.getCurrent().getActiveShell();
//      new ErrorDialog(parent, "Unable to complete operation : '" + e.getMessage() + "'", e).open();
//    }
//    catch (Throwable t) {
//      log.error("Unexpected error", t);
//      Shell parent = Display.getCurrent().getActiveShell();
//      new ErrorDialog(parent, "Unexpected error", t).open();
//    }
//    finally {
//      // Retour du curseur normal
//      Cursor normalCursor = parentShell.getDisplay().getSystemCursor(SWT.CURSOR_ARROW);
//      parentShell.setCursor(normalCursor);
//    }
//    // Retour du rsultat
//    log.debug(" -> result='" + result + "'");
//    return result;
//  }
  public Object run(final Shell parentShell, Object defaultValue) {
    log.debug("ParentShell : " + parentShell);
    final Result result = new Result();
    result.value = defaultValue;
    // Excution du traitement
    BusyIndicator.showWhile(parentShell.getDisplay(), new Runnable() {
      public void run() {
        try { result.value = runUnsafe(); }
        catch (AbstractException e) {
          log.info("UI Exception", e);
          new ErrorDialog(parentShell, "Unable to complete operation : '" + e.getMessage() + "'", e).open();
        }
        catch (Throwable t) {
          log.error("Unexpected error", t);
          new ErrorDialog(parentShell, "Unexpected error", t).open();
        }
      }
    });
    // Retour du rsultat
    log.debug(" -> result='" + result.value + "'");
    return result.value;
  }

  /**
   * Traitement potentiellement  risque.
   * 
   * <p>Cette mthode doit tre implmente.</p>
   * 
   * @return le rsultat du traitement.
   * @throws Exception le traitement peut potentiellement lever n'importe
   *     quelle exception.
   */
  protected abstract Object runUnsafe() throws Exception;

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.