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

8332895: Support interpolation for backgrounds and borders #1471

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4363,6 +4363,7 @@ task javadoc(type: Javadoc, dependsOn: createMSPfile) {
options.tags("apiNote:a:API Note:")
options.tags("implSpec:a:Implementation Requirements:")
options.tags("implNote:a:Implementation Note:")
options.tags("interpolationType:a:Interpolation type:")
options.tags("param")
options.tags("return")
options.tags("throws")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,6 +26,8 @@
package com.sun.javafx;

import java.util.AbstractList;
import java.util.List;
import java.util.Objects;
import java.util.RandomAccess;

/**
Expand Down Expand Up @@ -60,4 +62,68 @@ public UnmodifiableArrayList(T[] elements, int size) {
@Override public int size() {
return size;
}

/**
* Converts the specified list into an unmodifiable list that does not contain {@code null} values.
* The returned list is a copy of, and not a wrapper around the specified list.
*
* @param <T> the type of elements in the list
* @param list the list, not {@code null}
* @return an unmodifiable list that does not contain null values
*/
public static <T> UnmodifiableArrayList<T> copyOfNullFiltered(List<T> list) {
Objects.requireNonNull(list, "list cannot be null");

int index = 0;
int numNonNullValues = 0;

@SuppressWarnings("unchecked")
T[] newValues = (T[])new Object[list.size()];

if (list instanceof RandomAccess) {
// Prevents the iterator allocation for random-access lists.
for (int i = 0, max = list.size(); i < max; ++i) {
T value = list.get(i);
if (value != null) {
newValues[index++] = value;
++numNonNullValues;
}
}
} else {
for (T value : list) {
if (value != null) {
newValues[index++] = value;
++numNonNullValues;
}
}
}

return new UnmodifiableArrayList<>(newValues, numNonNullValues);
}

/**
* Converts the specified array into an unmodifiable list that does not contain {@code null} values.
* The returned list is a copy of, and not a wrapper around the specified array.
*
* @param <T> the type of elements in the array
* @param elements the array, not {@code null}
* @return an unmodifiable list that does not contain null values
*/
public static <T> UnmodifiableArrayList<T> copyOfNullFiltered(T[] elements) {
Objects.requireNonNull(elements, "elements cannot be null");

int numNonNullValues = 0;

@SuppressWarnings("unchecked")
T[] newValues = (T[])new Object[elements.length];

for (int i = 0, j = 0; i < elements.length; ++i) {
if (elements[i] != null) {
newValues[j++] = elements[i];
++numNonNullValues;
}
}

return new UnmodifiableArrayList<>(newValues, numNonNullValues);
}
}
1 change: 1 addition & 0 deletions modules/javafx.base/src/test/addExports
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
--add-exports javafx.base/com.sun.javafx=ALL-UNNAMED
--add-exports javafx.base/com.sun.javafx.binding=ALL-UNNAMED
--add-exports javafx.base/com.sun.javafx.collections=ALL-UNNAMED
--add-exports javafx.base/com.sun.javafx.event=ALL-UNNAMED
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.com.sun.javafx;

import com.sun.javafx.UnmodifiableArrayList;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class UnmodifiableArrayListTest {

@Test
void testCopyOfNullFilteredList_nullList() {
assertThrows(NullPointerException.class, () -> UnmodifiableArrayList.copyOfNullFiltered((List<?>)null));
}

@Test
void testCopyOfNullFilteredList_nullArray() {
assertThrows(NullPointerException.class, () -> UnmodifiableArrayList.copyOfNullFiltered((Object[])null));
}

@Test
void testCopyOfNullFilteredList_randomAccess() {
var list = new ArrayList<String>();
list.add("a");
list.add(null);
list.add("b");
list.add(null);

assertEquals(List.of("a", "b"), UnmodifiableArrayList.copyOfNullFiltered(list));
}

@Test
void testCopyOfNullFilteredList_nonRandomAccess() {
var list = new LinkedList<String>();
list.add("a");
list.add(null);
list.add("b");
list.add(null);

assertEquals(List.of("a", "b"), UnmodifiableArrayList.copyOfNullFiltered(list));
}

@Test
void testCopyOfNullFilteredArray() {
var list = new String[4];
list[0] = "a";
list[1] = null;
list[2] = "b";
list[3] = null;

assertEquals(List.of("a", "b"), UnmodifiableArrayList.copyOfNullFiltered(list));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.sun.javafx.scene.paint;

import javafx.animation.Interpolatable;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Paint;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import java.util.List;

public final class PaintUtils {

private PaintUtils() {}

/**
* Interpolates between potentially different types of paint.
* <p>
* In addition to homogeneous interpolations between paints of the same type, the following
* heterogeneous interpolations are supported:
* <ul>
* <li>Color ↔ LinearGradient
* <li>Color ↔ RadialGradient
* </ul>
* If a paint is not interpolatable, {@code startValue} is returned for {@code t < 0.5},
* and {@code endValue} is returned otherwise.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static Paint interpolate(Paint startValue, Paint endValue, double t) {
if (startValue instanceof Color start) {
if (endValue instanceof Color end) {
return start.interpolate(end, t);
}

if (endValue instanceof LinearGradient end) {
return newSolidGradient(end, start).interpolate(end, t);
}

if (endValue instanceof RadialGradient end) {
return newSolidGradient(end, start).interpolate(end, t);
}
}

if (startValue instanceof LinearGradient start) {
if (endValue instanceof LinearGradient end) {
return start.interpolate(end, t);
}

if (endValue instanceof Color end) {
return start.interpolate(newSolidGradient(start, end), t);
}
}

if (startValue instanceof RadialGradient start) {
if (endValue instanceof RadialGradient end) {
return start.interpolate(end, t);
}

if (endValue instanceof Color end) {
return start.interpolate(newSolidGradient(start, end), t);
}
}

if (startValue instanceof Interpolatable start
&& endValue instanceof Interpolatable end
&& startValue.getClass().isInstance(endValue)) {
return (Paint)start.interpolate(end, t);
}

return t < 0.5 ? startValue : endValue;
}

/**
* Creates a new linear gradient that consists of two stops with the same color.
*/
public static LinearGradient newSolidGradient(LinearGradient source, Color color) {
return new LinearGradient(
source.getStartX(), source.getStartY(),
source.getEndX(), source.getEndY(),
source.isProportional(),
source.getCycleMethod(),
List.of(new Stop(0, color), new Stop(1, color)));
}

/**
* Creates a new radial gradient that consists of two stops with the same color.
*/
public static RadialGradient newSolidGradient(RadialGradient source, Color color) {
return new RadialGradient(
source.getFocusAngle(), source.getFocusDistance(),
source.getCenterX(), source.getCenterY(),
source.getRadius(),
source.isProportional(),
source.getCycleMethod(),
List.of(new Stop(0, color), new Stop(1, color)));
}
}
Loading