This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit c3833778db575551ff506bd1fecaa001690834a6
Author: Eric Milles <[email protected]>
AuthorDate: Sun Oct 31 15:28:49 2021 -0500

    `UIManager#setLookAndFeel(String)` can load system classes
---
 .../groovy/groovy/swing/LookAndFeelHelper.groovy   | 66 +++++++++++-----------
 .../groovy/groovy/swing/SwingBuilderTest.groovy    |  6 +-
 2 files changed, 36 insertions(+), 36 deletions(-)

diff --git 
a/subprojects/groovy-swing/src/main/groovy/groovy/swing/LookAndFeelHelper.groovy
 
b/subprojects/groovy-swing/src/main/groovy/groovy/swing/LookAndFeelHelper.groovy
index d07d83f..a88467d 100644
--- 
a/subprojects/groovy-swing/src/main/groovy/groovy/swing/LookAndFeelHelper.groovy
+++ 
b/subprojects/groovy-swing/src/main/groovy/groovy/swing/LookAndFeelHelper.groovy
@@ -36,7 +36,7 @@ class LookAndFeelHelper {
         return instance ?: (instance = new LookAndFeelHelper())
     }
 
-    private final Map lafCodeNames = [
+    private final Map<String, String> lafCodeNames = [
         // stuff built into various JDKs
         metal   : 'javax.swing.plaf.metal.MetalLookAndFeel',
         nimbus  : getNimbusLAFName(),
@@ -101,53 +101,55 @@ class LookAndFeelHelper {
         attrs[attr] = handler
     }
 
-
     boolean isLeaf() {
         return true
     }
 
     LookAndFeel lookAndFeel(Object value, Map attributes, Closure initClosure) 
{
-        LookAndFeel lafInstance
-        String lafClassName
-
-        if ((value instanceof Closure) && (initClosure == null)) {
+        if (value instanceof Closure && initClosure == null) {
             initClosure = value
             value = null
         }
         if (value == null) {
             value = attributes.remove('lookAndFeel')
         }
-        if (value instanceof GString) value = value as String
-        if (FactoryBuilderSupport.checkValueIsTypeNotString(value, 
'lookAndFeel', LookAndFeel)) {
-            lafInstance = value
-            lafClassName = lafInstance.class.name
-        } else if (value != null) {
-            lafClassName = lafCodeNames[value] ?: value
-            lafInstance = Class.forName(lafClassName, true, 
getClass().classLoader).newInstance()
-        }
+        if (value instanceof GString) value = value.toString()
+
+        LookAndFeel laf = UIManager.getLookAndFeel()
+        try {
+            if (FactoryBuilderSupport.checkValueIsTypeNotString(value, 
'lookAndFeel', LookAndFeel)) {
+                UIManager.setLookAndFeel(value)
+            } else if (value != null) {
+                UIManager.setLookAndFeel(lafCodeNames[value] ?: value)
+            }
+
+            LookAndFeel lafInstance = UIManager.getLookAndFeel()
+            String lafClassName = lafInstance.class.name
+
+            // assume all configuration must be done prior to LAF being 
installed
+            Map possibleAttributes = extendedAttributes[lafClassName] ?: [:]
 
-        // assume all configuration must be done prior to LAF being installed
-        Map possibleAttributes = extendedAttributes[lafClassName] ?: [:]
-
-        attributes.each {k, v ->
-            if (possibleAttributes[k]) {
-                possibleAttributes[k](lafInstance, v)
-            } else {
-                try {
-                    lafInstance."$k" = v
-                } catch (MissingPropertyException mpe) {
-                    throw new RuntimeException("SwingBuilder initialization 
for the Look and Feel Class $lafClassName does accept the attribute $k")
+            attributes.each {k, v ->
+                if (possibleAttributes[k]) {
+                    possibleAttributes[k](lafInstance, v)
+                } else {
+                    try {
+                        lafInstance."$k" = v
+                    } catch (MissingPropertyException mpe) {
+                        throw new RuntimeException("SwingBuilder 
initialization for the Look and Feel Class $lafClassName does accept the 
attribute $k")
+                    }
                 }
             }
-        }
-
-        if (initClosure) {
-            initClosure.call(lafInstance)
-        }
 
-        UIManager.setLookAndFeel(lafInstance)
+            if (initClosure) {
+                initClosure.call(lafInstance)
+            }
 
-        return lafInstance
+            return lafInstance
+        } catch (RuntimeException e) {
+            UIManager.setLookAndFeel(laf)
+            throw e
+        }
     }
 
     static String getNimbusLAFName() {
diff --git 
a/subprojects/groovy-swing/src/test/groovy/groovy/swing/SwingBuilderTest.groovy 
b/subprojects/groovy-swing/src/test/groovy/groovy/swing/SwingBuilderTest.groovy
index d6d82fc..3a7e7ac 100644
--- 
a/subprojects/groovy-swing/src/test/groovy/groovy/swing/SwingBuilderTest.groovy
+++ 
b/subprojects/groovy-swing/src/test/groovy/groovy/swing/SwingBuilderTest.groovy
@@ -1431,10 +1431,8 @@ class SwingBuilderTest extends GroovySwingTestCase {
                 swing.lookAndFeel(lookAndFeel: 'metal', boldFonts: true) { laf 
->
                     assert laf instanceof MetalLookAndFeel
                 }
-                shouldFail {
-                    swing.lookAndFeel() {laf ->
-                        "do" + "Nothing"
-                    }
+                swing.lookAndFeel() { laf ->
+                    assert laf != null
                 }
             } finally {
                 UIManager.setLookAndFeel(oldLAF)

Reply via email to