ref #6566 Migrate help menu
[taxeditor.git] / eu.etaxonomy.taxeditor.workbench / src / eu / etaxonomy / taxeditor / workbench / update / PreloadingRepositoryHandler.java
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.workbench.update;
12
13 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
14 import org.eclipse.core.runtime.jobs.Job;
15 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
16 import org.eclipse.e4.core.di.annotations.Execute;
17 import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
18 import org.eclipse.equinox.p2.ui.ProvisioningUI;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.PlatformUI;
21
22 /**
23 * PreloadingRepositoryHandler provides background loading of
24 * repositories before executing the provisioning handler.
25 *
26 * @since 3.5
27 */
28 abstract class PreloadingRepositoryHandler{
29
30 /**
31 * The constructor.
32 */
33 public PreloadingRepositoryHandler() {
34 // constructor
35 }
36
37 /**
38 * Execute the command.
39 */
40 @Execute
41 public Object execute() {
42 doExecuteAndLoad();
43 return null;
44 }
45
46 private 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 @Override
55 public void done(IJobChangeEvent event) {
56 if (PlatformUI.isWorkbenchRunning()) {
57 if (event.getResult().isOK()) {
58 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
59 @Override
60 public void run() {
61 doExecute(loadJob);
62 }
63 });
64 }
65 }
66 }
67 });
68 loadJob.setUser(true);
69 loadJob.schedule();
70
71 } else {
72 loadJob.setSystem(true);
73 loadJob.setUser(false);
74 loadJob.schedule();
75 doExecute(null);
76 }
77 } else {
78 doExecute(null);
79 }
80 }
81
82 protected abstract void doExecute(LoadMetadataRepositoryJob job);
83
84 protected boolean preloadRepositories() {
85 return true;
86 }
87
88 protected boolean waitForPreload() {
89 return true;
90 }
91
92 protected void setLoadJobProperties(Job loadJob) {
93 loadJob.setProperty(LoadMetadataRepositoryJob.ACCUMULATE_LOAD_ERRORS, Boolean.toString(true));
94 }
95
96 protected ProvisioningUI getProvisioningUI() {
97 return ProvisioningUI.getDefaultUI();
98 }
99
100 /**
101 * Return a shell appropriate for parenting dialogs of this handler.
102 * @return a Shell
103 */
104 protected Shell getShell() {
105 return PlatformUI.getWorkbench().getModalDialogShellProvider().getShell();
106 }
107 }