diff --git a/src/share/classes/javax/swing/MultiUIDefaults.java b/src/share/classes/javax/swing/MultiUIDefaults.java index 20eeeafa24a4513ff7953d88138dc71e2e608449..4319ef4736704311432e9462ac6a4b9774c3b4a6 100644 --- a/src/share/classes/javax/swing/MultiUIDefaults.java +++ b/src/share/classes/javax/swing/MultiUIDefaults.java @@ -27,6 +27,7 @@ package javax.swing; 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 @@ class MultiUIDefaults extends UIDefaults @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 @@ class MultiUIDefaults extends UIDefaults @Override public Enumeration 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 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> entrySet() { Set> set = new HashSet>(); - 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 @@ class MultiUIDefaults extends UIDefaults private static class MultiUIDefaultsEnumerator implements Enumeration { - Enumeration[] enums; - int n = 0; + public static enum Type { KEYS, ELEMENTS }; + private Iterator> iterator; + private Type type; - MultiUIDefaultsEnumerator(Enumeration[] enums) { - this.enums = enums; + MultiUIDefaultsEnumerator(Type type, Set> 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; - } - - for (UIDefaults table : tables) { - value = (table != null) ? table.remove(key) : null; - 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; + } - return null; + return value; } @Override diff --git a/test/javax/swing/MultiUIDefaults/4300666/bug4300666.html b/test/javax/swing/MultiUIDefaults/4300666/bug4300666.html new file mode 100644 index 0000000000000000000000000000000000000000..5d958b96a09c32ccc94ca9c1b79b13796562a81f --- /dev/null +++ b/test/javax/swing/MultiUIDefaults/4300666/bug4300666.html @@ -0,0 +1,28 @@ + + + + + + + diff --git a/test/javax/swing/MultiUIDefaults/4300666/bug4300666.java b/test/javax/swing/MultiUIDefaults/4300666/bug4300666.java new file mode 100644 index 0000000000000000000000000000000000000000..b4d1abef4425ed62878d09a12ad9b44f694ee6e2 --- /dev/null +++ b/test/javax/swing/MultiUIDefaults/4300666/bug4300666.java @@ -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(); + } +} diff --git a/test/javax/swing/MultiUIDefaults/4331767/bug4331767.java b/test/javax/swing/MultiUIDefaults/4331767/bug4331767.java new file mode 100644 index 0000000000000000000000000000000000000000..9d6736129591340c0d087fb49d812f51627afedb --- /dev/null +++ b/test/javax/swing/MultiUIDefaults/4331767/bug4331767.java @@ -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; + } + } +} diff --git a/test/javax/swing/MultiUIDefaults/Test6860438.java b/test/javax/swing/MultiUIDefaults/Test6860438.java new file mode 100644 index 0000000000000000000000000000000000000000..0ce0cad8b1859ddbe8e4cf03caf69ae39d4a52c9 --- /dev/null +++ b/test/javax/swing/MultiUIDefaults/Test6860438.java @@ -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> entries = UIManager.getDefaults().entrySet(); + for (Entry e: entries) { + if (e.getKey() == key) { + check(e.getValue() == value, "MultiUIDefaults.entrySet()"); + found = true; + } + } + check(found == present, "MultiUIDefaults.entrySet()"); + } + + void checkEnumeration(Enumeration 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(); + } +}