#5297 Integrate remoting monitor cancel functionality in ui
authorCherian Mathew <c.mathew@bgbm.org>
Wed, 28 Oct 2015 14:19:52 +0000 (15:19 +0100)
committerCherian Mathew <c.mathew@bgbm.org>
Wed, 28 Oct 2015 14:23:00 +0000 (15:23 +0100)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/wizard/ClassificationChooserWizardPage.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/AbstractUtility.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/CdmProgressMonitorAdapter.java
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/service/ProgressMonitorServiceTest.java

index da35311..01e47b4 100644 (file)
@@ -29,6 +29,7 @@ import org.eclipse.swt.widgets.Text;
 import org.eclipse.wb.swt.ResourceManager;
 
 import eu.etaxonomy.cdm.model.taxon.Classification;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
 
 /**
@@ -105,38 +106,39 @@ public class ClassificationChooserWizardPage extends WizardPage implements Liste
                btnClear.setImage(ResourceManager.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
                btnClear.addListener(SWT.Selection, this);
 
-               //report
-               Label labelReportFile = new Label(composite, SWT.NONE);
-        labelReportFile.setText("Report File");
+               if(!CdmStore.getCurrentSessionManager().isRemoting()) {
+                   //report
+                   Label labelReportFile = new Label(composite, SWT.NONE);
+                   labelReportFile.setText("Report File");
 
-        fileDialogReport = new FileDialog(parent.getShell());
+                   fileDialogReport = new FileDialog(parent.getShell());
 
-        fileDialogReport.setFilterExtensions(new String[]{"*.*"});
+                   fileDialogReport.setFilterExtensions(new String[]{"*.*"});
 
-        textFileReport = new Text(composite, SWT.BORDER);
-        textFileReport.setEditable(false);
-        textFileReport.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
+                   textFileReport = new Text(composite, SWT.BORDER);
+                   textFileReport.setEditable(false);
+                   textFileReport.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
 
 
-        Button buttonReport = new Button(composite, SWT.PUSH);
-        buttonReport.setText("Browse...");
+                   Button buttonReport = new Button(composite, SWT.PUSH);
+                   buttonReport.setText("Browse...");
 
-        buttonReport.addSelectionListener(new SelectionAdapter(){
+                   buttonReport.addSelectionListener(new SelectionAdapter(){
 
-            /* (non-Javadoc)
-             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
-             */
-            @Override
-            public void widgetSelected(SelectionEvent e) {
-                String path = fileDialogReport.open();
-                if(path!=null){
-                    textFileReport.setText(path);
-                    setPageComplete(true);
-                }
-            }
-
-        });
+                       /* (non-Javadoc)
+                        * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
+                        */
+                       @Override
+                       public void widgetSelected(SelectionEvent e) {
+                           String path = fileDialogReport.open();
+                           if(path!=null){
+                               textFileReport.setText(path);
+                               setPageComplete(true);
+                           }
+                       }
 
+                   });
+               }
                setControl(composite);
        }
 
