jdk/src/share/classes/javax/swing/MultiUIDefaults.java
author peterz
Fri, 25 Dec 2009 17:47:08 +0300
changeset 4842 c9f791782a29
parent 2658 43e06bc950ec
child 5506 202f599c92aa
permissions -rw-r--r--
6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified Reviewed-by: rupashka
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;
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
    30
import java.util.Iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Locale;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
    32
import java.util.Map.Entry;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
    33
import java.util.Set;
2
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class MultiUIDefaults extends UIDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private UIDefaults[] tables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public MultiUIDefaults(UIDefaults[] defaults) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        tables = defaults;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public MultiUIDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        tables = new UIDefaults[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
    55
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public Object get(Object key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Object value = super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    63
        for (UIDefaults table : tables) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            value = (table != null) ? table.get(key) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
    73
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public Object get(Object key, Locale l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Object value = super.get(key,l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    81
        for (UIDefaults table : tables) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            value = (table != null) ? table.get(key,l) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
    91
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public int size() {
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
    93
        return entrySet().size();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
    96
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   101
    @Override
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   102
    public Enumeration<Object> keys()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    {
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   104
        return new MultiUIDefaultsEnumerator(
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   105
                MultiUIDefaultsEnumerator.Type.KEYS, entrySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   108
    @Override
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   109
    public Enumeration<Object> elements()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   111
        return new MultiUIDefaultsEnumerator(
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   112
                MultiUIDefaultsEnumerator.Type.ELEMENTS, entrySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   115
    @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   116
    public Set<Entry<Object, Object>> entrySet() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   117
        Set<Entry<Object, Object>> set = new HashSet<Entry<Object, Object>>();
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   118
        for (int i = tables.length - 1; i >= 0; i--) {
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   119
            if (tables[i] != null) {
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   120
                set.addAll(tables[i].entrySet());
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   121
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   122
        }
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   123
        set.addAll(super.entrySet());
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   124
        return set;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   125
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   126
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   127
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected void getUIError(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (tables.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            tables[0].getUIError(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            super.getUIError(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   136
    private static class MultiUIDefaultsEnumerator implements Enumeration<Object>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    {
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   138
        public static enum Type { KEYS, ELEMENTS };
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   139
        private Iterator<Entry<Object, Object>> iterator;
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   140
        private Type type;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   142
        MultiUIDefaultsEnumerator(Type type, Set<Entry<Object, Object>> entries) {
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   143
            this.type = type;
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   144
            this.iterator = entries.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public boolean hasMoreElements() {
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   148
            return iterator.hasNext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        public Object nextElement() {
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   152
            switch (type) {
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   153
                case KEYS: return iterator.next().getKey();
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   154
                case ELEMENTS: return iterator.next().getValue();
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   155
                default: return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   160
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public Object remove(Object key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    {
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   163
        Object value = null;
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   164
        for (int i = tables.length - 1; i >= 0; i--) {
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   165
            if (tables[i] != null) {
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   166
                Object v = tables[i].remove(key);
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   167
                if (v != null) {
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   168
                    value = v;
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   169
                }
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   170
            }
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   171
        }
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   172
        Object v = super.remove(key);
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   173
        if (v != null) {
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   174
            value = v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
4842
c9f791782a29 6860438: [Nimbus] Code to globally set slider's thumb background doesn't work as specified
peterz
parents: 2658
diff changeset
   177
        return value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   180
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        super.clear();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   183
        for (UIDefaults table : tables) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (table != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                table.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
   190
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        buf.append("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Enumeration keys = keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            buf.append(key + "=" + get(key) + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int length = buf.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            buf.delete(length-2, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        buf.append("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}