1 /*******************************************************************************
2 * Copyright (c) 2007, 2009 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package eu
.etaxonomy
.taxeditor
.handler
.update
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.core
.runtime
.IStatus
;
17 import org
.eclipse
.core
.runtime
.Status
;
18 import org
.eclipse
.core
.runtime
.jobs
.IJobChangeEvent
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.core
.runtime
.jobs
.JobChangeAdapter
;
21 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
22 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
23 import org
.eclipse
.e4
.ui
.workbench
.IWorkbench
;
24 import org
.eclipse
.equinox
.p2
.core
.IProvisioningAgent
;
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
.jface
.dialogs
.MessageDialog
;
29 import org
.eclipse
.swt
.widgets
.Shell
;
32 * UpdateHandler invokes the check for updates UI
36 public class UpdateHandler
{
39 public void execute(final IProvisioningAgent agent
, final Shell shell
, final UISynchronize sync
,
40 final IWorkbench workbench
) {
41 Job updateJob
= new Job("Update Job") {
43 protected IStatus
run(final IProgressMonitor monitor
) {
44 return checkForUpdates(agent
, shell
, sync
, workbench
, monitor
);
50 private IStatus
checkForUpdates(final IProvisioningAgent agent
, final Shell shell
, final UISynchronize sync
,
51 final IWorkbench workbench
, IProgressMonitor monitor
) {
53 // configure update operation
54 final ProvisioningSession session
= new ProvisioningSession(agent
);
55 final UpdateOperation operation
= new UpdateOperation(session
);
56 configureUpdate(operation
);
58 // check for updates, this causes I/O
59 final IStatus status
= operation
.resolveModal(monitor
);
61 // failed to find updates (inform user and exit)
62 if (status
.getCode() == UpdateOperation
.STATUS_NOTHING_TO_UPDATE
) {
63 showMessage(shell
, sync
);
64 return Status
.CANCEL_STATUS
;
68 final ProvisioningJob provisioningJob
= operation
.getProvisioningJob(monitor
);
70 // updates cannot run from within Eclipse IDE!!!
71 if (provisioningJob
== null) {
72 System
.err
.println("Trying to update from the Eclipse IDE? This won't work!");
73 return Status
.CANCEL_STATUS
;
75 configureProvisioningJob(provisioningJob
, shell
, sync
, workbench
);
77 provisioningJob
.schedule();
78 return Status
.OK_STATUS
;
82 private void configureProvisioningJob(ProvisioningJob provisioningJob
, final Shell shell
, final UISynchronize sync
,
83 final IWorkbench workbench
) {
85 // register a job change listener to track
86 // installation progress and notify user upon success
87 provisioningJob
.addJobChangeListener(new JobChangeAdapter() {
89 public void done(IJobChangeEvent event
) {
90 if (event
.getResult().isOK()) {
91 sync
.syncExec(new Runnable() {
95 boolean restart
= MessageDialog
.openQuestion(shell
, "Updates installed, restart?",
96 "Updates have been installed. Do you want to restart?");
110 private void showMessage(final Shell parent
, final UISynchronize sync
) {
111 sync
.syncExec(new Runnable() {
115 MessageDialog
.openWarning(parent
, "No update",
116 "No updates for the current installation have been found.");
121 private UpdateOperation
configureUpdate(final UpdateOperation operation
) {
122 // create uri and check for validity
124 uri
= P2Util
.getP2UpdateRepository();
126 // set location of artifact and metadata repo
127 operation
.getProvisioningContext().setArtifactRepositories(new URI
[] { uri
});
128 operation
.getProvisioningContext().setMetadataRepositories(new URI
[] { uri
});