6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
authorpeterz
Fri, 25 Dec 2009 17:47:08 +0300
changeset 4842 c9f791782a29
parent 4841 ae658e3b0f27
child 4843 6f85783b445f
6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified Reviewed-by: rupashka
jdk/src/share/classes/javax/swing/MultiUIDefaults.java
jdk/test/javax/swing/MultiUIDefaults/4300666/bug4300666.html
jdk/test/javax/swing/MultiUIDefaults/4300666/bug4300666.java
jdk/test/javax/swing/MultiUIDefaults/4331767/bug4331767.java
jdk/test/javax/swing/MultiUIDefaults/Test6860438.java
--- a/jdk/src/share/classes/javax/swing/MultiUIDefaults.java	Mon Dec 21 19:26:58 2009 +0300
+++ b/jdk/src/share/classes/javax/swing/MultiUIDefaults.java	Fri Dec 25 17:47:08 2009 +0300
@@ -27,6 +27,7 @@
 
 import java.util.Enumeration;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -89,11 +90,7 @@
 
     @Override
     public int size() {
-        int n = super.size();
-        for (UIDefaults table : tables) {
-            n += (table != null) ? table.size() : 0;
-        }
-        return n;
+        return entrySet().size();
     }
 
     @Override
@@ -104,40 +101,26 @@
     @Override
     public Enumeration<Object> keys()
     {
-        Enumeration[] enums = new Enumeration[1 + tables.length];
-        enums[0] = super.keys();
-        for(int i = 0; i < tables.length; i++) {
-            UIDefaults table = tables[i];
-            if (table != null) {
-                enums[i + 1] = table.keys();
-            }
-        }
-        return new MultiUIDefaultsEnumerator(enums);
+        return new MultiUIDefaultsEnumerator(
+                MultiUIDefaultsEnumerator.Type.KEYS, entrySet());
     }
 
     @Override
     public Enumeration<Object> elements()
     {
-        Enumeration[] enums = new Enumeration[1 + tables.length];
-        enums[0] = super.elements();
-        for(int i = 0; i < tables.length; i++) {
-            UIDefaults table = tables[i];
-            if (table != null) {
-                enums[i + 1] = table.elements();
-            }
-        }
-        return new MultiUIDefaultsEnumerator(enums);
+        return new MultiUIDefaultsEnumerator(
+                MultiUIDefaultsEnumerator.Type.ELEMENTS, entrySet());
     }
 
     @Override
     public Set<Entry<Object, Object>> entrySet() {
         Set<Entry<Object, Object>> set = new HashSet<Entry<Object, Object>>();
-        if (tables == null) return set;
-        for (UIDefaults table : tables) {
-            if (table != null) {
-                set.addAll(table.entrySet());
+        for (int i = tables.length - 1; i >= 0; i--) {
+            if (tables[i] != null) {
+                set.addAll(tables[i].entrySet());
             }
         }
+        set.addAll(super.entrySet());
         return set;
     }
 
@@ -152,50 +135,46 @@
 
     private static class MultiUIDefaultsEnumerator implements Enumeration<Object>
     {
-        Enumeration[] enums;
-        int n = 0;
+        public static enum Type { KEYS, ELEMENTS };
+        private Iterator<Entry<Object, Object>> iterator;
+        private Type type;
 
-        MultiUIDefaultsEnumerator(Enumeration[] enums) {
-            this.enums = enums;
+        MultiUIDefaultsEnumerator(Type type, Set<Entry<Object, Object>> entries) {
+            this.type = type;
+            this.iterator = entries.iterator();
         }
 
         public boolean hasMoreElements() {
-            for(int i = n; i < enums.length; i++) {
-                Enumeration e = enums[i];
-                if ((e != null) && (e.hasMoreElements())) {
-                    return true;
-                }
-            }
-            return false;
+            return iterator.hasNext();
         }
 
         public Object nextElement() {
-            for(; n < enums.length; n++) {
-                Enumeration e = enums[n];
-                if ((e != null) && (e.hasMoreElements())) {
-                    return e.nextElement();
-                }
+            switch (type) {
+                case KEYS: return iterator.next().getKey();
+                case ELEMENTS: return iterator.next().getValue();
+                default: return null;
             }
-            return null;
         }
     }
 
     @Override
     public Object remove(Object key)
     {
-        Object value = super.remove(key);
-        if (value != null) {
-            return value;
+        Object value = null;
+        for (int i = tables.length - 1; i >= 0; i--) {
+            if (tables[i] != null) {
+                Object v = tables[i].remove(key);
+                if (v != null) {
+                    value = v;
+                }
+            }
+        }
+        Object v = super.remove(key);
+        if (v != null) {
+            value = v;
         }
 
-        for (UIDefaults table : tables) {
-            value = (table != null) ? table.remove(key) : null;
-            if (value != null) {
-                return value;
-            }
-        }
-
-        return null;
+        return value;
     }
 
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/MultiUIDefaults/4300666/bug4300666.html	Fri Dec 25 17:47:08 2009 +0300
@@ -0,0 +1,28 @@
+<!--
+ Copyright 2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ CA 95054 USA or visit www.sun.com if you need additional information or
+ have any questions.
+-->
+ 
+<html>
+<body>
+<applet  code="bug4300666.class" width=200 height=200></applet>
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/MultiUIDefaults/4300666/bug4300666.java	Fri Dec 25 17:47:08 2009 +0300
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+   @bug 4300666
+   @summary Printing UIDefaults throws NoSuchElementExcept
+   @author Andrey Pikalev
+   @run applet bug4300666.html
+*/
+
+import javax.swing.*;
+
+
+public class bug4300666 extends JApplet {
+
+    public void init() {
+        UIDefaults d = UIManager.getDefaults();
+        d.toString();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/MultiUIDefaults/4331767/bug4331767.java	Fri Dec 25 17:47:08 2009 +0300
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+   @bug 4331767
+   @summary Tests that custom implementation of UIDefaults.getUIError() is
+            called when an UI error occurs
+   @author Peter Zhelezniakov
+   @run main bug4331767
+*/
+import javax.swing.*;
+import javax.swing.plaf.metal.MetalLookAndFeel;
+import java.util.Locale;
+
+public class bug4331767
+{
+    private static boolean passed = false;
+
+    public static void main(String[] argv) {
+        try {
+            UIManager.setLookAndFeel(new BrokenLookAndFeel());
+        } catch (Exception e) {
+            throw new Error("Failed to set BrokenLookAndFeel, cannot test", e);
+        }
+
+        // This should call BrokenUIDefaults.getUIError()
+        new JButton();
+
+        if (!passed) {
+            throw new RuntimeException("Failed: Custom getUIError() not called");
+        }
+    }
+
+    static class BrokenUIDefaults extends UIDefaults {
+        UIDefaults defaults;
+
+        public BrokenUIDefaults(UIDefaults def) {
+            defaults = def;
+        }
+
+        public Object get(Object key) {
+            if ("ButtonUI".equals(key)) {
+                System.err.println("[II]  Called BrokenUIDefaults.get(Object)");
+                return "a nonexistent class";
+            }
+            return defaults.get(key);
+        }
+
+        public Object get(Object key, Locale l) {
+            if ("ButtonUI".equals(key)) {
+                System.err.println("[II]  Called BrokenUIDefaults.get(Object, Locale)");
+                return "a nonexistent class";
+            }
+            return defaults.get(key, l);
+        }
+
+        protected void getUIError(String msg) {
+            System.err.println("[II]  BrokenUIDefaults.getUIError() called, test passes");
+            passed = true;
+        }
+    }
+
+    static class BrokenLookAndFeel extends MetalLookAndFeel {
+        UIDefaults defaults;
+
+        public BrokenLookAndFeel() {
+            defaults = new BrokenUIDefaults(super.getDefaults());
+        }
+
+        public UIDefaults getDefaults() {
+            return defaults;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/MultiUIDefaults/Test6860438.java	Fri Dec 25 17:47:08 2009 +0300
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+   @bug 6860438
+   @summary Tests various MultiUIDefaults methods
+   @author Peter Zhelezniakov
+   @run main Test6860438
+*/
+
+import java.util.Enumeration;
+import java.util.Map.Entry;
+import java.util.Set;
+import javax.swing.UIManager;
+
+public class Test6860438
+{
+    static final String KEY = "Test6860438.key";
+    static final String VALUE = "Test6860438.value";
+
+    void check(Object key, Object value, boolean present, int size) {
+        check(UIManager.get(key) == value, "UIManager.get()");
+        check(UIManager.getDefaults().size() == size, "MultiUIDefaults.size()");
+
+        checkEnumeration(UIManager.getDefaults().keys(),
+                key, present, "MultiUIDefaults.keys()");
+        checkEnumeration(UIManager.getDefaults().elements(),
+                value, present, "MultiUIDefaults.elements()");
+
+        // check MultiUIDefaults.entrySet()
+        boolean found = false;
+        Set<Entry<Object, Object>> entries = UIManager.getDefaults().entrySet();
+        for (Entry<Object, Object> e: entries) {
+            if (e.getKey() == key) {
+                check(e.getValue() == value, "MultiUIDefaults.entrySet()");
+                found = true;
+            }
+        }
+        check(found == present, "MultiUIDefaults.entrySet()");
+    }
+
+    void checkEnumeration(Enumeration<Object> e, Object elem,
+            boolean present, String error) {
+        boolean found = false;
+        while (e.hasMoreElements()) {
+            if (e.nextElement() == elem) {
+                found = true;
+            }
+        }
+        check(found == present, error);
+    }
+
+    void check(boolean condition, String methodName) {
+        if (! condition) {
+            throw new RuntimeException(methodName + " failed");
+        }
+    }
+
+    void test() {
+        int size = UIManager.getDefaults().size();
+
+        // create a new value, size increases
+        UIManager.getLookAndFeelDefaults().put(KEY, VALUE);
+        check(KEY, VALUE, true, size + 1);
+
+        // override the value, size remains the same
+        UIManager.put(KEY, VALUE);
+        check(KEY, VALUE, true, size + 1);
+
+        // remove the value, size decreases
+        UIManager.getDefaults().remove(KEY);
+        check(KEY, null, false, size);
+    }
+
+    public static void main(String[] args) {
+        new Test6860438().test();
+    }
+}