1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.util.Enumeration;
  29 import java.util.HashSet;
  30 import java.util.Iterator;
  31 import java.util.Locale;
  32 import java.util.Map.Entry;
  33 import java.util.Set;
  34 
  35 
  36 
  37 /**
  38  *
  39  * @author Hans Muller
  40  */
  41 @SuppressWarnings("serial") // Superclass is not serializable across versions
  42 class MultiUIDefaults extends UIDefaults
  43 {
  44     private UIDefaults[] tables;
  45 
  46     public MultiUIDefaults(UIDefaults[] defaults) {
  47         super();
  48         tables = defaults;
  49     }
  50 
  51     public MultiUIDefaults() {
  52         super();
  53         tables = new UIDefaults[0];
  54     }
  55 
  56     @Override
  57     public Object get(Object key)
  58     {
  59         Object value = super.get(key);
  60         if (value != null) {
  61             return value;
  62         }
  63 
  64         for (UIDefaults table : tables) {
  65             value = (table != null) ? table.get(key) : null;
  66             if (value != null) {
  67                 return value;
  68             }
  69         }
  70 
  71         return null;
  72     }
  73 
  74     @Override
  75     public Object get(Object key, Locale l)
  76     {
  77         Object value = super.get(key,l);
  78         if (value != null) {
  79             return value;
  80         }
  81 
  82         for (UIDefaults table : tables) {
  83             value = (table != null) ? table.get(key,l) : null;
  84             if (value != null) {
  85                 return value;
  86             }
  87         }
  88 
  89         return null;
  90     }
  91 
  92     @Override
  93     public int size() {
  94         return entrySet().size();
  95     }
  96 
  97     @Override
  98     public boolean isEmpty() {
  99         return size() == 0;
 100     }
 101 
 102     @Override
 103     public Enumeration<Object> keys()
 104     {
 105         return new MultiUIDefaultsEnumerator(
 106                 MultiUIDefaultsEnumerator.Type.KEYS, entrySet());
 107     }
 108 
 109     @Override
 110     public Enumeration<Object> elements()
 111     {
 112         return new MultiUIDefaultsEnumerator(
 113                 MultiUIDefaultsEnumerator.Type.ELEMENTS, entrySet());
 114     }
 115 
 116     @Override
 117     public Set<Entry<Object, Object>> entrySet() {
 118         Set<Entry<Object, Object>> set = new HashSet<Entry<Object, Object>>();
 119         for (int i = tables.length - 1; i >= 0; i--) {
 120             if (tables[i] != null) {
 121                 set.addAll(tables[i].entrySet());
 122             }
 123         }
 124         set.addAll(super.entrySet());
 125         return set;
 126     }
 127 
 128     @Override
 129     protected void getUIError(String msg) {
 130         if (tables.length > 0) {
 131             tables[0].getUIError(msg);
 132         } else {
 133             super.getUIError(msg);
 134         }
 135     }
 136 
 137     private static class MultiUIDefaultsEnumerator implements Enumeration<Object>
 138     {
 139         public static enum Type { KEYS, ELEMENTS };
 140         private Iterator<Entry<Object, Object>> iterator;
 141         private Type type;
 142 
 143         MultiUIDefaultsEnumerator(Type type, Set<Entry<Object, Object>> entries) {
 144             this.type = type;
 145             this.iterator = entries.iterator();
 146         }
 147 
 148         public boolean hasMoreElements() {
 149             return iterator.hasNext();
 150         }
 151 
 152         public Object nextElement() {
 153             switch (type) {
 154                 case KEYS: return iterator.next().getKey();
 155                 case ELEMENTS: return iterator.next().getValue();
 156                 default: return null;
 157             }
 158         }
 159     }
 160 
 161     @Override
 162     public Object remove(Object key)
 163     {
 164         Object value = null;
 165         for (int i = tables.length - 1; i >= 0; i--) {
 166             if (tables[i] != null) {
 167                 Object v = tables[i].remove(key);
 168                 if (v != null) {
 169                     value = v;
 170                 }
 171             }
 172         }
 173         Object v = super.remove(key);
 174         if (v != null) {
 175             value = v;
 176         }
 177 
 178         return value;
 179     }
 180 
 181     @Override
 182     public void clear() {
 183         super.clear();
 184         for (UIDefaults table : tables) {
 185             if (table != null) {
 186                 table.clear();
 187             }
 188         }
 189     }
 190 
 191     @Override
 192     public synchronized String toString() {
 193         StringBuilder sb = new StringBuilder();
 194         sb.append("{");
 195         Enumeration<?> keys = keys();
 196         while (keys.hasMoreElements()) {
 197             Object key = keys.nextElement();
 198             sb.append(key).append('=').append(get(key)).append(", ");
 199         }
 200         int length = sb.length();
 201         if (length > 1) {
 202             sb.delete(length - 2, length);
 203         }
 204         sb.append('}');
 205         return sb.toString();
 206     }
 207 }