Project

General

Profile

Download (6.28 KB) Statistics
| Branch: | Tag: | Revision:
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.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.p2.core.IProvisioningAgent;
26
import org.eclipse.equinox.p2.operations.ProvisioningJob;
27
import org.eclipse.equinox.p2.operations.ProvisioningSession;
28
import org.eclipse.equinox.p2.operations.UpdateOperation;
29
import org.eclipse.jface.dialogs.MessageDialog;
30
import org.eclipse.swt.widgets.Shell;
31

    
32
import eu.etaxonomy.taxeditor.l10n.Messages;
33

    
34
/**
35
 * UpdateHandler invokes the check for updates UI
36
 *
37
 * @since 3.4
38
 */
39
public class UpdateHandler {
40

    
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 checkForUpdates(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 checkForUpdates(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

    
137
    private void showMessage(final Shell parent, final UISynchronize sync) {
138
        sync.syncExec(()->
139
        MessageDialog.openWarning(parent, Messages.UpdateHandler_NO_UPDATE_TITLE,
140
                        Messages.UpdateHandler_NO_UPDATE_MESSAGE)
141
        );
142
    }
143

    
144
    private UpdateOperation configureUpdate(final UpdateOperation operation) {
145
        // create uri and check for validity
146
        URI uri = null;
147
        uri = P2Util.getP2UpdateRepository();
148

    
149
        // set location of artifact and metadata repo
150
        operation.getProvisioningContext().setArtifactRepositories(new URI[] { uri });
151
        operation.getProvisioningContext().setMetadataRepositories(new URI[] { uri });
152
        return operation;
153
    }
154

    
155
}
(2-2/2)