@@ -165,6 +167,9 @@ public class ClassificationChooserWizardPage extends WizardPage implements Liste
     }
 
     public URI getReportUri(){
+        if(textFileReport == null) {
+            return null;
+        }
         String text = textFileReport.getText();
         if(text==null || text.isEmpty()){
             return null;
index 1bc0a68..cdfbe19 100644 (file)
@@ -34,8 +34,8 @@ import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubProgressMonitor;
 import org.eclipse.core.runtime.jobs.ISchedulingRule;
+import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.jface.action.IStatusLineManager;
-import org.eclipse.jface.dialogs.ProgressMonitorDialog;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.resource.ColorRegistry;
 import org.eclipse.jface.resource.FontRegistry;
@@ -55,6 +55,7 @@ import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.handlers.IHandlerService;
 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
 import org.eclipse.ui.part.EditorPart;
+import org.eclipse.ui.progress.IProgressConstants;
 import org.eclipse.ui.progress.IProgressService;
 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
 import org.eclipse.ui.themes.ITheme;
@@ -430,13 +431,18 @@ public abstract class AbstractUtility {
                 throw new IllegalStateException("Remoting progress monitor is null");
             }
 
-            final ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(getShell());
-            IRunnableWithProgress runnable = new IRunnableWithProgress() {
+            Job job = new Job(label) {
+
 
                 @Override
-                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
-                    // run the monitor untilthe operation is finished
-                    IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor(label, uuid, pollInterval, postOp, monitor);
+                public IStatus run(IProgressMonitor monitor) {
+                    // run the monitor until the operation is finished
+                    IRemotingProgressMonitor remotingMonitor;
+                    try {
+                        remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor(label, uuid, pollInterval, postOp, monitor);
+                    } catch (InterruptedException ie) {
+                        return new Status(Status.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Operation Interrupted", ie);
+                    }
                     final StringBuilder reportSb = new StringBuilder();
                     // collect reports
                     for(String report : remotingMonitor.getReports()) {
@@ -447,17 +453,37 @@ public abstract class AbstractUtility {
                             @Override
                             public void run() {
                                 // display reports with possibility to save
-                                ReportTextDialog dialog = new ReportTextDialog(progressDialog.getShell());
+                                ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                                 dialog.setTitle(label + " Report");
                                 dialog.setReportText(reportSb.toString());
                                 dialog.open();
                             }
                         });
                     }
+                    return Status.OK_STATUS;
+                }
+
+                @Override
+                protected void canceling() {
+                    CdmStore.getCurrentApplicationConfiguration().getProgressMonitorService().cancel(uuid);
                 }
             };
 
-            progressDialog.run(true, cancelable, runnable);
+//            job.addJobChangeListener(new JobChangeAdapter() {
+//                @Override
+//                public void done(IJobChangeEvent event) {
+//                    if(event.getJob().) {
+//                        logger.warn("in jobc change listener");
+//                    }
+//                }
+//            });
+
+            // configure the job
+            job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
+            job.setUser(true);
+            // schedule job
+            job.schedule();
+
         } catch (Exception e) {
             MessagingUtils.errorDialog("Error executing operation",
                     AbstractUtility.class,
index f9e9940..9edcdf7 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * 
+ *
  */
 package eu.etaxonomy.taxeditor.model;
 
@@ -16,17 +16,17 @@ import eu.etaxonomy.taxeditor.store.StoreUtil;
  * @version 1.0
  */
 public class CdmProgressMonitorAdapter implements eu.etaxonomy.cdm.common.monitor.IProgressMonitor {
-       
+
        private IProgressMonitor progressMonitor;
 
        private CdmProgressMonitorAdapter (IProgressMonitor monitor){
                if(monitor == null){
                        monitor = new NullProgressMonitor();
                }
-               
+
                this.progressMonitor = monitor;
        }
-       
+
        /**
         * <p>CreateMonitor</p>
         *
@@ -36,7 +36,7 @@ public class CdmProgressMonitorAdapter implements eu.etaxonomy.cdm.common.monito
        public static CdmProgressMonitorAdapter CreateMonitor (IProgressMonitor monitor){
                return new CdmProgressMonitorAdapter(monitor);
        }
-       
+
        /**
         * <p>CreateSubMonitor</p>
         *
@@ -48,7 +48,7 @@ public class CdmProgressMonitorAdapter implements eu.etaxonomy.cdm.common.monito
                IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, ticks);
                return new CdmProgressMonitorAdapter(subMonitor);
        }
-       
+
        /** {@inheritDoc} */
        @Override
        public void beginTask(String name, int totalWork) {
@@ -107,4 +107,13 @@ public class CdmProgressMonitorAdapter implements eu.etaxonomy.cdm.common.monito
        public void internalWorked(double arg0) {
                progressMonitor.internalWorked(arg0);
        }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void waitForFeedback() {
+        // TODO Auto-generated method stub
+
+    }
 }
index 89e6da3..e132f71 100644 (file)
@@ -88,8 +88,8 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
     }
 
     @Test
-    public void testMonitLongRunningMethodWithCancellation() throws InterruptedException {
-        IllegalStateException ise = new IllegalStateException("Cancelled Exception");
+    public void testMonitLongRunningMethodWithInterrupt() throws InterruptedException {
+        IllegalStateException ise = new IllegalStateException("Interrupted Exception");
         final UUID uuid = testService.monitLongRunningMethod(ise);
         final int pollInterval = 1000;
         final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
@@ -125,6 +125,40 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
 
     }
 
+    @Test
+    public void testMonitLongRunningMethodWithCancellation() throws InterruptedException {
+
+        final UUID uuid = testService.monitLongRunningMethod(null);
+        final int pollInterval = 1000;
+        final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
+        expectedMonitor.setResult("Cancelled");
+        expectedMonitor.setCanceled(true);
+        expectedMonitor.done();
+
+        Thread thread = new Thread() {
+            @Override
+            public void run() {
+                try {
+                    IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
+                            uuid,
+                            pollInterval,
+                            null,
+                            new NullProgressMonitor());
+                } catch (InterruptedException e) {
+
+                }
+            }
+
+        };
+        thread.start();
+        while(!progressMonitorService.isMonitorThreadRunning(uuid)) {}
+        progressMonitorService.cancel(uuid);
+        while(progressMonitorService.isMonitorThreadRunning(uuid)) {}
+        IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
+        new MockPostMoniteredOperationEnabled(expectedMonitor, uuid).postOperation(remotingMonitor);
+
+    }
+
 
     class MockPostMoniteredOperationEnabled implements IPostMoniteredOperationEnabled {