2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.update
;
11 import java
.lang
.reflect
.InvocationTargetException
;
14 import org
.eclipse
.core
.commands
.AbstractHandler
;
15 import org
.eclipse
.core
.commands
.ExecutionEvent
;
16 import org
.eclipse
.core
.commands
.ExecutionException
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
20 import org
.eclipse
.core
.runtime
.jobs
.IJobChangeEvent
;
21 import org
.eclipse
.core
.runtime
.jobs
.JobChangeAdapter
;
22 import org
.eclipse
.equinox
.internal
.p2
.ui
.ProvUI
;
23 import org
.eclipse
.equinox
.p2
.core
.IProvisioningAgent
;
24 import org
.eclipse
.equinox
.p2
.core
.ProvisionException
;
25 import org
.eclipse
.equinox
.p2
.operations
.ProvisioningJob
;
26 import org
.eclipse
.equinox
.p2
.operations
.ProvisioningSession
;
27 import org
.eclipse
.equinox
.p2
.operations
.UpdateOperation
;
28 import org
.eclipse
.equinox
.p2
.repository
.IRepositoryManager
;
29 import org
.eclipse
.equinox
.p2
.repository
.metadata
.IMetadataRepositoryManager
;
30 import org
.eclipse
.equinox
.p2
.ui
.ProvisioningUI
;
31 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
32 import org
.eclipse
.jface
.dialogs
.ProgressMonitorDialog
;
33 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.ui
.IWorkbench
;
37 import org
.eclipse
.ui
.PlatformUI
;
38 import org
.osgi
.framework
.BundleContext
;
39 import org
.osgi
.framework
.ServiceReference
;
41 import eu
.etaxonomy
.taxeditor
.TaxonomicEditorPlugin
;
42 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
45 * UpdateHandler invokes the check for updates UI
48 public class UpdateHandler
extends AbstractHandler
{
50 private static final String ERROR_PERFORMING_UPDATES
= "Error performing updates";
55 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
56 // update using a progress monitor
57 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
59 public void run(IProgressMonitor monitor
) throws InvocationTargetException
,
60 InterruptedException
{
66 new ProgressMonitorDialog(null).run(true, true, runnable
);
67 } catch (InvocationTargetException
| InterruptedException e
) {
73 private void doExecute(IProgressMonitor monitor
) {
75 //UpdateOperation operation = new UpdateOperation(ProvisioningUI.getDefaultUI().getSession());
77 BundleContext bundleContext
= TaxonomicEditorPlugin
.getContext();
78 ServiceReference reference
= bundleContext
.getServiceReference(IProvisioningAgent
.SERVICE_NAME
);
79 if (reference
== null) {
80 showMessage(MessageDialog
.ERROR
, ERROR_PERFORMING_UPDATES
, "No provisioning agent found. This application is not set up for updates.");
84 final IProvisioningAgent agent
= (IProvisioningAgent
) bundleContext
.getService(reference
);
85 final IWorkbench workbench
= PlatformUI
.getWorkbench();
87 ProvisioningSession session
= new ProvisioningSession(agent
);
88 // update all user-visible installable units
89 UpdateOperation operation
= new UpdateOperation(session
);
91 // force refresh all the repository caches before update
92 IMetadataRepositoryManager metaManager
= ProvUI
.getMetadataRepositoryManager(ProvisioningUI
.getDefaultUI().getSession());
93 URI
[] repos
= metaManager
.getKnownRepositories(IRepositoryManager
.REPOSITORIES_ALL
);
95 for(URI repo
: repos
) {
97 metaManager
.refreshRepository(repo
, null);
98 } catch (ProvisionException
| OperationCanceledException e
) {
103 // set location of artifact and metadata repo
104 operation
.getProvisioningContext().setArtifactRepositories(repos
);
105 operation
.getProvisioningContext().setMetadataRepositories(repos
);
107 IStatus status
= operation
.resolveModal(monitor
);
109 // if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) {
110 // if (UpdateSingleIUWizard.validFor(operation)) {
111 // // Special case for only updating a single root
112 // UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation);
113 // WizardDialog dialog = new WizardDialog(getShell(), wizard);
117 // // Open the normal version of the update wizard
118 // getProvisioningUI().openUpdateWizard(false, operation, job);
122 if (status
.getCode() == UpdateOperation
.STATUS_NOTHING_TO_UPDATE
) {
123 showMessage(MessageDialog
.INFORMATION
, "Checking for updates", "No updates were found");
127 final ProvisioningJob provisioningJob
= operation
.getProvisioningJob(monitor
);
128 if (provisioningJob
!= null) {
129 Display
.getDefault().syncExec(new Runnable() {
132 boolean performUpdate
= MessageDialog
.openQuestion(
135 "There are updates available. Do you want to install them now?");
137 provisioningJob
.addJobChangeListener(new JobChangeAdapter() {
139 public void done(IJobChangeEvent event
) {
140 if (event
.getResult().isOK()) {
141 Display
.getDefault().syncExec(new Runnable() {
144 boolean restart
= MessageDialog
.openQuestion(null,
145 "Updates installed, restart?",
146 "Updates have been installed successfully, do you want to restart?");
153 MessageDialog
.openInformation(
155 ERROR_PERFORMING_UPDATES
,
156 event
.getResult().getMessage());
160 provisioningJob
.schedule();
165 if (operation
.hasResolved()) {
166 showMessage(MessageDialog
.ERROR
, ERROR_PERFORMING_UPDATES
, "Couldn't get provisioning job: " + operation
.getResolutionResult());
168 showMessage(MessageDialog
.ERROR
, ERROR_PERFORMING_UPDATES
, "Couldn't resolve provisioning job");
173 private void showMessage(final int kind
, final String title
, final String info
) {
174 Display
.getDefault().syncExec(new Runnable() {
177 MessageDialog
.open(kind
, null, title
, info
, SWT
.NONE
);
182 private void showError(final Throwable t
) {
183 Display
.getDefault().syncExec(new Runnable() {
186 MessagingUtils
.errorDialog(ERROR_PERFORMING_UPDATES
,
189 TaxonomicEditorPlugin
.PLUGIN_ID
,