jdk/src/share/classes/javax/swing/MultiUIDefaults.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class MultiUIDefaults extends UIDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private UIDefaults[] tables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public MultiUIDefaults(UIDefaults[] defaults) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        tables = defaults;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public MultiUIDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        tables = new UIDefaults[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public Object get(Object key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Object value = super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for(int i = 0; i < tables.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            UIDefaults table = tables[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            value = (table != null) ? table.get(key) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public Object get(Object key, Locale l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Object value = super.get(key,l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for(int i = 0; i < tables.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            UIDefaults table = tables[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            value = (table != null) ? table.get(key,l) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int n = super.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for(int i = 0; i < tables.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            UIDefaults table = tables[i];
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public Enumeration keys()
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public Enumeration elements()
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected void getUIError(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (tables.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            tables[0].getUIError(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            super.getUIError(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static class MultiUIDefaultsEnumerator implements Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Enumeration[] enums;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        MultiUIDefaultsEnumerator(Enumeration[] enums) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            this.enums = enums;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            for(int i = n; i < enums.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                Enumeration e = enums[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if ((e != null) && (e.hasMoreElements())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            for(; n < enums.length; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                Enumeration e = enums[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if ((e != null) && (e.hasMoreElements())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    return e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public Object remove(Object key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Object value = super.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for(int i = 0; i < tables.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            UIDefaults table = tables[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            value = (table != null) ? table.remove(key) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        super.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        for(int i = 0; i < tables.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            UIDefaults table = tables[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (table != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                table.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        buf.append("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Enumeration keys = keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            buf.append(key + "=" + get(key) + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int length = buf.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            buf.delete(length-2, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        buf.append("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}