jdk/src/share/classes/javax/swing/plaf/basic/LazyActionMap.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 5506 202f599c92aa
child 25565 ce603b34c98d
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An ActionMap that populates its contents as necessary. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * contents are populated by invoking the <code>loadActionMap</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * method on the passed in Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
    38
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class LazyActionMap extends ActionMapUIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Object to invoke <code>loadActionMap</code> on. This may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * a Class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private transient Object _loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Installs an ActionMap that will be populated by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * <code>loadActionMap</code> method on the specified Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * This should be used if the ActionMap can be shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param c JComponent to install the ActionMap on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @param loaderClass Class object that gets loadActionMap invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *                    on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param defaultsKey Key to use to defaults table to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *        existing map and what resulting Map will be registered on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static void installLazyActionMap(JComponent c, Class loaderClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                     String defaultsKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        ActionMap map = (ActionMap)UIManager.get(defaultsKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            map = new LazyActionMap(loaderClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        SwingUtilities.replaceUIActionMap(c, map);
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
     * Returns an ActionMap that will be populated by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <code>loadActionMap</code> method on the specified Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * This should be used if the ActionMap can be shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param c JComponent to install the ActionMap on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param loaderClass Class object that gets loadActionMap invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *                    on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param defaultsKey Key to use to defaults table to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *        existing map and what resulting Map will be registered on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static ActionMap getActionMap(Class loaderClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                  String defaultsKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ActionMap map = (ActionMap)UIManager.get(defaultsKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            map = new LazyActionMap(loaderClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private LazyActionMap(Class loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        _loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void put(Action action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        put(action.getValue(Action.NAME), action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public void put(Object key, Action action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        super.put(key, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public Action get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        super.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        super.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public Object[] keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return super.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return super.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public Object[] allKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return super.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void setParent(ActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        super.setParent(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private void loadIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (_loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            Object loader = _loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            _loader = null;
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   146
            Class<?> klass = (Class<?>)loader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                Method method = klass.getDeclaredMethod("loadActionMap",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                      new Class[] { LazyActionMap.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                method.invoke(klass, new Object[] { this });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                assert false : "LazyActionMap unable to load actions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                assert false : "LazyActionMap unable to load actions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                assert false : "LazyActionMap unable to load actions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        ite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                assert false : "LazyActionMap unable to load actions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}