Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2de27673aaa582af5a3fce355dc0ee81ccb0a9a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 *  Copyright (c) 2012 Christian Pontesegger and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     Christian Pontesegger - initial API and implementation
 *******************************************************************************/

package org.eclipse.pde.internal.ui.views.imagebrowser.repositories;

import org.eclipse.pde.internal.ui.PDEUIMessages;

import java.io.File;
import java.net.URI;
import org.eclipse.core.runtime.*;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.core.target.*;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.views.imagebrowser.IImageTarget;
import org.eclipse.ui.PlatformUI;

public class TargetPlatformRepository extends AbstractRepository {

	private boolean fUseCurrent;

	/**
	 * Creates a new target platform repository.  If useCurrent is <code>true</code>
	 * the current target platform set on the preference page.  If <code>false</code>
	 * a default target definition (the running application) will be used.
	 * 
	 * @param useCurrent whether to use the current target platform or the default target (running application)
	 */
	public TargetPlatformRepository(boolean useCurrent) {
		fUseCurrent = useCurrent;
	}

	public IStatus searchImages(final IImageTarget target, final IProgressMonitor monitor) {
		try {

			ITargetPlatformService service = (ITargetPlatformService) PlatformUI.getWorkbench().getService(ITargetPlatformService.class);
			if (service != null) {
				ITargetDefinition definition = null;
				if (fUseCurrent) {
					ITargetHandle workspaceTargetHandle = service.getWorkspaceTargetHandle();
					if (workspaceTargetHandle != null) {
						definition = workspaceTargetHandle.getTargetDefinition();
					}
				} else {
					definition = service.newDefaultTarget();
				}

				if (definition != null) {

					if (!definition.isResolved())
						definition.resolve(monitor);

					TargetBundle[] allBundles = definition.getAllBundles();
					if (allBundles != null) {
						for (TargetBundle bundle : allBundles) {
							URI location = bundle.getBundleInfo().getLocation();
							File file = new File(location);
							if (isJar(file))
								searchJarFile(file, target, monitor);
						}
					}
				}
			} else {
				PDEPlugin.logErrorMessage(PDEUIMessages.TargetPlatformRepository_CouldNotFindTargetPlatformService);
			}

		} catch (CoreException e) {
			PDEPlugin.log(e);
		}

		return Status.OK_STATUS;
	}

	public String toString() {
		if (!fUseCurrent) {
			return PDEUIMessages.TargetPlatformRepository_RunningPlatform;
		}

		try {
			ITargetPlatformService service = (ITargetPlatformService) PlatformUI.getWorkbench().getService(ITargetPlatformService.class);
			if (service != null) {
				ITargetHandle workspaceTargetHandle = service.getWorkspaceTargetHandle();
				if (workspaceTargetHandle != null) {
					ITargetDefinition definition = workspaceTargetHandle.getTargetDefinition();
					String name = definition.getName();
					if (name.length() > 30) {
						name = name.substring(0, 30);
					}
					return NLS.bind(PDEUIMessages.TargetPlatformRepository_TargetPlatformLabel, name);
				}
			}
		} catch (CoreException e) {
			PDEPlugin.log(e);
		}

		return super.toString();
	}

}

Back to the top