SWT - Tweaking my ProgressMonitorDialog

旧城冷巷雨未停 提交于 2019-12-12 04:58:57

问题


I have a working ProgressMonitorDialog, but I want to make sure that I am setting it up correctly.

First the Code:

Method to create Dialog

 public void startProgressBar() {
  try {
     new ProgressMonitorDialog(getShell()).run(true, true,
        new ProgressBarThread());
  } 
  catch (InvocationTargetException e) {
     MessageDialog.openError(getShell(), "Error", e.getMessage());
  } 
  catch (InterruptedException e) {
     MessageDialog.openInformation(getShell(), "Cancelled", e.getMessage());
  }
}

Class File

 class ProgressBarThread implements IRunnableWithProgress {
  private static final int TOTAL_TIME = 1000;

  public ProgressBarThread() {

  }

  public void run(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException {
     monitor.beginTask("Creating PDF File(s): Please wait.....", IProgressMonitor.UNKNOWN);
     for (int total = 0; total < TOTAL_TIME ; total++) {
        Thread.sleep(total);
        monitor.worked(total);
        if (total == TOTAL_TIME / 2) monitor.subTask("Please be patient... Operation should finish soon.");
    }
    monitor.done();

  }
}

Method that calls the ProgressBar and runs a Pdf file creation Operation

private void startSavePdfOperation() {
  Display.getDefault().asyncExec(new Runnable() {
     public void run() {
        startProgressBar();
     }
  });
  saveOp = new AplotSaveOperation(appReg.getString("aplot.message.SAVETOPDF"), "PDF", session);
  saveOp.addOperationListener(new MyOperationListener(this) {

     public void endOperationImpl() {
        java.io.File zipFile = null;
        try {               
           AplotSaveResultsParser.SaveResult saveResults = saveOp.getSaveResults();
           if (saveResults != null) {
           ETC.....   ETC......  

Questions:

  1. Being the ProgressMonitorDialog is a GUI, it needs to be executed in a Display.getDefault().asyncExec?

  2. If the ProgressMonitorDialog is running in a separate thread, how does it know to close when the operation is finsihed?

  3. Is there any relationship between the progressbar and the operation?

  4. I am correct in assuming that the for loop in the ProgressBarThread class is basically the timer that keeps the monitor open?

  5. Is there a way to increase the speed of the ProgressMonitorDialog's indicator, also can you remove the cancel button?

This is what I am thinking is happening currently.

  1. I am starting the progress bar just before I start the PDF Operation Listener See startSavePdfOperation() Above

  2. The progress bar is running as unknown, but using a for loop to keep the progress bar dialog open, while the operation is running on a thread in the background.
    See Class ProgressBarThread above

  3. When the PDF operation completes the listener operation class closes the base GUI dialog.

    public void endOperation() {
     try {
        endOperationImpl();
     }
     finally {
        Display.getDefault().asyncExec(new Runnable() {
           public void run() {
              w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_ARROW));
              w.recursiveSetEnabled(getShell(), true);
              w.getShell().setEnabled(!getShell().getEnabled());
              w.close();
           }
        });
     }
    

    }

  4. I am not sure what is happening to the ProgressBarThread monitor?

Is this Possible?

  1. When the PDF Operation starts, the ProgressMonitorDialog opens and starts the indicator. OK with keeping it unknown.

  2. When the PDF Operation completes, the monitor closes, then the base Dialog

I am just wanting to open progress bar dialog that will inform the user that their request is working in the background.

As stated the above code works, but I am afraid by letting the closing of Base GUI, destroy my Progress Thread and Monitor is not good practice.


回答1:


First of all, in your ProgressBarThread#run() you should use monitor.worked(1). You don't need to set the total worked but increment it by the amount of work done, since the last time it was called.

Q1. Yes it needs to be executed in the display thread Q2. Normally the work that needs to be done is actually performed in the runnable that is passed to the progress monitor dialog so that you can accurately report the amount of progress made. So your operation (if it is a synchronous call) should be called from within ProgressBarThread#run() so that you call monitor.worked(1) only when one file processing is complete.

Q3. What kind of operation are you running, perhaps it already supports showing progress bar, and you just need to invoke the right API. Is it an IUndoableOperation?

Q4. As I said this approach is problematic because you can never accurately report the progress and close the dialog only when the operation is completed. But if this is the only choice you have, then you can just save the monitor reference somewhere so that it is accessible to the other thread. Once monitor.done() is called, your ProgressBarThread#run() should return, the dialog will close.

Q5. You can remove the cancel button by passing the correct parameter to ProgressMonitorDialog#run(..):

new ProgressMonitorDialog(getShell()).run(true, false, new ProgressBarThread());

For the rest of the questions I can better answer if I know what kind of operation (what API) you are using.



来源:https://stackoverflow.com/questions/13957562/swt-tweaking-my-progressmonitordialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!