Skip to main content
summaryrefslogtreecommitdiffstats
blob: 41e273456a97b31450ab72ce48cc0e077d446a16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2012 BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ide.jdt.core.internal;

import java.io.File;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.fx.core.log.Logger;
import org.eclipse.fx.osgi.util.LoggerCreator;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.LibraryLocation;

public class BuildPathSupport {
	public static final String WEB_JAVADOC_LOCATION = "http://docs.oracle.com/javafx/2/api/";
	
	private static final Logger LOGGER = LoggerCreator.createLogger(BuildPathSupport.class);
		
	public static IClasspathEntry getJavaFXLibraryEntry(IJavaProject project) {
		IPath[] paths = getFxJarPath(project);
		if( paths != null ) {
			
			IPath jarLocationPath = paths[0];
			IPath javadocLocation = paths[1];
			IPath fxSource = paths[3];
			
			IClasspathAttribute[] attributes;
			IAccessRule[] accessRules= { };
			if (javadocLocation == null || !javadocLocation.toFile().exists()) {
				attributes= new IClasspathAttribute[] { JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, WEB_JAVADOC_LOCATION) };
			} else {
				attributes= new IClasspathAttribute[] { JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadocLocation.toFile().toURI().toString()) };
			}
			
			if( jarLocationPath.toFile().exists() ) {
				return JavaCore.newLibraryEntry(jarLocationPath, fxSource, null, accessRules, attributes, false);	
			}	
		}
		
		return null;
	}
	
	public static IPath[] getFxJarPath(IJavaProject project) {
		IPath jarLocationPath = null;
		IPath javadocLocation = null;
		IPath antJarLocationPath = null;
		IPath sourceLocationPath = null;
		
		try {
			IVMInstall i = JavaRuntime.getVMInstall(project);
			if( i == null ) {
				i = JavaRuntime.getDefaultVMInstall();
			}
			return getFxJarPath(i);
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new IPath[] { jarLocationPath, javadocLocation, antJarLocationPath, sourceLocationPath };
	}
	
	public static IPath[] getFxJarPath(IVMInstall i) {
		for( LibraryLocation l : JavaRuntime.getLibraryLocations(i) ) {
			if( "jfxrt.jar".equals(l.getSystemLibraryPath().lastSegment()) ) {
				return null;
			}
		}
		
		IPath jarLocationPath = null;
		IPath javadocLocation = null;
		IPath antJarLocationPath = null;
		IPath sourceLocationPath = null;
		
		File installDir = i.getInstallLocation();
			
		IPath[] checkPaths = {
			// Java 7
			new Path(installDir.getAbsolutePath()).append("jre").append("lib").append("jfxrt.jar"),
			new Path(installDir.getAbsolutePath()).append("lib").append("jfxrt.jar") // JRE
			
		};
		
		jarLocationPath = checkPaths[0];
				
		if( ! jarLocationPath.toFile().exists() ) {
			for( IPath p : checkPaths ) {
				if( p.toFile().exists() ) {
					jarLocationPath = p;
					break;
				}
			}	
		}
		
		if( ! jarLocationPath.toFile().exists() ) {
			LOGGER.error("Unable to detect JavaFX jar for JRE " + i.getName());
			LOGGER.error("	JRE: " + installDir.getAbsolutePath());
			LOGGER.error("	Checked paths:" );
			for( IPath p : checkPaths ) {
				LOGGER.error("		" + p.toFile().getAbsolutePath());
			}
			
			return null;
		}
		
		javadocLocation = new Path(installDir.getParentFile().getAbsolutePath()).append("docs").append("api"); //TODO Not shipped yet
		if( ! javadocLocation.toFile().exists() ) {
			IPath p = new Path(System.getProperty("user.home")).append("javafx-api-"+ i.getName()).append("docs").append("api");
			if( p.toFile().exists() ) {
				javadocLocation = p;
			}
		}
		
		antJarLocationPath = new Path(installDir.getParent()).append("lib").append("ant-javafx.jar");
		
		sourceLocationPath = new Path(installDir.getAbsolutePath()).append("javafx-src.zip");
		
		if( ! sourceLocationPath.toFile().exists() ) {
			sourceLocationPath = null;
		}
		
		return new IPath[] { jarLocationPath, javadocLocation, antJarLocationPath, sourceLocationPath };
	}
}

Back to the top