5cf51b0441a5924fb384553209b51f50e7f0c247
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / handler / update / UpdateHandler.java
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
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package eu.etaxonomy.taxeditor.handler.update;
12
13 import java.net.URI;
14
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;
30
31 /**
32 * UpdateHandler invokes the check for updates UI
33 *
34 * @since 3.4
35 */
36 public class UpdateHandler {
37
38 @Execute
39 public void execute(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
40 final IWorkbench workbench) {
41 Job updateJob = new Job("Update Job") {
42 @Override
43 protected IStatus run(final IProgressMonitor monitor) {
44 return checkForUpdates(agent, shell, sync, workbench, monitor);
45 }
46 };
47 updateJob.schedule();
48 }
49
50 private IStatus checkForUpdates(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
51 final IWorkbench workbench, IProgressMonitor monitor) {
52
53 // configure update operation
54 final ProvisioningSession session = new ProvisioningSession(agent);
55 final UpdateOperation operation = new UpdateOperation(session);
56 configureUpdate(operation);
57
58 // check for updates, this causes I/O
59 final IStatus status = operation.resolveModal(monitor);
60
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;
65 }
66
67 // run installation
68 final ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor);
69
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;
74 }
75 configureProvisioningJob(provisioningJob, shell, sync, workbench);
76
77 provisioningJob.schedule();
78 return Status.OK_STATUS;
79
80 }
81
82 private void configureProvisioningJob(ProvisioningJob provisioningJob, final Shell shell, final UISynchronize sync,
83 final IWorkbench workbench) {
84
85 // register a job change listener to track
86 // installation progress and notify user upon success
87 provisioningJob.addJobChangeListener(new JobChangeAdapter() {
88 @Override
89 public void done(IJobChangeEvent event) {
90 if (event.getResult().isOK()) {
91 sync.syncExec(new Runnable() {
92
93 @Override
94 public void run() {
95 boolean restart = MessageDialog.openQuestion(shell, "Updates installed, restart?",
96 "Updates have been installed. Do you want to restart?");
97 if (restart) {
98 workbench.restart();
99 }
100 }
101 });
102
103 }
104 super.done(event);
105 }
106 });
107
108 }
109
110 private void showMessage(final Shell parent, final UISynchronize sync) {
111 sync.syncExec(new Runnable() {
112
113 @Override
114 public void run() {
115 MessageDialog.openWarning(parent, "No update",
116 "No updates for the current installation have been found.");
117 }
118 });
119 }
120
121 private UpdateOperation configureUpdate(final UpdateOperation operation) {
122 // create uri and check for validity
123 URI uri = null;
124 uri = P2Util.getP2UpdateRepository();
125
126 // set location of artifact and metadata repo
127 operation.getProvisioningContext().setArtifactRepositories(new URI[] { uri });
128 operation.getProvisioningContext().setMetadataRepositories(new URI[] { uri });
129 return operation;
130 }
131
132 }