jdk/src/share/classes/javax/swing/plaf/basic/LazyActionMap.java
author malenkov
Fri, 26 Jun 2009 16:30:02 +0400
changeset 3104 cce457efb5d8
parent 1639 a97859015238
child 5506 202f599c92aa
permissions -rw-r--r--
6557223: Resize cursor stays after fast outline-resize of JInternalFrame with JScrollPane Reviewed-by: peterz
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: 1290
diff changeset
     2
 * Copyright 2002-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
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class LazyActionMap extends ActionMapUIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * Object to invoke <code>loadActionMap</code> on. This may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * a Class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private transient Object _loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Installs an ActionMap that will be populated by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * <code>loadActionMap</code> method on the specified Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * This should be used if the ActionMap can be shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param c JComponent to install the ActionMap on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param loaderClass Class object that gets loadActionMap invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *                    on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param defaultsKey Key to use to defaults table to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *        existing map and what resulting Map will be registered on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static void installLazyActionMap(JComponent c, Class loaderClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                     String defaultsKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        ActionMap map = (ActionMap)UIManager.get(defaultsKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            map = new LazyActionMap(loaderClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        SwingUtilities.replaceUIActionMap(c, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Returns an ActionMap that will be populated by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <code>loadActionMap</code> method on the specified Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * This should be used if the ActionMap can be shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param c JComponent to install the ActionMap on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param loaderClass Class object that gets loadActionMap invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *                    on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param defaultsKey Key to use to defaults table to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *        existing map and what resulting Map will be registered on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static ActionMap getActionMap(Class loaderClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                  String defaultsKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ActionMap map = (ActionMap)UIManager.get(defaultsKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            map = new LazyActionMap(loaderClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private LazyActionMap(Class loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        _loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void put(Action action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        put(action.getValue(Action.NAME), action);
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 void put(Object key, Action action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        super.put(key, action);
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 Action get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        super.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        super.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public Object[] keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return super.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return super.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public Object[] allKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return super.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void setParent(ActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        loadIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        super.setParent(map);
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 void loadIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (_loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Object loader = _loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            _loader = null;
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   145
            Class<?> klass = (Class<?>)loader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                Method method = klass.getDeclaredMethod("loadActionMap",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                      new Class[] { LazyActionMap.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                method.invoke(klass, new Object[] { this });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                assert false : "LazyActionMap unable to load actions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                assert false : "LazyActionMap unable to load actions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                assert false : "LazyActionMap unable to load actions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        ite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                assert false : "LazyActionMap unable to load actions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        iae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}