Skip to content

Commit ef0e0d2

Browse files
committed
Merge pull request #76 from adangel:issue-72-javafx-version
Correctly determine JavaFX version when embedded #76
2 parents 17e7f25 + f149736 commit ef0e0d2

File tree

4 files changed

+106
-53
lines changed

4 files changed

+106
-53
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#61](https://github.com/pmd/pmd-designer/issues/61) Remove dependency to jcommander
1010
* [#62](https://github.com/pmd/pmd-designer/issues/62) Exceptions and errors are not always logged
1111
* [#63](https://github.com/pmd/pmd-designer/issues/63) Update to PMD 7.0.0-rc3
12+
* [#72](https://github.com/pmd/pmd-designer/issues/72) NPE when launching Designer
1213
* [#73](https://github.com/pmd/pmd-designer/issues/73) Remove commons-io dependency
1314

1415
**Merged pull requests:**
@@ -18,6 +19,7 @@
1819
* [#70](https://github.com/pmd/pmd-designer/pull/70) Fix drag and drop for tests case violations by [@adangel](https://github.com/adangel)
1920
* [#74](https://github.com/pmd/pmd-designer/pull/74) Remove commons-io by [@adangel](https://github.com/adangel)
2021
* [#75](https://github.com/pmd/pmd-designer/pull/75) Update to PMD 7.0.0-SNAPSHOT (upcoming 7.0.0-rc4) by [@adangel](https://github.com/adangel)
22+
* [#76](https://github.com/pmd/pmd-designer/pull/76) Correctly determine JavaFX version when embedded by [@adangel](https://github.com/adangel)
2123

2224
See https://github.com/pmd/pmd-designer/milestone/11
2325

src/main/java/net/sourceforge/pmd/util/fxdesigner/DesignerStarter.java

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
package net.sourceforge.pmd.util.fxdesigner;
66

7-
import java.io.IOException;
8-
import java.io.InputStream;
9-
import java.util.Properties;
10-
import javax.swing.JOptionPane;
7+
import static net.sourceforge.pmd.util.fxdesigner.util.JavaFxUtil.isCompatibleJavaFxVersion;
8+
import static net.sourceforge.pmd.util.fxdesigner.util.JavaFxUtil.isJavaFxAvailable;
9+
import static net.sourceforge.pmd.util.fxdesigner.util.JavaFxUtil.setSystemProperties;
1110

12-
import org.apache.commons.lang3.SystemUtils;
11+
import javax.swing.JOptionPane;
1312

1413
import net.sourceforge.pmd.annotation.InternalApi;
1514

@@ -30,20 +29,9 @@ public final class DesignerStarter {
3029
+ " Please install the latest JavaFX on your system and try again." + System.lineSeparator()
3130
+ " See https://gluonhq.com/products/javafx/";
3231

33-
private static final int MIN_JAVAFX_VERSION_ON_MAC_OSX = 14;
34-
3532
private DesignerStarter() {
3633
}
3734

38-
private static boolean isJavaFxAvailable() {
39-
try {
40-
DesignerStarter.class.getClassLoader().loadClass("javafx.application.Application");
41-
return true;
42-
} catch (ClassNotFoundException | LinkageError e) {
43-
return false;
44-
}
45-
}
46-
4735
/**
4836
* Starting from PMD 7.0.0 this method usage will be limited for development.
4937
* CLI support will be provided by pmd-cli
@@ -54,41 +42,6 @@ public static void main(String[] args) {
5442
System.exit(ret.getCode());
5543
}
5644

57-
private static void setSystemProperties() {
58-
if (SystemUtils.IS_OS_LINUX) {
59-
// On Linux, JavaFX renders text poorly by default. These settings help to alleviate the problems.
60-
System.setProperty("prism.text", "t2k");
61-
System.setProperty("prism.lcdtext", "true");
62-
}
63-
}
64-
65-
private static boolean isCompatibleJavaFxVersion() {
66-
if (SystemUtils.IS_OS_MAC_OSX) {
67-
final String javaFxVersion = getJavaFxVersion();
68-
if (javaFxVersion != null) {
69-
final int major = Integer.parseInt(javaFxVersion.split("\\.")[0]);
70-
if (major < MIN_JAVAFX_VERSION_ON_MAC_OSX) {
71-
// Prior to JavaFx 14, text on Mac OSX was garbled and unreadable
72-
return false;
73-
}
74-
}
75-
}
76-
77-
return true;
78-
}
79-
80-
private static String getJavaFxVersion() {
81-
try (InputStream is = DesignerStarter.class.getClassLoader().getResourceAsStream("javafx.properties")) {
82-
final Properties javaFxProperties = new Properties();
83-
javaFxProperties.load(is);
84-
return (String) javaFxProperties.get("javafx.version");
85-
} catch (IOException ignored) {
86-
// Can't determine the version
87-
}
88-
89-
return null;
90-
}
91-
9245
@SuppressWarnings("PMD.AvoidCatchingThrowable")
9346
public static ExitStatus launchGui(String[] args) {
9447
setSystemProperties();

src/main/java/net/sourceforge/pmd/util/fxdesigner/popups/SimplePopups.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRoot;
2424
import net.sourceforge.pmd.util.fxdesigner.util.AuxLanguageRegistry;
2525
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
26+
import net.sourceforge.pmd.util.fxdesigner.util.JavaFxUtil;
2627
import net.sourceforge.pmd.util.fxdesigner.util.ResourceUtil;
2728

2829
import javafx.animation.Animation;
@@ -207,14 +208,17 @@ public static void showAboutPopup(DesignerRoot root) {
207208
TextArea textArea = new TextArea();
208209

209210
String sb =
210-
"PMD core version:\t\t\t" + PMDVersion.VERSION + "\n"
211+
"PMD core version:\t\t" + PMDVersion.VERSION + "\n"
211212
+ "Designer version:\t\t\t" + DesignerVersion.getCurrentVersion()
212213
+ " (supports PMD core " + DesignerVersion.getPmdCoreMinVersion() + ")\n"
213214
+ "Designer settings dir:\t\t"
214215
+ root.getService(DesignerRoot.DISK_MANAGER).getSettingsDirectory() + "\n"
215216
+ "Available languages:\t\t"
216217
+ AuxLanguageRegistry.getSupportedLanguages().map(Language::getTerseName).collect(Collectors.toList())
217-
+ "\n";
218+
+ "\n"
219+
+ "\n"
220+
+ "Java Version:\t\t\t\t" + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")\n"
221+
+ "JavaFX Version:\t\t\t" + JavaFxUtil.getJavaFxVersion() + "\n";
218222

219223
textArea.setText(sb);
220224
scroll.setContent(textArea);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
package net.sourceforge.pmd.util.fxdesigner.util;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.net.URISyntaxException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.Properties;
14+
15+
import org.apache.commons.lang3.SystemUtils;
16+
17+
import javafx.application.Application;
18+
19+
public final class JavaFxUtil {
20+
private static final int MIN_JAVAFX_VERSION_ON_MAC_OSX = 14;
21+
22+
private JavaFxUtil() {
23+
}
24+
25+
public static boolean isJavaFxAvailable() {
26+
try {
27+
JavaFxUtil.class.getClassLoader().loadClass("javafx.application.Application");
28+
return true;
29+
} catch (ClassNotFoundException | LinkageError e) {
30+
return false;
31+
}
32+
}
33+
34+
public static String getJavaFxVersion() {
35+
InputStream javafxProperties = JavaFxUtil.class.getClassLoader().getResourceAsStream("javafx.properties");
36+
37+
if (javafxProperties == null) {
38+
try {
39+
Path path = Paths.get(Application.class.getProtectionDomain().getCodeSource().getLocation().toURI());
40+
Path propertiesFile1 = path.resolveSibling("javafx.properties");
41+
Path propertiesFile2 = path.getParent().getParent().resolve("javafx.properties");
42+
if (Files.exists(propertiesFile1)) {
43+
javafxProperties = Files.newInputStream(propertiesFile1);
44+
} else if (Files.exists(propertiesFile2)) {
45+
javafxProperties = Files.newInputStream(propertiesFile2);
46+
}
47+
} catch (URISyntaxException | IOException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
51+
52+
if (javafxProperties != null) {
53+
try (InputStream is = javafxProperties) {
54+
final Properties javaFxProperties = new Properties();
55+
javaFxProperties.load(is);
56+
String javaFxVersion = javaFxProperties.getProperty("javafx.version");
57+
if (javaFxVersion != null) {
58+
return javaFxVersion;
59+
}
60+
return javaFxProperties.getProperty("javafx.runtime.version");
61+
} catch (IOException ignored) {
62+
// Can't determine the version
63+
System.err.println("Can't determine javafx version: " + ignored);
64+
}
65+
} else {
66+
System.err.println("Can't determine javafx version: javafx.properties not found in classpath.");
67+
}
68+
return null;
69+
}
70+
71+
public static void setSystemProperties() {
72+
if (SystemUtils.IS_OS_LINUX) {
73+
// On Linux, JavaFX renders text poorly by default. These settings help to alleviate the problems.
74+
System.setProperty("prism.text", "t2k");
75+
System.setProperty("prism.lcdtext", "true");
76+
}
77+
}
78+
79+
80+
public static boolean isCompatibleJavaFxVersion() {
81+
if (SystemUtils.IS_OS_MAC_OSX) {
82+
final String javaFxVersion = getJavaFxVersion();
83+
if (javaFxVersion != null) {
84+
final int major = Integer.parseInt(javaFxVersion.split("\\.")[0]);
85+
if (major < MIN_JAVAFX_VERSION_ON_MAC_OSX) {
86+
// Prior to JavaFx 14, text on Mac OSX was garbled and unreadable
87+
return false;
88+
}
89+
}
90+
}
91+
92+
return true;
93+
}
94+
}

0 commit comments

Comments
 (0)