adds update functionality for SCViewer
This commit is contained in:
@ -56,7 +56,7 @@
|
||||
<children xsi:type="menu:HandledMenuItem" xmi:id="_JTXBgYl_EeWxJ_wPkM6yGQ" elementId="" label="Preferences" command="_AxH6sIl_EeWxJ_wPkM6yGQ"/>
|
||||
</children>
|
||||
<children xsi:type="menu:Menu" xmi:id="_95QGxHNmEeWBq8z1Dv39LA" elementId="com.minres.scviewer.e4.application.menu.help" label="Help">
|
||||
<children xsi:type="menu:HandledMenuItem" xmi:id="_UQRi0B07EeuiP60JNw0iiA" elementId="com.minres.scviewer.e4.application.handledmenuitem.checkforupdate" visible="false" label="Check for Update" enabled="false" command="_-9ED4B06EeuiP60JNw0iiA"/>
|
||||
<children xsi:type="menu:HandledMenuItem" xmi:id="_UQRi0B07EeuiP60JNw0iiA" elementId="com.minres.scviewer.e4.application.handledmenuitem.checkforupdate" label="Check for Update" enabled="false" command="_-9ED4B06EeuiP60JNw0iiA"/>
|
||||
<children xsi:type="menu:HandledMenuItem" xmi:id="_qJS-MHCOEeyub8CfGE1sGA" label="Help Content" command="_RdUMoHCOEeyub8CfGE1sGA"/>
|
||||
<children xsi:type="menu:HandledMenuItem" xmi:id="_95QGxXNmEeWBq8z1Dv39LA" label="Web Help" command="_lqjIYEYEEeyPM8G0E2EYww"/>
|
||||
<children xsi:type="menu:HandledMenuItem" xmi:id="_4xtmgEYEEeyPM8G0E2EYww" label="About" command="_95PfxnNmEeWBq8z1Dv39LA"/>
|
||||
|
@ -1,24 +1,140 @@
|
||||
|
||||
package com.minres.scviewer.e4.application.handlers;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.eclipse.core.runtime.SubMonitor;
|
||||
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
|
||||
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
|
||||
import org.eclipse.e4.core.di.annotations.Execute;
|
||||
import org.eclipse.e4.ui.di.UISynchronize;
|
||||
import org.eclipse.e4.ui.workbench.IWorkbench;
|
||||
import org.eclipse.equinox.p2.core.IProvisioningAgent;
|
||||
import org.eclipse.equinox.p2.operations.ProvisioningJob;
|
||||
import org.eclipse.equinox.p2.operations.ProvisioningSession;
|
||||
import org.eclipse.equinox.p2.operations.UpdateOperation;
|
||||
import org.eclipse.jface.dialogs.MessageDialog;
|
||||
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
|
||||
import org.eclipse.jface.operation.IRunnableWithProgress;
|
||||
|
||||
public class UpdateHandler {
|
||||
|
||||
boolean cancelled = false;
|
||||
|
||||
@Execute
|
||||
public void execute(IProvisioningAgent agent, UISynchronize synchronize, IWorkbench workbench) {
|
||||
ProvisioningSession session = new ProvisioningSession(agent);
|
||||
UpdateOperation operation = new UpdateOperation(session);
|
||||
IStatus status = operation.resolveModal(null);
|
||||
if(status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
|
||||
MessageDialog.openInformation(null, "Information", "Nothing to update");
|
||||
public void execute(IProvisioningAgent agent, UISynchronize sync, IWorkbench workbench) {
|
||||
// update using a progress monitor
|
||||
IRunnableWithProgress runnable = new IRunnableWithProgress() {
|
||||
@Override
|
||||
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
|
||||
update(agent, monitor, sync, workbench);
|
||||
}
|
||||
};
|
||||
try {
|
||||
new ProgressMonitorDialog(null).run(true, true, runnable);
|
||||
} catch (InvocationTargetException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private IStatus update(IProvisioningAgent agent, IProgressMonitor monitor, UISynchronize sync,
|
||||
IWorkbench workbench) {
|
||||
ProvisioningSession session = new ProvisioningSession(agent);
|
||||
// update the whole running profile, otherwise specify IUs
|
||||
UpdateOperation operation = new UpdateOperation(session);
|
||||
try {
|
||||
operation.getProvisioningContext().setArtifactRepositories(new URI("http://https://minres.github.io/SCViewer/repository"));
|
||||
operation.getProvisioningContext().setMetadataRepositories(new URI("http://https://minres.github.io/SCViewer/repository"));
|
||||
} catch (URISyntaxException e) {}
|
||||
SubMonitor sub = SubMonitor.convert(monitor, "Checking for application updates...", 200);
|
||||
// check if updates are available
|
||||
IStatus status = operation.resolveModal(sub.newChild(100));
|
||||
if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
|
||||
showMessage(sync, "Nothing to update");
|
||||
return Status.CANCEL_STATUS;
|
||||
} else {
|
||||
ProvisioningJob provisioningJob = operation.getProvisioningJob(sub.newChild(100));
|
||||
if (provisioningJob != null) {
|
||||
sync.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()) {
|
||||
sync.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 {
|
||||
showError(sync, event.getResult().getMessage());
|
||||
cancelled = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
// since we switched to the UI thread for interacting with the user
|
||||
// we need to schedule the provisioning thread, otherwise it would
|
||||
// be executed also in the UI thread and not in a background thread
|
||||
provisioningJob.schedule();
|
||||
} else {
|
||||
cancelled = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (operation.hasResolved()) {
|
||||
showError(sync, "Couldn't get provisioning job: " + operation.getResolutionResult());
|
||||
} else {
|
||||
showError(sync, "Couldn't resolve provisioning job");
|
||||
}
|
||||
cancelled = true;
|
||||
}
|
||||
}
|
||||
if (cancelled) {
|
||||
// reset cancelled flag
|
||||
cancelled = false;
|
||||
return Status.CANCEL_STATUS;
|
||||
}
|
||||
return Status.OK_STATUS;
|
||||
}
|
||||
|
||||
private void showMessage(UISynchronize sync, final String message) {
|
||||
// as the provision needs to be executed in a background thread
|
||||
// we need to ensure that the message dialog is executed in
|
||||
// the UI thread
|
||||
sync.syncExec(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
MessageDialog.openInformation(null, "Information", message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showError(UISynchronize sync, final String message) {
|
||||
// as the provision needs to be executed in a background thread
|
||||
// we need to ensure that the message dialog is executed in
|
||||
// the UI thread
|
||||
sync.syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
MessageDialog.openError(null, "Error", message);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user