Skip to content

Commit

Permalink
Merge pull request #614 from JFormDesigner/java9-reflection
Browse files Browse the repository at this point in the history
Java 9 compatibility (using reflection)
  • Loading branch information
JordanMartinez authored Oct 20, 2017
2 parents b6b4fb6 + aaee833 commit 3e039a3
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ matrix:
- os: linux
dist: trusty
jdk: oraclejdk8
- os: linux
dist: trusty
jdk: oraclejdk9
env: _JAVA_OPTIONS="-DcheckSourceCompatibility=9"

# Headless Build: xvfb + TestFX does not work on Travis OSX environments (as of 2017-05-24)
- os: osx
Expand Down
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@ subprojects {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

// allow setting source/target compatibility from command line
// for checking Java 9+ compatibility in Travis CI
def checkSourceCompatibility = System.properties["checkSourceCompatibility"]
if (checkSourceCompatibility != null) {
sourceCompatibility = checkSourceCompatibility
targetCompatibility = checkSourceCompatibility
}

compileJava.options.deprecation = true
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.fxmisc.richtext.demo.hyperlink;

import com.sun.deploy.uitoolkit.impl.fx.HostServicesFactory;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.fxmisc.flowless.VirtualizedScrollPane;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.function.Consumer;

/**
Expand All @@ -21,7 +24,16 @@ public static void main(String[] args) {

@Override
public void start(Stage primaryStage) {
Consumer<String> showLink = HostServicesFactory.getInstance(this)::showDocument;
Consumer<String> showLink = (string) -> {
try
{
Desktop.getDesktop().browse(new URI(string));
}
catch (IOException | URISyntaxException e)
{
throw new RuntimeException(e);
}
};
TextHyperlinkArea area = new TextHyperlinkArea(showLink);

area.appendText("Some text in the area\n");
Expand Down
7 changes: 5 additions & 2 deletions richtextfx/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ dependencies {

integrationTestCompile group: 'junit', name: 'junit', version: '4.12'
integrationTestCompile group: 'com.nitorcreations', name: 'junit-runners', version: '1.2'
integrationTestCompile "org.testfx:testfx-core:4.0.6-alpha"
integrationTestCompile ("org.testfx:testfx-junit:4.0.6-alpha") {
integrationTestCompile "org.testfx:testfx-core:4.0.8-alpha"
if (org.gradle.api.JavaVersion.current().isJava9()) {
integrationTestCompile "org.testfx:testfx-internal-java9:4.0.8-alpha"
}
integrationTestCompile ("org.testfx:testfx-junit:4.0.8-alpha") {
exclude(group: "junit", module: "junit")
}
integrationTestCompile "org.testfx:openjfx-monocle:8u76-b04"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.fxmisc.richtext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.StringTokenizer;
import javafx.beans.property.ObjectProperty;
import javafx.css.StyleConverter;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;

class JavaFXCompatibility {

private static boolean isJava9orLater;

static {
try {
// Java 9 version-String Scheme: http://openjdk.java.net/jeps/223
StringTokenizer st = new StringTokenizer(System.getProperty("java.version"), "._-+");
int majorVersion = Integer.parseInt(st.nextToken());
isJava9orLater = majorVersion >= 9;
} catch (Exception e) {
// Java 8 or older
}
}

/**
* Java 8: javafx.scene.text.Text.impl_selectionFillProperty()
* Java 9+: javafx.scene.text.Text.selectionFillProperty()
*/
@SuppressWarnings("unchecked")
static ObjectProperty<Paint> Text_selectionFillProperty(Text text) {
try {
if (mText_selectionFillProperty == null) {
mText_selectionFillProperty = Text.class.getMethod(
isJava9orLater ? "selectionFillProperty" : "impl_selectionFillProperty");
}
return (ObjectProperty<Paint>) mText_selectionFillProperty.invoke(text);
} catch(NoSuchMethodException | SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
throw new Error(e);
}
}

private static Method mText_selectionFillProperty;

/**
* Java 8: com.sun.javafx.css.converters.SizeConverter.SequenceConverter.getInstance()
* Java 9+: javafx.css.converter.SizeConverter.SequenceConverter.getInstance()
*/
@SuppressWarnings("unchecked")
static StyleConverter<?, Number[]> SizeConverter_SequenceConverter_getInstance() {
try {
if (mSizeConverter_SequenceConverter_getInstance == null) {
Class<?> c = Class.forName(isJava9orLater
? "javafx.css.converter.SizeConverter$SequenceConverter"
: "com.sun.javafx.css.converters.SizeConverter$SequenceConverter");
mSizeConverter_SequenceConverter_getInstance = c.getMethod("getInstance");
}
return (StyleConverter<?, Number[]>) mSizeConverter_SequenceConverter_getInstance.invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException |
IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
throw new Error(e);
}
}

private static Method mSizeConverter_SequenceConverter_getInstance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public ObjectProperty<Paint> highlightTextFillProperty() {
TextExt t = (TextExt) n;
// XXX: binding selectionFill to textFill,
// see the note at highlightTextFill
t.impl_selectionFillProperty().bind(t.fillProperty());
JavaFXCompatibility.Text_selectionFillProperty(t).bind(t.fillProperty());
}
getChildren().add(n);
});
Expand Down
13 changes: 5 additions & 8 deletions richtextfx/src/main/java/org/fxmisc/richtext/TextExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import java.util.Collections;
import java.util.List;

