1 /*
2 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.swing;
27
28 import java.util.Enumeration;
29 import java.util.Locale;
30
31
32
33 /**
34 *
35 * @author Hans Muller
36 */
37 class MultiUIDefaults extends UIDefaults
38 {
39 private UIDefaults[] tables;
40
41 public MultiUIDefaults(UIDefaults[] defaults) {
42 super();
43 tables = defaults;
44 }
45
46 public MultiUIDefaults() {
47 super();
48 tables = new UIDefaults[0];
49 }
50
51
52 public Object get(Object key)
53 {
54 Object value = super.get(key);
55 if (value != null) {
56 return value;
57 }
58
59 for(int i = 0; i < tables.length; i++) {
60 UIDefaults table = tables[i];
61 value = (table != null) ? table.get(key) : null;
62 if (value != null) {
63 return value;
64 }
65 }
66
67 return null;
68 }
69
70
71 public Object get(Object key, Locale l)
72 {
73 Object value = super.get(key,l);
74 if (value != null) {
75 return value;
76 }
77
78 for(int i = 0; i < tables.length; i++) {
79 UIDefaults table = tables[i];
80 value = (table != null) ? table.get(key,l) : null;
81 if (value != null) {
82 return value;
83 }
84 }
85
86 return null;
87 }
88
89
90 public int size() {
91 int n = super.size();
92 for(int i = 0; i < tables.length; i++) {
93 UIDefaults table = tables[i];
94 n += (table != null) ? table.size() : 0;
95 }
96 return n;
97 }
98
99
100 public boolean isEmpty() {
101 return size() == 0;
102 }
103
104
105 public Enumeration keys()
106 {
107 Enumeration[] enums = new Enumeration[1 + tables.length];
108 enums[0] = super.keys();
109 for(int i = 0; i < tables.length; i++) {
110 UIDefaults table = tables[i];
111 if (table != null) {
112 enums[i + 1] = table.keys();
113 }
114 }
115 return new MultiUIDefaultsEnumerator(enums);
116 }
117
118
119 public Enumeration elements()
120 {
121 Enumeration[] enums = new Enumeration[1 + tables.length];
122 enums[0] = super.elements();
123 for(int i = 0; i < tables.length; i++) {
124 UIDefaults table = tables[i];
125 if (table != null) {
126 enums[i + 1] = table.elements();
127 }
128 }
129 return new MultiUIDefaultsEnumerator(enums);
130 }
131
132 protected void getUIError(String msg) {
133 if (tables.length > 0) {
134 tables[0].getUIError(msg);
135 } else {
136 super.getUIError(msg);
137 }
138 }
139
140 private static class MultiUIDefaultsEnumerator implements Enumeration
141 {
142 Enumeration[] enums;
143 int n = 0;
144
145 MultiUIDefaultsEnumerator(Enumeration[] enums) {
146 this.enums = enums;
147 }
148
149 public boolean hasMoreElements() {
150 for(int i = n; i < enums.length; i++) {
151 Enumeration e = enums[i];
152 if ((e != null) && (e.hasMoreElements())) {
153 return true;
154 }
155 }
156 return false;
157 }
158
159 public Object nextElement() {
160 for(; n < enums.length; n++) {
161 Enumeration e = enums[n];
162 if ((e != null) && (e.hasMoreElements())) {
163 return e.nextElement();
164 }
165 }
166 return null;
167 }
168 }
169
170
171 public Object remove(Object key)
172 {
173 Object value = super.remove(key);
174 if (value != null) {
175 return value;
176 }
177
178 for(int i = 0; i < tables.length; i++) {
179 UIDefaults table = tables[i];
180 value = (table != null) ? table.remove(key) : null;
181 if (value != null) {
182 return value;
183 }
184 }
185
186 return null;
187 }
188
189
190 public void clear() {
191 super.clear();
192 for(int i = 0; i < tables.length; i++) {
193 UIDefaults table = tables[i];
194 if (table != null) {
195 table.clear();
196 }
197 }
198 }
199
200 public synchronized String toString() {
201 StringBuffer buf = new StringBuffer();
202 buf.append("{");
203 Enumeration keys = keys();
204 while (keys.hasMoreElements()) {
205 Object key = keys.nextElement();
206 buf.append(key + "=" + get(key) + ", ");
207 }
208 int length = buf.length();
209 if (length > 1) {
210 buf.delete(length-2, length);
211 }
212 buf.append("}");
213 return buf.toString();
214 }
215 }