Project

General

Profile

Download (2.93 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2008, 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.update;
12

    
13
import org.eclipse.core.commands.AbstractHandler;
14
import org.eclipse.core.commands.ExecutionEvent;
15
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
16
import org.eclipse.core.runtime.jobs.Job;
17
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
18
import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
19
import org.eclipse.equinox.p2.ui.ProvisioningUI;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.ui.PlatformUI;
22

    
23
/**
24
 * PreloadingRepositoryHandler provides background loading of
25
 * repositories before executing the provisioning handler.
26
 * 
27
 * @since 3.5
28
 */
29
abstract class PreloadingRepositoryHandler extends AbstractHandler {
30

    
31
	/**
32
	 * The constructor.
33
	 */
34
	public PreloadingRepositoryHandler() {
35
		// constructor
36
	}
37

    
38
	/**
39
	 * Execute the command.
40
	 */
41
	public Object execute(ExecutionEvent event) {
42
		doExecuteAndLoad();
43
		return null;
44
	}
45

    
46
	void doExecuteAndLoad() {
47
		if (preloadRepositories()) {
48
			//cancel any load that is already running
49
			Job.getJobManager().cancel(LoadMetadataRepositoryJob.LOAD_FAMILY);
50
			final LoadMetadataRepositoryJob loadJob = new LoadMetadataRepositoryJob(getProvisioningUI());
51
			setLoadJobProperties(loadJob);
52
			if (waitForPreload()) {
53
				loadJob.addJobChangeListener(new JobChangeAdapter() {
54
					public void done(IJobChangeEvent event) {
55
						if (PlatformUI.isWorkbenchRunning())
56
							if (event.getResult().isOK()) {
57
								PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
58
									public void run() {
59
										doExecute(loadJob);
60
									}
61
								});
62
							}
63
					}
64
				});
65
				loadJob.setUser(true);
66
				loadJob.schedule();
67

    
68
			} else {
69
				loadJob.setSystem(true);
70
				loadJob.setUser(false);
71
				loadJob.schedule();
72
				doExecute(null);
73
			}
74
		} else {
75
			doExecute(null);
76
		}
77
	}
78

    
79
	protected abstract void doExecute(LoadMetadataRepositoryJob job);
80

    
81
	protected boolean preloadRepositories() {
82
		return true;
83
	}
84

    
85
	protected boolean waitForPreload() {
86
		return true;
87
	}
88

    
89
	protected void setLoadJobProperties(Job loadJob) {
90
		loadJob.setProperty(LoadMetadataRepositoryJob.ACCUMULATE_LOAD_ERRORS, Boolean.toString(true));
91
	}
92

    
93
	protected ProvisioningUI getProvisioningUI() {
94
		return ProvisioningUI.getDefaultUI();
95
	}
96

    
97
	/**
98
	 * Return a shell appropriate for parenting dialogs of this handler.
99
	 * @return a Shell
100
	 */
101
	protected Shell getShell() {
102
		return PlatformUI.getWorkbench().getModalDialogShellProvider().getShell();
103
	}
104
}
(3-3/4)