Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008 Benjamin Muskalla 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 |
* Benjamin Muskalla - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.internal.ui.wizards.buildpaths; |
12 |
|
13 |
import java.io.IOException; |
14 |
import java.net.MalformedURLException; |
15 |
import java.net.URL; |
16 |
|
17 |
import org.osgi.framework.Bundle; |
18 |
|
19 |
import org.eclipse.core.runtime.CoreException; |
20 |
import org.eclipse.core.runtime.FileLocator; |
21 |
import org.eclipse.core.runtime.IPath; |
22 |
import org.eclipse.core.runtime.Path; |
23 |
import org.eclipse.core.runtime.Platform; |
24 |
|
25 |
import org.eclipse.jdt.core.ClasspathContainerInitializer; |
26 |
import org.eclipse.jdt.core.IAccessRule; |
27 |
import org.eclipse.jdt.core.IClasspathAttribute; |
28 |
import org.eclipse.jdt.core.IClasspathContainer; |
29 |
import org.eclipse.jdt.core.IClasspathEntry; |
30 |
import org.eclipse.jdt.core.IJavaProject; |
31 |
import org.eclipse.jdt.core.JavaCore; |
32 |
|
33 |
|
34 |
public class SWTClasspathContainerInitializer extends ClasspathContainerInitializer { |
35 |
|
36 |
public static final String SWT_CONTAINER_ID= "org.eclipse.jdt.SWT_CONTAINER"; //$NON-NLS-1$ |
37 |
|
38 |
public static class SWTContainer implements IClasspathContainer { |
39 |
|
40 |
private final IClasspathEntry[] fEntries; |
41 |
|
42 |
private final IPath fPath; |
43 |
|
44 |
public SWTContainer(IPath path, IClasspathEntry[] entries) { |
45 |
fPath= path; |
46 |
fEntries= entries; |
47 |
} |
48 |
|
49 |
public IClasspathEntry[] getClasspathEntries() { |
50 |
return fEntries; |
51 |
} |
52 |
|
53 |
public String getDescription() { |
54 |
return "Standard Widget Toolkit (SWT)"; |
55 |
} |
56 |
|
57 |
public int getKind() { |
58 |
return IClasspathContainer.K_APPLICATION; |
59 |
} |
60 |
|
61 |
public IPath getPath() { |
62 |
return fPath; |
63 |
} |
64 |
|
65 |
} |
66 |
|
67 |
|
68 |
public SWTClasspathContainerInitializer() { |
69 |
} |
70 |
|
71 |
/* |
72 |
* (non-Javadoc) |
73 |
* |
74 |
* @see |
75 |
* org.eclipse.jdt.core.ClasspathContainerInitializer#initialize(org.eclipse.core.runtime.IPath, |
76 |
* org.eclipse.jdt.core.IJavaProject) |
77 |
*/ |
78 |
public void initialize(IPath containerPath, IJavaProject project) throws CoreException { |
79 |
if (isValidSWTContainerPath(containerPath)) { |
80 |
SWTContainer container= getNewContainer(containerPath); |
81 |
JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project }, new IClasspathContainer[] { container }, null); |
82 |
} |
83 |
} |
84 |
|
85 |
private SWTContainer getNewContainer(IPath containerPath) { |
86 |
IClasspathEntry[] entries= getSWTLibraryEntrys(); |
87 |
return new SWTContainer(containerPath, entries); |
88 |
} |
89 |
|
90 |
public static IClasspathEntry[] getSWTLibraryEntrys() { |
91 |
Bundle hostBundle= Platform.getBundle("org.eclipse.swt"); //$NON-NLS-1$ |
92 |
Bundle[] fragments= Platform.getFragments(hostBundle); |
93 |
|
94 |
IPath hostBundlePath= getBundleLocation( hostBundle ); |
95 |
|
96 |
// TODO: can we assume that SWT has only 1 fragment? |
97 |
final Bundle swtFragment= fragments[0]; |
98 |
final Bundle swtFragmentSrc= Platform.getBundle( swtFragment.getSymbolicName() + ".source"); //$NON-NLS-1$ |
99 |
|
100 |
IPath fragmentBundlePath= getBundleLocation( swtFragment ); |
101 |
IPath fragmentBundleSrcPath= getBundleLocation( swtFragmentSrc ); |
102 |
|
103 |
if (hostBundlePath != null && fragmentBundlePath != null ) { |
104 |
IAccessRule[] accessRules= {}; |
105 |
|
106 |
final IClasspathAttribute[] attributes= new IClasspathAttribute[] {}; |
107 |
IClasspathEntry[] entries = new IClasspathEntry[]{ |
108 |
JavaCore.newLibraryEntry(hostBundlePath, null, null, accessRules, attributes, false), |
109 |
JavaCore.newLibraryEntry(fragmentBundlePath, fragmentBundleSrcPath, null, accessRules, attributes, false) |
110 |
}; |
111 |
return entries; |
112 |
} |
113 |
return null; |
114 |
} |
115 |
|
116 |
private static IPath getBundleLocation(Bundle bundle) { |
117 |
if (bundle == null) |
118 |
return null; |
119 |
|
120 |
URL local= null; |
121 |
IPath path; |
122 |
try { |
123 |
local= FileLocator.resolve(bundle.getEntry("/")); //$NON-NLS-1$ |
124 |
path= getFilePath(local); |
125 |
} catch (MalformedURLException e) { |
126 |
return null; |
127 |
} catch (IOException e) { |
128 |
return null; |
129 |
} |
130 |
return path; |
131 |
} |
132 |
|
133 |
private static Path getFilePath(URL local) throws MalformedURLException { |
134 |
if (local.getProtocol().equals("file")) { //$NON-NLS-1$ |
135 |
return new Path(local.getFile()); |
136 |
} else if (local.getProtocol().equals("jar")) { //$NON-NLS-1$ |
137 |
String f= local.getFile(); |
138 |
int idx= f.lastIndexOf('!'); |
139 |
if (idx >= 0) { |
140 |
f= f.substring(0, idx); |
141 |
} |
142 |
return getFilePath(new URL(f)); |
143 |
} |
144 |
return null; |
145 |
} |
146 |
|
147 |
private static boolean isValidSWTContainerPath(IPath path) { |
148 |
return path != null && path.segmentCount() == 1 && SWT_CONTAINER_ID.equals(path.segment(0)); |
149 |
} |
150 |
|
151 |
} |