Eclipse平臺中“進度條對話框”(ProgressMonitorDialog)的簡單實現

運行環境:Eclipse 3.0.x

下面是的方法實現了一個簡單的“進度條對話框”:

private byte[] progressMonitorDialogTest(final String bhContent, final String root, final String userName,   final String pwd, final String payload) throws Exception {
  final byte[][] result = new byte[1][];
  IRunnableWithProgress pro = new IRunnableWithProgress () {

    //實現接口中的execute方法:
    protected void execute(IProgressMonitor monitor) throws CoreException {

       //具體的業務邏輯:
       result[0] = invokeDLL(bhContent, root, userName, pwd, payload);
    }
   

   //實現接口中的run方法,該方法是一個同步方法:
   public synchronized final void run(IProgressMonitor monitor)
     throws InvocationTargetException, InterruptedException {
    try {

     //總的工作量
     int totalWork = IProgressMonitor.UNKNOWN;
     monitor.beginTask("A Progress monitor dialog example...", totalWork);
     if (result[0] == null)
      totalWork = 400;
     monitor.worked(totalWork);

     //執行業務邏輯:
     execute(monitor);
     if (result[0] != null) {
      totalWork = 1000;
     }
     else {
      totalWork = 700;
     }
     monitor.worked(totalWork);
     
    }
    catch (CoreException e) {
     throw new InvocationTargetException(e);
    }
    catch (OperationCanceledException e) {
     throw new InterruptedException(e.getMessage());
    }
    finally {
     monitor.done();
    }
   }
  };
  
  try {

   //調用:
   new ProgressMonitorDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell()).run(true, false, pro);
  }
  catch (Exception e) {
   e.printStackTrace();
  }
  
  return result[0];
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章