Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly determine JavaFX version when embedded #76

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#61](https://github.com/pmd/pmd-designer/issues/61) Remove dependency to jcommander
* [#62](https://github.com/pmd/pmd-designer/issues/62) Exceptions and errors are not always logged
* [#63](https://github.com/pmd/pmd-designer/issues/63) Update to PMD 7.0.0-rc3
* [#72](https://github.com/pmd/pmd-designer/issues/72) NPE when launching Designer

**Merged pull requests:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

package net.sourceforge.pmd.util.fxdesigner;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.swing.JOptionPane;
import static net.sourceforge.pmd.util.fxdesigner.util.JavaFxUtil.isCompatibleJavaFxVersion;
import static net.sourceforge.pmd.util.fxdesigner.util.JavaFxUtil.isJavaFxAvailable;
import static net.sourceforge.pmd.util.fxdesigner.util.JavaFxUtil.setSystemProperties;

import org.apache.commons.lang3.SystemUtils;
import javax.swing.JOptionPane;

import net.sourceforge.pmd.annotation.InternalApi;

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

private static final int MIN_JAVAFX_VERSION_ON_MAC_OSX = 14;

private DesignerStarter() {
}

private static boolean isJavaFxAvailable() {
try {
DesignerStarter.class.getClassLoader().loadClass("javafx.application.Application");
return true;
} catch (ClassNotFoundException | LinkageError e) {
return false;
}
}

/**
* Starting from PMD 7.0.0 this method usage will be limited for development.
* CLI support will be provided by pmd-cli
Expand All @@ -54,41 +42,6 @@ public static void main(String[] args) {
System.exit(ret.getCode());
}

private static void setSystemProperties() {
if (SystemUtils.IS_OS_LINUX) {
// On Linux, JavaFX renders text poorly by default. These settings help to alleviate the problems.
System.setProperty("prism.text", "t2k");
System.setProperty("prism.lcdtext", "true");
}
}

private static boolean isCompatibleJavaFxVersion() {
if (SystemUtils.IS_OS_MAC_OSX) {
final String javaFxVersion = getJavaFxVersion();
if (javaFxVersion != null) {
final int major = Integer.parseInt(javaFxVersion.split("\\.")[0]);
if (major < MIN_JAVAFX_VERSION_ON_MAC_OSX) {
// Prior to JavaFx 14, text on Mac OSX was garbled and unreadable
return false;
}
}
}

return true;
}

private static String getJavaFxVersion() {
try (InputStream is = DesignerStarter.class.getClassLoader().getResourceAsStream("javafx.properties")) {
final Properties javaFxProperties = new Properties();
javaFxProperties.load(is);
return (String) javaFxProperties.get("javafx.version");
} catch (IOException ignored) {
// Can't determine the version
}

return null;
}

@SuppressWarnings("PMD.AvoidCatchingThrowable")
public static ExitStatus launchGui(String[] args) {
setSystemProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRoot;
import net.sourceforge.pmd.util.fxdesigner.util.AuxLanguageRegistry;
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
import net.sourceforge.pmd.util.fxdesigner.util.JavaFxUtil;

import javafx.animation.Animation;
import javafx.animation.Interpolator;
Expand Down Expand Up @@ -212,14 +213,17 @@ public static void showAboutPopup(DesignerRoot root) {
TextArea textArea = new TextArea();

String sb =
"PMD core version:\t\t\t" + PMDVersion.VERSION + "\n"
"PMD core version:\t\t" + PMDVersion.VERSION + "\n"
+ "Designer version:\t\t\t" + DesignerVersion.getCurrentVersion()
+ " (supports PMD core " + DesignerVersion.getPmdCoreMinVersion() + ")\n"
+ "Designer settings dir:\t\t"
+ root.getService(DesignerRoot.DISK_MANAGER).getSettingsDirectory() + "\n"
+ "Available languages:\t\t"
+ AuxLanguageRegistry.getSupportedLanguages().map(Language::getTerseName).collect(Collectors.toList())
+ "\n";
+ "\n"
+ "\n"
+ "Java Version:\t\t\t\t" + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")\n"
+ "JavaFX Version:\t\t\t" + JavaFxUtil.getJavaFxVersion() + "\n";

textArea.setText(sb);
scroll.setContent(textArea);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

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

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import org.apache.commons.lang3.SystemUtils;

import javafx.application.Application;

public final class JavaFxUtil {
private static final int MIN_JAVAFX_VERSION_ON_MAC_OSX = 14;

private JavaFxUtil() {
}

public static boolean isJavaFxAvailable() {
try {
JavaFxUtil.class.getClassLoader().loadClass("javafx.application.Application");
return true;
} catch (ClassNotFoundException | LinkageError e) {
return false;
}
}

public static String getJavaFxVersion() {
InputStream javafxProperties = JavaFxUtil.class.getClassLoader().getResourceAsStream("javafx.properties");

if (javafxProperties == null) {
try {
Path path = Paths.get(Application.class.getProtectionDomain().getCodeSource().getLocation().toURI());
Path propertiesFile1 = path.resolveSibling("javafx.properties");
Path propertiesFile2 = path.getParent().getParent().resolve("javafx.properties");
if (Files.exists(propertiesFile1)) {
javafxProperties = Files.newInputStream(propertiesFile1);
} else if (Files.exists(propertiesFile2)) {
javafxProperties = Files.newInputStream(propertiesFile2);
}
} catch (URISyntaxException | IOException e) {
throw new RuntimeException(e);
}
}

if (javafxProperties != null) {
try (InputStream is = javafxProperties) {
final Properties javaFxProperties = new Properties();
javaFxProperties.load(is);
String javaFxVersion = javaFxProperties.getProperty("javafx.version");
if (javaFxVersion != null) {
return javaFxVersion;
}
return javaFxProperties.getProperty("javafx.runtime.version");
} catch (IOException ignored) {
// Can't determine the version
System.err.println("Can't determine javafx version: " + ignored);
}
} else {
System.err.println("Can't determine javafx version: javafx.properties not found in classpath.");
}
return null;
}

public static void setSystemProperties() {
if (SystemUtils.IS_OS_LINUX) {
// On Linux, JavaFX renders text poorly by default. These settings help to alleviate the problems.
System.setProperty("prism.text", "t2k");
System.setProperty("prism.lcdtext", "true");
}
}


public static boolean isCompatibleJavaFxVersion() {
if (SystemUtils.IS_OS_MAC_OSX) {
final String javaFxVersion = getJavaFxVersion();
if (javaFxVersion != null) {
final int major = Integer.parseInt(javaFxVersion.split("\\.")[0]);
if (major < MIN_JAVAFX_VERSION_ON_MAC_OSX) {
// Prior to JavaFx 14, text on Mac OSX was garbled and unreadable
return false;
}
}
}

return true;
}
}