import com.sun.javafx.css.converters.EnumConverter;
import com.sun.javafx.css.converters.SizeConverter;

import javafx.beans.property.ObjectProperty;
import javafx.css.CssMetaData;
import javafx.css.StyleConverter;
Expand Down Expand Up @@ -226,7 +223,7 @@ public ObjectProperty<Paint> borderStrokeColorProperty() {
*/
public ObjectProperty<Number> underlineWidthProperty() { return underlineWidth; }

// Dash array for the text underline
// Dash array for the text underline
public Number[] getUnderlineDashArray() { return underlineDashArray.get(); }
public void setUnderlineDashArray(Number[] dashArray) { underlineDashArray.set(dashArray); }

Expand Down Expand Up @@ -275,12 +272,12 @@ private static class StyleableProperties {
);

private static final CssMetaData<TextExt, StrokeType> BORDER_TYPE = new CustomCssMetaData<>(
"-rtfx-border-stroke-type", new EnumConverter<>(StrokeType.class),
"-rtfx-border-stroke-type", (StyleConverter<?, StrokeType>) StyleConverter.getEnumConverter(StrokeType.class),
StrokeType.INSIDE, n -> n.borderStrokeType
);

private static final CssMetaData<TextExt, Number[]> BORDER_DASH_ARRAY = new CustomCssMetaData<>(
"-rtfx-border-stroke-dash-array", SizeConverter.SequenceConverter.getInstance(),
"-rtfx-border-stroke-dash-array", JavaFXCompatibility.SizeConverter_SequenceConverter_getInstance(),
new Double[0], n -> n.borderStrokeDashArray
);

Expand All @@ -295,12 +292,12 @@ private static class StyleableProperties {
);

private static final CssMetaData<TextExt, Number[]> UNDERLINE_DASH_ARRAY = new CustomCssMetaData<>(
"-rtfx-underline-dash-array", SizeConverter.SequenceConverter.getInstance(),
"-rtfx-underline-dash-array", JavaFXCompatibility.SizeConverter_SequenceConverter_getInstance(),
new Double[0], n -> n.underlineDashArray
);

private static final CssMetaData<TextExt, StrokeLineCap> UNDERLINE_CAP = new CustomCssMetaData<>(
"-rtfx-underline-cap", new EnumConverter<StrokeLineCap>(StrokeLineCap.class),
"-rtfx-underline-cap", (StyleConverter<?, StrokeLineCap>) StyleConverter.getEnumConverter(StrokeLineCap.class),
StrokeLineCap.SQUARE, n -> n.underlineCap
);
}
Expand Down
Loading

0 comments on commit 3e039a3

Please sign in to comment.