Move to simpler update process
authorCherian Mathew <c.mathew@bgbm.org>
Tue, 24 Nov 2015 13:02:40 +0000 (14:02 +0100)
committerCherian Mathew <c.mathew@bgbm.org>
Tue, 24 Nov 2015 13:02:40 +0000 (14:02 +0100)
eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/UpdateHandler.java

index 4a5f40742ecc82c5c0a2a6ca05d750034cc97ee0..3894b0432f13dec8553829f174b8158c2b32d77c 100644 (file)
@@ -8,22 +8,31 @@
 */
 package eu.etaxonomy.taxeditor.update;
 
+import java.lang.reflect.InvocationTargetException;
 import java.net.URI;
 
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
+import org.eclipse.core.runtime.jobs.JobChangeAdapter;
 import org.eclipse.equinox.internal.p2.ui.ProvUI;
-import org.eclipse.equinox.internal.p2.ui.dialogs.UpdateSingleIUWizard;
 import org.eclipse.equinox.p2.core.IProvisioningAgent;
 import org.eclipse.equinox.p2.core.ProvisionException;
+import org.eclipse.equinox.p2.operations.ProvisioningJob;
 import org.eclipse.equinox.p2.operations.ProvisioningSession;
 import org.eclipse.equinox.p2.operations.UpdateOperation;
 import org.eclipse.equinox.p2.repository.IRepositoryManager;
 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
-import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
 import org.eclipse.equinox.p2.ui.ProvisioningUI;
 import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.wizard.WizardDialog;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.PlatformUI;
 import org.osgi.framework.BundleContext;
@@ -36,26 +45,44 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
  * UpdateHandler invokes the check for updates UI
  *
  */
-public class UpdateHandler extends PreloadingRepositoryHandler {
-
+public class UpdateHandler extends AbstractHandler {
 
+    private static final String ERROR_PERFORMING_UPDATES = "Error performing updates";
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    protected void doExecute(LoadMetadataRepositoryJob job) {
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+        // update using a progress monitor
+        IRunnableWithProgress runnable = new IRunnableWithProgress() {
+            @Override
+            public void run(IProgressMonitor monitor) throws InvocationTargetException,
+                    InterruptedException {
+                doExecute(monitor);
+            }
+        };
+
+        try {
+            new ProgressMonitorDialog(null).run(true, true, runnable);
+        } catch (InvocationTargetException | InterruptedException e) {
+            showError(e);
+        }
+        return null;
+    }
+
+    private void doExecute(IProgressMonitor monitor) {
 
         //UpdateOperation operation = new UpdateOperation(ProvisioningUI.getDefaultUI().getSession());
 
         BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
         ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
         if (reference == null) {
-            MessageDialog.openInformation(
-                    null,
-                    "Information",
-                    "No provisioning agent found.  This application is not set up for updates.");
+            showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "No provisioning agent found.  This application is not set up for updates.");
             return;
         }
 
         final IProvisioningAgent agent = (IProvisioningAgent) bundleContext.getService(reference);
-        IWorkbench workbench = PlatformUI.getWorkbench();
+        final IWorkbench workbench = PlatformUI.getWorkbench();
 
         ProvisioningSession session = new ProvisioningSession(agent);
         // update all user-visible installable units
@@ -68,23 +95,8 @@ public class UpdateHandler extends PreloadingRepositoryHandler {
         for(URI repo : repos) {
             try {
                 metaManager.refreshRepository(repo, null);
-            } catch (ProvisionException pe) {
-                MessagingUtils.errorDialog("Error reloading repository cache",
-                        this,
-                        pe.getMessage(),
-                        TaxonomicEditorPlugin.PLUGIN_ID,
-                        pe,
-                        true);
-                return;
-
-            } catch (OperationCanceledException oce) {
-                MessagingUtils.errorDialog("Error reloading repository cache",
-                        this,
-                        oce.getMessage(),
-                        TaxonomicEditorPlugin.PLUGIN_ID,
-                        oce,
-                        true);
-                return;
+            } catch (ProvisionException | OperationCanceledException e) {
+                showError(e);
             }
         }
 
@@ -92,19 +104,94 @@ public class UpdateHandler extends PreloadingRepositoryHandler {
         operation.getProvisioningContext().setArtifactRepositories(repos);
         operation.getProvisioningContext().setMetadataRepositories(repos);
 
-        IStatus status = operation.resolveModal(null);
+        IStatus status = operation.resolveModal(monitor);
+
+//        if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
+//            if (UpdateSingleIUWizard.validFor(operation)) {
+//                // Special case for only updating a single root
+//                UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
+//                WizardDialog dialog = new WizardDialog(getShell(), wizard);
+//                dialog.create();
+//                dialog.open();
+//            } else {
+//                // Open the normal version of the update wizard
+//                getProvisioningUI().openUpdateWizard(false, operation, job);
+//            }
+//        }
+
+        if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
+            showMessage(MessageDialog.INFORMATION, "Checking for updates", "No updates were found");
+            return;
+        }
 
-        if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
-            if (UpdateSingleIUWizard.validFor(operation)) {
-                // Special case for only updating a single root
-                UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
-                WizardDialog dialog = new WizardDialog(getShell(), wizard);
-                dialog.create();
-                dialog.open();
+        final ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor);
+        if (provisioningJob != null) {
+            Display.getDefault().syncExec(new Runnable() {
+                @Override
+                public void run() {
+                    boolean performUpdate = MessageDialog.openQuestion(
+                            null,
+                            "Updates available",
+                            "There are updates available. Do you want to install them now?");
+                    if (performUpdate) {
+                        provisioningJob.addJobChangeListener(new JobChangeAdapter() {
+                            @Override
+                            public void done(IJobChangeEvent event) {
+                                if (event.getResult().isOK()) {
+                                    Display.getDefault().syncExec(new Runnable() {
+                                        @Override
+                                        public void run() {
+                                            boolean restart = MessageDialog.openQuestion(null,
+                                                    "Updates installed, restart?",
+                                                    "Updates have been installed successfully, do you want to restart?");
+                                            if (restart) {
+                                                workbench.restart();
+                                            }
+                                        }
+                                    });
+                                } else {
+                                    MessageDialog.openInformation(
+                                            null,
+                                            ERROR_PERFORMING_UPDATES,
+                                            event.getResult().getMessage());
+                                }
+                            }
+                        });
+                        provisioningJob.schedule();
+                    }
+                }
+            });
+        } else {
+            if (operation.hasResolved()) {
+                showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "Couldn't get provisioning job: " + operation.getResolutionResult());
             } else {
-                // Open the normal version of the update wizard
-                getProvisioningUI().openUpdateWizard(false, operation, job);
+                showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "Couldn't resolve provisioning job");
             }
         }
     }
+
+    private void showMessage(final int kind, final String title, final String info) {
+        Display.getDefault().syncExec(new Runnable() {
+            @Override
+            public void run() {
+                MessageDialog.open(kind, null, title, info, SWT.NONE);
+            }
+        });
+    }
+
+    private void showError(final Throwable t) {
+        Display.getDefault().syncExec(new Runnable() {
+            @Override
+            public void run() {
+                MessagingUtils.errorDialog(ERROR_PERFORMING_UPDATES,
+                        this,
+                        t.getMessage(),
+                        TaxonomicEditorPlugin.PLUGIN_ID,
+                        t,
+                        true);
+            }
+        });
+    }
+
+
 }