8136366: Add a public API to create a L&F without installation
Reviewed-by: prr, serb
--- a/jdk/src/java.desktop/share/classes/javax/swing/UIManager.java Sat May 21 02:53:37 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/UIManager.java Sun May 22 12:31:56 2016 +0400
@@ -59,6 +59,7 @@
import sun.swing.SwingUtilities2;
import java.lang.reflect.Method;
import java.util.HashMap;
+import java.util.Objects;
import sun.awt.AppContext;
import sun.awt.AWTAccessor;
@@ -495,6 +496,48 @@
return getLAFState().lookAndFeel;
}
+ /**
+ * Creates a supported built-in Java {@code LookAndFeel} specified
+ * by the given {@code L&F name} name.
+ *
+ * @param name a {@code String} specifying the name of the built-in
+ * look and feel
+ * @return the built-in {@code LookAndFeel} object
+ * @throws NullPointerException if {@code name} is {@code null}
+ * @throws UnsupportedLookAndFeelException if the built-in Java {@code L&F}
+ * is not found for the given name or it is not supported by the
+ * underlying platform
+ *
+ * @see LookAndFeel#getName
+ * @see LookAndFeel#isSupportedLookAndFeel
+ *
+ * @since 9
+ */
+ public static LookAndFeel createLookAndFeel(String name)
+ throws UnsupportedLookAndFeelException {
+ Objects.requireNonNull(name);
+
+ if ("GTK look and feel".equals(name)) {
+ name = "GTK+";
+ }
+
+ try {
+ for (LookAndFeelInfo info : installedLAFs) {
+ if (info.getName().equals(name)) {
+ Class<?> cls = Class.forName(UIManager.class.getModule(),
+ info.getClassName());
+ LookAndFeel laf = (LookAndFeel) cls.newInstance();
+ if (!laf.isSupportedLookAndFeel()) {
+ break;
+ }
+ return laf;
+ }
+ }
+ } catch (InstantiationException | IllegalAccessException ignore) {
+ }
+
+ throw new UnsupportedLookAndFeelException(name);
+ }
/**
* Sets the current look and feel to {@code newLookAndFeel}.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/UIManager/8136366/CreateLookAndFeelTest.java Sun May 22 12:31:56 2016 +0400
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2016, 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.
+ *
+ * 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.
+ */
+
+import java.awt.Toolkit;
+import javax.swing.LookAndFeel;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+import sun.awt.SunToolkit;
+
+/**
+ * @test
+ * @bug 8136366
+ * @summary Add a public API to create a L&F without installation
+ * @modules java.desktop/sun.awt
+ */
+public class CreateLookAndFeelTest {
+
+ public static void main(String[] args) throws Exception {
+ SwingUtilities.invokeAndWait(CreateLookAndFeelTest::createLAFs);
+ }
+
+ private static void createLAFs() {
+
+ for (LAFTest lafTest : LAFTest.values()) {
+ createLAF(lafTest);
+ }
+
+ try {
+ UIManager.createLookAndFeel(null);
+ } catch (NullPointerException e) {
+ return;
+ } catch (UnsupportedLookAndFeelException ignore) {
+ }
+
+ throw new RuntimeException("NPE is not thrown!");
+ }
+
+ private static void createLAF(LAFTest lafTest) {
+ try {
+ UIManager.createLookAndFeel(lafTest.lafName);
+
+ } catch (UnsupportedLookAndFeelException e) {
+ if (lafTest.isSupported) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private static boolean isOSAvailable(String supportedOS) {
+ return System.getProperty("os.name")
+ .toLowerCase()
+ .contains(supportedOS);
+ }
+
+ private static boolean isGTKAvailable() {
+ Toolkit toolkit = Toolkit.getDefaultToolkit();
+ if (!(toolkit instanceof SunToolkit)) {
+ return false;
+ }
+ return ((SunToolkit) toolkit).isNativeGTKAvailable();
+ }
+
+ enum LAFTest {
+
+ METAL("Metal"),
+ NIMBUS("Nimbus"),
+ MOTIF("CDE/Motif"),
+ WINDOWS("Windows", isOSAvailable("windows")),
+ GTK("GTK look and feel", isGTKAvailable()),
+ MAC("Mac OS X", isOSAvailable("mac"));
+
+ private final String lafName;
+ private final boolean isSupported;
+
+ private LAFTest(String lafName) {
+ this(lafName, true);
+ }
+
+ private LAFTest(String lafName, boolean crossPlatform) {
+ this.lafName = lafName;
+ this.isSupported = crossPlatform;
+ }
+ }
+}