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
+ }
0 commit comments