KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > MultiUIDefaults


1 /*
2  * @(#)MultiUIDefaults.java 1.16 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.util.Enumeration JavaDoc;
11 import java.util.Locale JavaDoc;
12
13
14
15 /**
16  *
17  * @version 1.10 02/02/00
18  * @author Hans Muller
19  */

20 class MultiUIDefaults extends UIDefaults JavaDoc
21 {
22     private UIDefaults JavaDoc[] tables;
23
24     public MultiUIDefaults(UIDefaults JavaDoc[] defaults) {
25     super();
26     tables = defaults;
27     }
28
29     public MultiUIDefaults() {
30     super();
31     tables = new UIDefaults JavaDoc[0];
32     }
33
34
35     public Object JavaDoc get(Object JavaDoc key)
36     {
37     Object JavaDoc value = super.get(key);
38     if (value != null) {
39         return value;
40     }
41
42     for(int i = 0; i < tables.length; i++) {
43         UIDefaults JavaDoc table = tables[i];
44         value = (table != null) ? table.get(key) : null;
45         if (value != null) {
46         return value;
47         }
48     }
49
50     return null;
51     }
52
53
54     public Object JavaDoc get(Object JavaDoc key, Locale JavaDoc l)
55     {
56     Object JavaDoc value = super.get(key,l);
57     if (value != null) {
58         return value;
59     }
60
61     for(int i = 0; i < tables.length; i++) {
62         UIDefaults JavaDoc table = tables[i];
63         value = (table != null) ? table.get(key,l) : null;
64         if (value != null) {
65         return value;
66         }
67     }
68
69     return null;
70     }
71
72
73     public int size() {
74     int n = super.size();
75     for(int i = 0; i < tables.length; i++) {
76         UIDefaults JavaDoc table = tables[i];
77         n += (table != null) ? table.size() : 0;
78     }
79     return n;
80     }
81
82
83     public boolean isEmpty() {
84     return size() == 0;
85     }
86
87
88     public Enumeration JavaDoc keys()
89     {
90     Enumeration JavaDoc[] enums = new Enumeration JavaDoc[1 + tables.length];
91     enums[0] = super.keys();
92     for(int i = 0; i < tables.length; i++) {
93         UIDefaults JavaDoc table = tables[i];
94         if (table != null) {
95         enums[i + 1] = table.keys();
96         }
97     }
98     return new MultiUIDefaultsEnumerator(enums);
99     }
100
101
102     public Enumeration JavaDoc elements()
103     {
104     Enumeration JavaDoc[] enums = new Enumeration JavaDoc[1 + tables.length];
105     enums[0] = super.elements();
106     for(int i = 0; i < tables.length; i++) {
107         UIDefaults JavaDoc table = tables[i];
108         if (table != null) {
109         enums[i + 1] = table.elements();
110         }
111     }
112     return new MultiUIDefaultsEnumerator(enums);
113     }
114
115     protected void getUIError(String JavaDoc msg) {
116         if (tables.length > 0) {
117             tables[0].getUIError(msg);
118         } else {
119             super.getUIError(msg);
120         }
121     }
122
123     private static class MultiUIDefaultsEnumerator implements Enumeration JavaDoc
124     {
125     Enumeration JavaDoc[] enums;
126     int n = 0;
127
128     MultiUIDefaultsEnumerator(Enumeration JavaDoc[] enums) {
129         this.enums = enums;
130     }
131
132     public boolean hasMoreElements() {
133         for(int i = n; i < enums.length; i++) {
134         Enumeration JavaDoc e = enums[i];
135         if ((e != null) && (e.hasMoreElements())) {
136             return true;
137         }
138         }
139         return false;
140     }
141
142     public Object JavaDoc nextElement() {
143         for(; n < enums.length; n++) {
144         Enumeration JavaDoc e = enums[n];
145         if ((e != null) && (e.hasMoreElements())) {
146             return e.nextElement();
147         }
148         }
149         return null;
150     }
151     }
152
153
154     public Object JavaDoc remove(Object JavaDoc key)
155     {
156     Object JavaDoc value = super.remove(key);
157     if (value != null) {
158         return value;
159     }
160
161     for(int i = 0; i < tables.length; i++) {
162         UIDefaults JavaDoc table = tables[i];
163         value = (table != null) ? table.remove(key) : null;
164         if (value != null) {
165         return value;
166         }
167     }
168
169     return null;
170     }
171
172
173     public void clear() {
174     super.clear();
175     for(int i = 0; i < tables.length; i++) {
176         UIDefaults JavaDoc table = tables[i];
177         if (table != null) {
178         table.clear();
179         }
180     }
181     }
182
183     public synchronized String JavaDoc toString() {
184     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
185     buf.append("{");
186     Enumeration JavaDoc keys = keys();
187     while (keys.hasMoreElements()) {
188         Object JavaDoc key = keys.nextElement();
189         buf.append(key + "=" + get(key) + ", ");
190     }
191     int length = buf.length();
192     if (length > 1) {
193         buf.delete(length-2, length);
194     }
195     buf.append("}");
196     return buf.toString();
197     }
198 }
199
Popular Tags