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