View | Details | Raw Unified | Return to bug 240705
Collapse All | Expand All

(-)plugin.xml (+15 lines)
Lines 6314-6318 Link Here
6314
         </colorOverride>
6314
         </colorOverride>
6315
      </theme>
6315
      </theme>
6316
   </extension>
6316
   </extension>
6317
   <extension
6318
         point="org.eclipse.jdt.core.classpathContainerInitializer">
6319
      <classpathContainerInitializer
6320
            class="org.eclipse.jdt.internal.ui.wizards.buildpaths.SWTClasspathContainerInitializer"
6321
            id="org.eclipse.jdt.SWT_CONTAINER">
6322
      </classpathContainerInitializer>
6323
   </extension>
6324
   <extension
6325
         point="org.eclipse.jdt.ui.classpathContainerPage">
6326
      <classpathContainerPage
6327
            class="org.eclipse.jdt.internal.ui.wizards.buildpaths.SWTClasspathContainerPage"
6328
            id="org.eclipse.jdt.SWT_LIBRARY"
6329
            name="Standard Widget Library (SWT)">
6330
      </classpathContainerPage>
6331
   </extension>
6317
6332
6318
</plugin>
6333
</plugin>
(-)ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/SWTClasspathContainerInitializer.java (+151 lines)
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
}
(-)ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/SWTClasspathContainerPage.java (+57 lines)
Added Link Here
1
package org.eclipse.jdt.internal.ui.wizards.buildpaths;
2
3
import org.eclipse.swt.SWT;
4
import org.eclipse.swt.widgets.Composite;
5
import org.eclipse.swt.widgets.Label;
6
7
import org.eclipse.core.runtime.Path;
8
9
import org.eclipse.jface.resource.ImageDescriptor;
10
import org.eclipse.jface.wizard.WizardPage;
11
12
import org.eclipse.jdt.core.IClasspathEntry;
13
import org.eclipse.jdt.core.JavaCore;
14
15
import org.eclipse.jdt.ui.wizards.IClasspathContainerPage;
16
17
public class SWTClasspathContainerPage extends WizardPage implements IClasspathContainerPage {
18
19
	private IClasspathEntry fEntry;
20
	
21
	public SWTClasspathContainerPage() {
22
		super("SWTClasspathContainerPage"); //$NON-NLS-1$
23
		setTitle("Standard Widget Toolkit Library");
24
		setMessage("Select - I need a message here :)");
25
	}
26
27
	public SWTClasspathContainerPage(String pageName, String title, ImageDescriptor titleImage) {
28
		super(pageName, title, titleImage);
29
	}
30
31
	public boolean finish() {
32
		if (fEntry == null) { // new entry
33
			fEntry= JavaCore.newContainerEntry(new Path(SWTClasspathContainerInitializer.SWT_CONTAINER_ID));
34
		}
35
		return true;
36
	}
37
38
	public IClasspathEntry getSelection() {
39
		return fEntry;
40
	}
41
42
	public void setSelection(IClasspathEntry containerEntry) {
43
		fEntry= containerEntry;
44
	}
45
46
	public void createControl(Composite parent) {
47
		Label label= new Label(parent, SWT.NONE);
48
		if (fEntry == null) {
49
			label.setText("Nothing to configure. Press 'Finish' to add new entry"); //$NON-NLS-1$
50
		} else {
51
			label.setText("Nothing to configure."); //$NON-NLS-1$
52
			setPageComplete(false);
53
		}
54
		setControl(label);
55
	}
56
57
}

Return to bug 240705