Skip to content

Commit 6cd4d63

Browse files
committed
Add JavaFX support
Signed-off-by: Fred Bricon <fbricon@gmail.com>
1 parent e69383c commit 6cd4d63

File tree

11 files changed

+625
-0
lines changed

11 files changed

+625
-0
lines changed

org.eclipse.jdt.ls.core/plugin.xml

+13
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,17 @@
146146
class="org.eclipse.jdt.ls.core.internal.corext.refactoring.ChangeMethodSignatureRefactoringContribution"
147147
id="org.eclipse.jdt.ls.change.method.signature"/>
148148
</extension>
149+
<extension
150+
point="org.eclipse.jdt.launching.executionEnvironments">
151+
<ruleParticipant
152+
class="org.eclipse.jdt.ls.core.internal.javafx.FXAccessRuleParticipant"
153+
id="org.eclipse.jdt.ls.core.fxrule">
154+
</ruleParticipant>
155+
</extension>
156+
<extension
157+
point="org.eclipse.jdt.launching.libraryLocationResolvers">
158+
<resolver
159+
class="org.eclipse.jdt.ls.core.internal.javafx.FXLibraryLocationResolver">
160+
</resolver>
161+
</extension>
149162
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2012-2020 BestSolution.at and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.jdt.ls.core.internal.javafx;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.nio.file.Files;
18+
import java.nio.file.Paths;
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
24+
import org.eclipse.core.runtime.CoreException;
25+
import org.eclipse.core.runtime.IPath;
26+
import org.eclipse.core.runtime.Path;
27+
import org.eclipse.core.runtime.preferences.InstanceScope;
28+
import org.eclipse.jdt.core.IAccessRule;
29+
import org.eclipse.jdt.core.IClasspathAttribute;
30+
import org.eclipse.jdt.core.IClasspathEntry;
31+
import org.eclipse.jdt.core.IJavaProject;
32+
import org.eclipse.jdt.core.JavaCore;
33+
import org.eclipse.jdt.launching.IVMInstall;
34+
import org.eclipse.jdt.launching.JavaRuntime;
35+
import org.eclipse.jdt.launching.LibraryLocation;
36+
37+
/**
38+
*
39+
* Copied from
40+
* https://github.com/eclipse-efx/efxclipse-eclipse/blob/master/bundles/tooling/org.eclipse.fx.ide.jdt.core/src/org/eclipse/fx/ide/jdt/core/internal/BuildPathSupport.java
41+
*
42+
*/
43+
public class BuildPathSupport {
44+
public static final String WEB_JAVADOC_LOCATION = "http://docs.oracle.com/javase/8/javafx/api/";
45+
46+
public static List<IClasspathEntry> getJavaFXLibraryEntry(IJavaProject project) {
47+
FXVersion version = getFXVersion(project);
48+
if( version == FXVersion.FX2 || version == FXVersion.FX8) {
49+
IPath[] paths = getFxJarPath(project);
50+
List<IClasspathEntry> rv = new ArrayList<>();
51+
if( paths != null ) {
52+
53+
IPath jarLocationPath = paths[0];
54+
IPath javadocLocation = paths[1];
55+
IPath fxSource = paths[3];
56+
57+
IClasspathAttribute[] attributes;
58+
IAccessRule[] accessRules= { };
59+
if (javadocLocation == null || !javadocLocation.toFile().exists()) {
60+
attributes= new IClasspathAttribute[] { JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, WEB_JAVADOC_LOCATION) };
61+
} else {
62+
attributes= new IClasspathAttribute[] { JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadocLocation.toFile().toURI().toString()) };
63+
}
64+
65+
if( jarLocationPath.toFile().exists() ) {
66+
rv.add(JavaCore.newLibraryEntry(jarLocationPath, fxSource, null, accessRules, attributes, false));
67+
}
68+
}
69+
70+
return rv;
71+
} else if( version == FXVersion.FX11 || version == FXVersion.FX11PLUS ) {
72+
String sdkPath = InstanceScope.INSTANCE.getNode("org.eclipse.fx.ide.ui").get("javafx-sdk", null);
73+
List<IClasspathEntry> entries = new ArrayList<>();
74+
if( sdkPath != null ) {
75+
java.nio.file.Path path = Paths.get(sdkPath);
76+
if( Files.exists(path) ) {
77+
try {
78+
79+
entries.addAll(Files.list(path).filter( p -> p.getFileName().toString().endsWith(".jar")).map( p -> {
80+
IClasspathAttribute moduleAttr = JavaCore.newClasspathAttribute(IClasspathAttribute.MODULE, "true"); //$NON-NLS-1$
81+
return JavaCore.newLibraryEntry(
82+
new Path(p.toAbsolutePath().toString()),
83+
new Path(p.getParent().resolve("src.zip").toAbsolutePath().toString()),
84+
new Path("."),
85+
new IAccessRule[]{
86+
JavaCore.newAccessRule(new Path("javafx/*"),IAccessRule.K_ACCESSIBLE),
87+
JavaCore.newAccessRule(new Path("com/sun/*"),IAccessRule.K_DISCOURAGED),
88+
JavaCore.newAccessRule(new Path("netscape/javascript/*"),IAccessRule.K_DISCOURAGED)},
89+
new IClasspathAttribute[] { moduleAttr },
90+
false);
91+
}).collect(Collectors.toList()));
92+
} catch (IOException e) {
93+
throw new IllegalStateException();
94+
}
95+
}
96+
}
97+
return entries;
98+
}
99+
return Collections.emptyList();
100+
}
101+
102+
public static FXVersion getFXVersion(IJavaProject project) {
103+
try {
104+
IVMInstall i = JavaRuntime.getVMInstall(project);
105+
if( i == null ) {
106+
i = JavaRuntime.getDefaultVMInstall();
107+
}
108+
109+
return FXVersionUtil.getFxVersion(i);
110+
} catch (CoreException e) {
111+
throw new IllegalStateException(e);
112+
}
113+
}
114+
115+
public static IPath[] getFxJarPath(IJavaProject project) {
116+
IPath jarLocationPath = null;
117+
IPath javadocLocation = null;
118+
IPath antJarLocationPath = null;
119+
IPath sourceLocationPath = null;
120+
121+
try {
122+
IVMInstall i = JavaRuntime.getVMInstall(project);
123+
if( i == null ) {
124+
i = JavaRuntime.getDefaultVMInstall();
125+
}
126+
127+
if( FXVersionUtil.getFxVersion(i) != FXVersion.FX9 ) {
128+
return getFxJarPath(i);
129+
}
130+
return null;
131+
} catch (CoreException e) {
132+
// TODO Auto-generated catch block
133+
e.printStackTrace();
134+
}
135+
136+
return new IPath[] { jarLocationPath, javadocLocation, antJarLocationPath, sourceLocationPath };
137+
}
138+
139+
140+
public static IPath[] getSwtFxJarPath(IVMInstall i) {
141+
File installDir = i.getInstallLocation();
142+
IPath[] checkPaths = {
143+
// JDK 8
144+
new Path(installDir.getAbsolutePath()).append("jre").append("lib").append("jfxswt.jar"),
145+
new Path(installDir.getAbsolutePath()).append("lib").append("jfxswt.jar"), // JRE
146+
new Path(installDir.getAbsolutePath()).append("lib").append("javafx-swt.jar")
147+
};
148+
149+
IPath jarLocationPath = null;
150+
IPath javadocLocation = null;
151+
IPath sourceLocationPath = null;
152+
153+
jarLocationPath = checkPaths[0];
154+
155+
if( ! jarLocationPath.toFile().exists() ) {
156+
for( IPath p : checkPaths ) {
157+
if( p.toFile().exists() ) {
158+
jarLocationPath = p;
159+
break;
160+
}
161+
}
162+
}
163+
164+
if( jarLocationPath.toFile().exists() ) {
165+
sourceLocationPath = new Path(installDir.getAbsolutePath()).append("javafx-src.zip");
166+
167+
return new IPath[] { jarLocationPath, javadocLocation, sourceLocationPath };
168+
}
169+
170+
return null;
171+
}
172+
173+
public static IPath[] getFxJarPath(IVMInstall i) {
174+
for( LibraryLocation l : JavaRuntime.getLibraryLocations(i) ) {
175+
if( "jfxrt.jar".equals(l.getSystemLibraryPath().lastSegment()) ) {
176+
return null;
177+
}
178+
}
179+
180+
IPath jarLocationPath = null;
181+
IPath javadocLocation = null;
182+
IPath antJarLocationPath = null;
183+
IPath sourceLocationPath = null;
184+
185+
File installDir = i.getInstallLocation();
186+
187+
IPath[] checkPaths = {
188+
// Java 7
189+
new Path(installDir.getAbsolutePath()).append("jre").append("lib").append("jfxrt.jar"),
190+
new Path(installDir.getAbsolutePath()).append("lib").append("jfxrt.jar") // JRE
191+
192+
};
193+
194+
jarLocationPath = checkPaths[0];
195+
196+
if( ! jarLocationPath.toFile().exists() ) {
197+
for( IPath p : checkPaths ) {
198+
if( p.toFile().exists() ) {
199+
jarLocationPath = p;
200+
break;
201+
}
202+
}
203+
}
204+
205+
if( ! jarLocationPath.toFile().exists() ) {
206+
StringBuilder error = new StringBuilder("Unable to detect JavaFX jar for JRE ").append(i.getName());
207+
error.append(System.lineSeparator()).append(" JRE: ").append(installDir.getAbsolutePath());
208+
error.append(System.lineSeparator()).append(" Checked paths:");
209+
for( IPath p : checkPaths ) {
210+
error.append(System.lineSeparator()).append(" " + p.toFile().getAbsolutePath());
211+
}
212+
213+
return null;
214+
}
215+
216+
javadocLocation = new Path(installDir.getParentFile().getAbsolutePath()).append("docs").append("api"); //TODO Not shipped yet
217+
if( ! javadocLocation.toFile().exists() ) {
218+
IPath p = new Path(System.getProperty("user.home")).append("javafx-api-"+ i.getName()).append("docs").append("api");
219+
if( p.toFile().exists() ) {
220+
javadocLocation = p;
221+
}
222+
}
223+
224+
antJarLocationPath = new Path(installDir.getParent()).append("lib").append("ant-javafx.jar");
225+
226+
sourceLocationPath = new Path(installDir.getAbsolutePath()).append("javafx-src.zip");
227+
228+
if( ! sourceLocationPath.toFile().exists() ) {
229+
sourceLocationPath = null;
230+
}
231+
232+
return new IPath[] { jarLocationPath, javadocLocation, antJarLocationPath, sourceLocationPath };
233+
}
234+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2013-2020 BestSolution.at and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.jdt.ls.core.internal.javafx;
14+
15+
import org.eclipse.core.runtime.Path;
16+
import org.eclipse.jdt.core.IAccessRule;
17+
import org.eclipse.jdt.core.IJavaProject;
18+
import org.eclipse.jdt.core.JavaCore;
19+
import org.eclipse.jdt.launching.IVMInstall;
20+
import org.eclipse.jdt.launching.LibraryLocation;
21+
import org.eclipse.jdt.launching.environments.IAccessRuleParticipant;
22+
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
23+
24+
/**
25+
*
26+
* Copied from
27+
* https://github.com/eclipse-efx/efxclipse-eclipse/blob/master/bundles/tooling/org.eclipse.fx.ide.jdt.core/src/org/eclipse/fx/ide/jdt/core/internal/FXAccessRuleParticipant.java
28+
*
29+
*/
30+
public class FXAccessRuleParticipant implements IAccessRuleParticipant {
31+
32+
@Override
33+
public IAccessRule[][] getAccessRules(IExecutionEnvironment environment, IVMInstall vm, LibraryLocation[] libraries, IJavaProject project) {
34+
boolean fxOnExtPath = false;
35+
for( LibraryLocation loc : libraries ) {
36+
if( "jfxrt.jar".equals(loc.getSystemLibraryPath().lastSegment()) ) {
37+
fxOnExtPath = true;
38+
break;
39+
}
40+
}
41+
42+
if( fxOnExtPath ) {
43+
IAccessRule[] rules = new IAccessRule[6];
44+
rules[0] = JavaCore.newAccessRule(new Path("javafx/**"), IAccessRule.K_ACCESSIBLE);
45+
rules[1] = JavaCore.newAccessRule(new Path("netscape/javascript/**"), IAccessRule.K_ACCESSIBLE);
46+
rules[2] = JavaCore.newAccessRule(new Path("com/sun/javafx/**"), IAccessRule.K_DISCOURAGED);
47+
rules[3] = JavaCore.newAccessRule(new Path("com/sun/glass/**"), IAccessRule.K_DISCOURAGED);
48+
rules[4] = JavaCore.newAccessRule(new Path("com/sun/media/jfxmedia/**"), IAccessRule.K_DISCOURAGED);
49+
rules[5] = JavaCore.newAccessRule(new Path("com/sun/prism/**"), IAccessRule.K_DISCOURAGED);
50+
51+
IAccessRule[][] rv = new IAccessRule[libraries.length][];
52+
53+
for( int i = 0; i < rv.length; i++ ) {
54+
rv[i] = rules;
55+
}
56+
57+
return rv;
58+
} else if( FXVersionUtil.getFxVersion(vm) == FXVersion.FX9 ) {
59+
IAccessRule[] rules = new IAccessRule[2];
60+
rules[0] = JavaCore.newAccessRule(new Path("javafx/**"), IAccessRule.K_ACCESSIBLE);
61+
rules[1] = JavaCore.newAccessRule(new Path("netscape/javascript/**"), IAccessRule.K_ACCESSIBLE);
62+
63+
IAccessRule[][] rv = new IAccessRule[libraries.length][];
64+
65+
for( int i = 0; i < rv.length; i++ ) {
66+
rv[i] = rules;
67+
}
68+
69+
return rv;
70+
}
71+
72+
return new IAccessRule[0][0];
73+
}
74+
75+
}

0 commit comments

Comments
 (0)