1
+ package org .eclipse .jdt .ls .core .internal .javafx ;
2
+
3
+ import java .io .File ;
4
+ import java .io .IOException ;
5
+ import java .nio .file .Files ;
6
+ import java .nio .file .Paths ;
7
+ import java .util .ArrayList ;
8
+ import java .util .Collections ;
9
+ import java .util .List ;
10
+ import java .util .stream .Collectors ;
11
+
12
+ import org .eclipse .core .runtime .CoreException ;
13
+ import org .eclipse .core .runtime .IPath ;
14
+ import org .eclipse .core .runtime .Path ;
15
+ import org .eclipse .core .runtime .preferences .InstanceScope ;
16
+ import org .eclipse .jdt .core .IAccessRule ;
17
+ import org .eclipse .jdt .core .IClasspathAttribute ;
18
+ import org .eclipse .jdt .core .IClasspathEntry ;
19
+ import org .eclipse .jdt .core .IJavaProject ;
20
+ import org .eclipse .jdt .core .JavaCore ;
21
+ import org .eclipse .jdt .launching .IVMInstall ;
22
+ import org .eclipse .jdt .launching .JavaRuntime ;
23
+ import org .eclipse .jdt .launching .LibraryLocation ;
24
+
25
+ /**
26
+ *
27
+ * Copied from
28
+ * 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
29
+ *
30
+ */
31
+ public class BuildPathSupport {
32
+ public static final String WEB_JAVADOC_LOCATION = "http://docs.oracle.com/javase/8/javafx/api/" ;
33
+
34
+ public static List <IClasspathEntry > getJavaFXLibraryEntry (IJavaProject project ) {
35
+ FXVersion version = getFXVersion (project );
36
+ if ( version == FXVersion .FX2 || version == FXVersion .FX8 ) {
37
+ IPath [] paths = getFxJarPath (project );
38
+ List <IClasspathEntry > rv = new ArrayList <>();
39
+ if ( paths != null ) {
40
+
41
+ IPath jarLocationPath = paths [0 ];
42
+ IPath javadocLocation = paths [1 ];
43
+ IPath fxSource = paths [3 ];
44
+
45
+ IClasspathAttribute [] attributes ;
46
+ IAccessRule [] accessRules = { };
47
+ if (javadocLocation == null || !javadocLocation .toFile ().exists ()) {
48
+ attributes = new IClasspathAttribute [] { JavaCore .newClasspathAttribute (IClasspathAttribute .JAVADOC_LOCATION_ATTRIBUTE_NAME , WEB_JAVADOC_LOCATION ) };
49
+ } else {
50
+ attributes = new IClasspathAttribute [] { JavaCore .newClasspathAttribute (IClasspathAttribute .JAVADOC_LOCATION_ATTRIBUTE_NAME , javadocLocation .toFile ().toURI ().toString ()) };
51
+ }
52
+
53
+ if ( jarLocationPath .toFile ().exists () ) {
54
+ rv .add (JavaCore .newLibraryEntry (jarLocationPath , fxSource , null , accessRules , attributes , false ));
55
+ }
56
+ }
57
+
58
+ return rv ;
59
+ } else if ( version == FXVersion .FX11 || version == FXVersion .FX11PLUS ) {
60
+ String sdkPath = InstanceScope .INSTANCE .getNode ("org.eclipse.fx.ide.ui" ).get ("javafx-sdk" , null );
61
+ List <IClasspathEntry > entries = new ArrayList <>();
62
+ if ( sdkPath != null ) {
63
+ java .nio .file .Path path = Paths .get (sdkPath );
64
+ if ( Files .exists (path ) ) {
65
+ try {
66
+
67
+ entries .addAll (Files .list (path ).filter ( p -> p .getFileName ().toString ().endsWith (".jar" )).map ( p -> {
68
+ IClasspathAttribute moduleAttr = JavaCore .newClasspathAttribute (IClasspathAttribute .MODULE , "true" ); //$NON-NLS-1$
69
+ return JavaCore .newLibraryEntry (
70
+ new Path (p .toAbsolutePath ().toString ()),
71
+ new Path (p .getParent ().resolve ("src.zip" ).toAbsolutePath ().toString ()),
72
+ new Path ("." ),
73
+ new IAccessRule []{
74
+ JavaCore .newAccessRule (new Path ("javafx/*" ),IAccessRule .K_ACCESSIBLE ),
75
+ JavaCore .newAccessRule (new Path ("com/sun/*" ),IAccessRule .K_DISCOURAGED ),
76
+ JavaCore .newAccessRule (new Path ("netscape/javascript/*" ),IAccessRule .K_DISCOURAGED )},
77
+ new IClasspathAttribute [] { moduleAttr },
78
+ false );
79
+ }).collect (Collectors .toList ()));
80
+ } catch (IOException e ) {
81
+ throw new IllegalStateException ();
82
+ }
83
+ }
84
+ }
85
+ return entries ;
86
+ }
87
+ return Collections .emptyList ();
88
+ }
89
+
90
+ public static FXVersion getFXVersion (IJavaProject project ) {
91
+ try {
92
+ IVMInstall i = JavaRuntime .getVMInstall (project );
93
+ if ( i == null ) {
94
+ i = JavaRuntime .getDefaultVMInstall ();
95
+ }
96
+
97
+ return FXVersionUtil .getFxVersion (i );
98
+ } catch (CoreException e ) {
99
+ throw new IllegalStateException (e );
100
+ }
101
+ }
102
+
103
+ public static IPath [] getFxJarPath (IJavaProject project ) {
104
+ IPath jarLocationPath = null ;
105
+ IPath javadocLocation = null ;
106
+ IPath antJarLocationPath = null ;
107
+ IPath sourceLocationPath = null ;
108
+
109
+ try {
110
+ IVMInstall i = JavaRuntime .getVMInstall (project );
111
+ if ( i == null ) {
112
+ i = JavaRuntime .getDefaultVMInstall ();
113
+ }
114
+
115
+ if ( FXVersionUtil .getFxVersion (i ) != FXVersion .FX9 ) {
116
+ return getFxJarPath (i );
117
+ }
118
+ return null ;
119
+ } catch (CoreException e ) {
120
+ // TODO Auto-generated catch block
121
+ e .printStackTrace ();
122
+ }
123
+
124
+ return new IPath [] { jarLocationPath , javadocLocation , antJarLocationPath , sourceLocationPath };
125
+ }
126
+
127
+
128
+ public static IPath [] getSwtFxJarPath (IVMInstall i ) {
129
+ File installDir = i .getInstallLocation ();
130
+ IPath [] checkPaths = {
131
+ // JDK 8
132
+ new Path (installDir .getAbsolutePath ()).append ("jre" ).append ("lib" ).append ("jfxswt.jar" ),
133
+ new Path (installDir .getAbsolutePath ()).append ("lib" ).append ("jfxswt.jar" ), // JRE
134
+ new Path (installDir .getAbsolutePath ()).append ("lib" ).append ("javafx-swt.jar" )
135
+ };
136
+
137
+ IPath jarLocationPath = null ;
138
+ IPath javadocLocation = null ;
139
+ IPath sourceLocationPath = null ;
140
+
141
+ jarLocationPath = checkPaths [0 ];
142
+
143
+ if ( ! jarLocationPath .toFile ().exists () ) {
144
+ for ( IPath p : checkPaths ) {
145
+ if ( p .toFile ().exists () ) {
146
+ jarLocationPath = p ;
147
+ break ;
148
+ }
149
+ }
150
+ }
151
+
152
+ if ( jarLocationPath .toFile ().exists () ) {
153
+ sourceLocationPath = new Path (installDir .getAbsolutePath ()).append ("javafx-src.zip" );
154
+
155
+ return new IPath [] { jarLocationPath , javadocLocation , sourceLocationPath };
156
+ }
157
+
158
+ return null ;
159
+ }
160
+
161
+ public static IPath [] getFxJarPath (IVMInstall i ) {
162
+ for ( LibraryLocation l : JavaRuntime .getLibraryLocations (i ) ) {
163
+ if ( "jfxrt.jar" .equals (l .getSystemLibraryPath ().lastSegment ()) ) {
164
+ return null ;
165
+ }
166
+ }
167
+
168
+ IPath jarLocationPath = null ;
169
+ IPath javadocLocation = null ;
170
+ IPath antJarLocationPath = null ;
171
+ IPath sourceLocationPath = null ;
172
+
173
+ File installDir = i .getInstallLocation ();
174
+
175
+ IPath [] checkPaths = {
176
+ // Java 7
177
+ new Path (installDir .getAbsolutePath ()).append ("jre" ).append ("lib" ).append ("jfxrt.jar" ),
178
+ new Path (installDir .getAbsolutePath ()).append ("lib" ).append ("jfxrt.jar" ) // JRE
179
+
180
+ };
181
+
182
+ jarLocationPath = checkPaths [0 ];
183
+
184
+ if ( ! jarLocationPath .toFile ().exists () ) {
185
+ for ( IPath p : checkPaths ) {
186
+ if ( p .toFile ().exists () ) {
187
+ jarLocationPath = p ;
188
+ break ;
189
+ }
190
+ }
191
+ }
192
+
193
+ if ( ! jarLocationPath .toFile ().exists () ) {
194
+ StringBuilder error = new StringBuilder ("Unable to detect JavaFX jar for JRE " ).append (i .getName ());
195
+ error .append (System .lineSeparator ()).append (" JRE: " ).append (installDir .getAbsolutePath ());
196
+ error .append (System .lineSeparator ()).append (" Checked paths:" );
197
+ for ( IPath p : checkPaths ) {
198
+ error .append (System .lineSeparator ()).append (" " + p .toFile ().getAbsolutePath ());
199
+ }
200
+
201
+ return null ;
202
+ }
203
+
204
+ javadocLocation = new Path (installDir .getParentFile ().getAbsolutePath ()).append ("docs" ).append ("api" ); //TODO Not shipped yet
205
+ if ( ! javadocLocation .toFile ().exists () ) {
206
+ IPath p = new Path (System .getProperty ("user.home" )).append ("javafx-api-" + i .getName ()).append ("docs" ).append ("api" );
207
+ if ( p .toFile ().exists () ) {
208
+ javadocLocation = p ;
209
+ }
210
+ }
211
+
212
+ antJarLocationPath = new Path (installDir .getParent ()).append ("lib" ).append ("ant-javafx.jar" );
213
+
214
+ sourceLocationPath = new Path (installDir .getAbsolutePath ()).append ("javafx-src.zip" );
215
+
216
+ if ( ! sourceLocationPath .toFile ().exists () ) {
217
+ sourceLocationPath = null ;
218
+ }
219
+
220
+ return new IPath [] { jarLocationPath , javadocLocation , antJarLocationPath , sourceLocationPath };
221
+ }
222
+ }
0 commit comments