1
|
/**
|
2
|
* Copyright (C) 2020 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.handler.update;
|
10
|
|
11
|
import java.net.URI;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.eclipse.core.runtime.IProgressMonitor;
|
17
|
import org.eclipse.core.runtime.IStatus;
|
18
|
import org.eclipse.core.runtime.Status;
|
19
|
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
|
20
|
import org.eclipse.core.runtime.jobs.Job;
|
21
|
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
|
22
|
import org.eclipse.e4.core.di.annotations.Execute;
|
23
|
import org.eclipse.e4.ui.di.UISynchronize;
|
24
|
import org.eclipse.e4.ui.workbench.IWorkbench;
|
25
|
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
|
26
|
import org.eclipse.equinox.p2.core.IProvisioningAgent;
|
27
|
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
|
28
|
import org.eclipse.equinox.p2.operations.ProvisioningJob;
|
29
|
import org.eclipse.equinox.p2.operations.ProvisioningSession;
|
30
|
import org.eclipse.equinox.p2.operations.UpdateOperation;
|
31
|
import org.eclipse.jface.dialogs.MessageDialog;
|
32
|
import org.eclipse.swt.widgets.Shell;
|
33
|
|
34
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
35
|
|
36
|
/**
|
37
|
* @author k.luther
|
38
|
* @since Sep 24, 2020
|
39
|
*/
|
40
|
public class SearchPluginHandler {
|
41
|
private Logger logger = Logger.getLogger(getClass());
|
42
|
|
43
|
private UpdateOperation operation;
|
44
|
|
45
|
@Execute
|
46
|
public void execute(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
|
47
|
final IWorkbench workbench) {
|
48
|
Job checkUpdateJob = new Job(Messages.UpdateHandler_CHECK_UPDATE_JOB) {
|
49
|
@Override
|
50
|
protected IStatus run(final IProgressMonitor monitor) {
|
51
|
return checkForPlugins(agent, shell, sync, monitor);
|
52
|
}
|
53
|
};
|
54
|
checkUpdateJob.schedule();
|
55
|
|
56
|
|
57
|
checkUpdateJob.addJobChangeListener(new JobChangeAdapter() {
|
58
|
@Override
|
59
|
public void done(IJobChangeEvent event) {
|
60
|
if (event.getResult().isOK()) {
|
61
|
sync.syncExec(new Runnable() {
|
62
|
|
63
|
@Override
|
64
|
public void run() {
|
65
|
if(MessageDialog.openConfirm(shell, Messages.UpdateHandler_UPDATES_FOUND_TITLE, Messages.UpdateHandler_UPDATES_FOUND_MESSAGE)){
|
66
|
Job installUpdateJob = new Job(Messages.UpdateHandler_INSTALL_JOB) {
|
67
|
@Override
|
68
|
protected IStatus run(final IProgressMonitor monitor) {
|
69
|
// run installation
|
70
|
ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor);
|
71
|
|
72
|
// updates cannot run from within Eclipse IDE!!!
|
73
|
if (provisioningJob == null) {
|
74
|
logger.error("Trying to update from the Eclipse IDE? This won't work!"); //$NON-NLS-1$
|
75
|
return Status.CANCEL_STATUS;
|
76
|
}
|
77
|
configureProvisioningJob(provisioningJob, shell, sync, workbench);
|
78
|
provisioningJob.schedule();
|
79
|
return Status.OK_STATUS;
|
80
|
}
|
81
|
};
|
82
|
installUpdateJob.schedule();
|
83
|
}
|
84
|
}
|
85
|
});
|
86
|
}
|
87
|
}
|
88
|
});
|
89
|
}
|
90
|
|
91
|
private IStatus checkForPlugins(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
|
92
|
IProgressMonitor monitor) {
|
93
|
|
94
|
// configure update operation
|
95
|
final ProvisioningSession session = new ProvisioningSession(agent);
|
96
|
operation = new UpdateOperation(session);
|
97
|
configureUpdate(operation);
|
98
|
|
99
|
// check for updates, this causes I/O
|
100
|
final IStatus status = operation.resolveModal(monitor);
|
101
|
|
102
|
// failed to find updates (inform user and exit)
|
103
|
if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
|
104
|
showMessage(shell, sync);
|
105
|
return Status.CANCEL_STATUS;
|
106
|
}
|
107
|
return Status.OK_STATUS;
|
108
|
}
|
109
|
|
110
|
private void configureProvisioningJob(ProvisioningJob provisioningJob, final Shell shell, final UISynchronize sync,
|
111
|
final IWorkbench workbench) {
|
112
|
|
113
|
// register a job change listener to track
|
114
|
// installation progress and notify user upon success
|
115
|
provisioningJob.addJobChangeListener(new JobChangeAdapter() {
|
116
|
@Override
|
117
|
public void done(IJobChangeEvent event) {
|
118
|
if (event.getResult().isOK()) {
|
119
|
sync.syncExec(new Runnable() {
|
120
|
|
121
|
@Override
|
122
|
public void run() {
|
123
|
boolean restart = MessageDialog.openQuestion(shell, Messages.UpdateHandler_UPDATE_INSTALLED_TITLE,
|
124
|
Messages.UpdateHandler_UPDATE_INSTALLED_TITLE_MESSAGE);
|
125
|
if (restart) {
|
126
|
workbench.restart();
|
127
|
}
|
128
|
}
|
129
|
});
|
130
|
}
|
131
|
super.done(event);
|
132
|
}
|
133
|
});
|
134
|
}
|
135
|
|
136
|
private void showMessage(final Shell parent, final UISynchronize sync) {
|
137
|
sync.syncExec(()->
|
138
|
MessageDialog.openWarning(parent, Messages.UpdateHandler_NO_UPDATE_TITLE,
|
139
|
Messages.UpdateHandler_NO_UPDATE_MESSAGE)
|
140
|
);
|
141
|
}
|
142
|
|
143
|
private UpdateOperation configureUpdate(final UpdateOperation operation) {
|
144
|
// create uri and check for validity
|
145
|
URI uri = null;
|
146
|
uri = P2Util.getP2UpdateRepository();
|
147
|
|
148
|
// set location of artifact and metadata repo
|
149
|
operation.getProvisioningContext().setArtifactRepositories(new URI[] { uri });
|
150
|
operation.getProvisioningContext().setMetadataRepositories(new URI[] { uri });
|
151
|
InstallableUnit ctxIU = new InstallableUnit();
|
152
|
ctxIU.setId("eu.etaxonomy.taxeditor.local");
|
153
|
List<IInstallableUnit> extraIUs = new ArrayList<>();
|
154
|
extraIUs.add(ctxIU);
|
155
|
operation.getProvisioningContext().setExtraInstallableUnits(extraIUs);
|
156
|
|
157
|
return operation;
|
158
|
}
|
159
|
}
|