Home » openjdk-7 » javax » swing » [javadoc | source]

    1   /*
    2    * Copyright (c) 1997, 2008, 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   class MultiUIDefaults extends UIDefaults
   42   {
   43       private UIDefaults[] tables;
   44   
   45       public MultiUIDefaults(UIDefaults[] defaults) {
   46           super();
   47           tables = defaults;
   48       }
   49   
   50       public MultiUIDefaults() {
   51           super();
   52           tables = new UIDefaults[0];
   53       }
   54   
   55       @Override
   56       public Object get(Object key)
   57       {
   58           Object value = super.get(key);
   59           if (value != null) {
   60               return value;
   61           }
   62   
   63           for (UIDefaults table : tables) {
   64               value = (table != null) ? table.get(key) : null;
   65               if (value != null) {
   66                   return value;
   67               }
   68           }
   69   
   70           return null;
   71       }
   72   
   73       @Override
   74       public Object get(Object key, Locale l)
   75       {
   76           Object value = super.get(key,l);
   77           if (value != null) {
   78               return value;
   79           }
   80   
   81           for (UIDefaults table : tables) {
   82               value = (table != null) ? table.get(key,l) : null;
   83               if (value != null) {
   84                   return value;
   85               }
   86           }
   87   
   88           return null;
   89       }
   90   
   91       @Override
   92       public int size() {
   93           return entrySet().size();
   94       }
   95   
   96       @Override
   97       public boolean isEmpty() {
   98           return size() == 0;
   99       }
  100   
  101       @Override
  102       public Enumeration<Object> keys()
  103       {
  104           return new MultiUIDefaultsEnumerator(
  105                   MultiUIDefaultsEnumerator.Type.KEYS, entrySet());
  106       }
  107   
  108       @Override
  109       public Enumeration<Object> elements()
  110       {
  111           return new MultiUIDefaultsEnumerator(
  112                   MultiUIDefaultsEnumerator.Type.ELEMENTS, entrySet());
  113       }
  114   
  115       @Override
  116       public Set<Entry<Object, Object>> entrySet() {
  117           Set<Entry<Object, Object>> set = new HashSet<Entry<Object, Object>>();
  118           for (int i = tables.length - 1; i >= 0; i--) {
  119               if (tables[i] != null) {
  120                   set.addAll(tables[i].entrySet());
  121               }
  122           }
  123           set.addAll(super.entrySet());
  124           return set;
  125       }
  126   
  127       @Override
  128       protected void getUIError(String msg) {
  129           if (tables.length > 0) {
  130               tables[0].getUIError(msg);
  131           } else {
  132               super.getUIError(msg);
  133           }
  134       }
  135   
  136       private static class MultiUIDefaultsEnumerator implements Enumeration<Object>
  137       {
  138           public static enum Type { KEYS, ELEMENTS };
  139           private Iterator<Entry<Object, Object>> iterator;
  140           private Type type;
  141   
  142           MultiUIDefaultsEnumerator(Type type, Set<Entry<Object, Object>> entries) {
  143               this.type = type;
  144               this.iterator = entries.iterator();
  145           }
  146   
  147           public boolean hasMoreElements() {
  148               return iterator.hasNext();
  149           }
  150   
  151           public Object nextElement() {
  152               switch (type) {
  153                   case KEYS: return iterator.next().getKey();
  154                   case ELEMENTS: return iterator.next().getValue();
  155                   default: return null;
  156               }
  157           }
  158       }
  159   
  160       @Override
  161       public Object remove(Object key)
  162       {
  163           Object value = null;
  164           for (int i = tables.length - 1; i >= 0; i--) {
  165               if (tables[i] != null) {
  166                   Object v = tables[i].remove(key);
  167                   if (v != null) {
  168                       value = v;
  169                   }
  170               }
  171           }
  172           Object v = super.remove(key);
  173           if (v != null) {
  174               value = v;
  175           }
  176   
  177           return value;
  178       }
  179   
  180       @Override
  181       public void clear() {
  182           super.clear();
  183           for (UIDefaults table : tables) {
  184               if (table != null) {
  185                   table.clear();
  186               }
  187           }
  188       }
  189   
  190       @Override
  191       public synchronized String toString() {
  192           StringBuffer buf = new StringBuffer();
  193           buf.append("{");
  194           Enumeration keys = keys();
  195           while (keys.hasMoreElements()) {
  196               Object key = keys.nextElement();
  197               buf.append(key + "=" + get(key) + ", ");
  198           }
  199           int length = buf.length();
  200           if (length > 1) {
  201               buf.delete(length-2, length);
  202           }
  203           buf.append("}");
  204           return buf.toString();
  205       }
  206   }

Home » openjdk-7 » javax » swing » [javadoc | source]