jdk/src/share/classes/javax/swing/ActionPropertyChangeListener.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 23697 e556a715949f
permissions -rw-r--r--
8032874: ArrayIndexOutOfBoundsException in JTable while clearing data in JTable Reviewed-by: alexp, alexsch
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: 23010
diff changeset
     2
 * Copyright (c) 1999, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A package-private PropertyChangeListener which listens for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * property changes on an Action and updates the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * of an ActionEvent source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Subclasses must override the actionPropertyChanged method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * which is invoked from the propertyChange method as long as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the target is still valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * WARNING WARNING WARNING WARNING WARNING WARNING:<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Do NOT create an annonymous inner class that extends this!  If you do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * a strong reference will be held to the containing class, which in most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * cases defeats the purpose of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @param T the type of JComponent the underlying Action is attached to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see AbstractButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
    53
@SuppressWarnings("serial") // Bound of type variable  is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
abstract class ActionPropertyChangeListener<T extends JComponent>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        implements PropertyChangeListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static ReferenceQueue<JComponent> queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // WeakReference's aren't serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private transient OwnedWeakReference<T> target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // The Component's that reference an Action do so through a strong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // reference, so that there is no need to check for serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private Action action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static ReferenceQueue<JComponent> getQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        synchronized(ActionPropertyChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (queue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                queue = new ReferenceQueue<JComponent>();
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 queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public ActionPropertyChangeListener(T c, Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        setTarget(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.action = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * PropertyChangeListener method.  If the target has been gc'ed this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * will remove the <code>PropertyChangeListener</code> from the Action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * otherwise this will invoke actionPropertyChanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public final void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        T target = getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            getAction().removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            actionPropertyChanged(target, getAction(), e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Invoked when a property changes on the Action and the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * still exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected abstract void actionPropertyChanged(T target, Action action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                                  PropertyChangeEvent e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private void setTarget(T c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ReferenceQueue<JComponent> queue = getQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // Check to see whether any old buttons have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // been enqueued for GC.  If so, look up their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // PCL instance and remove it from its Action.
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   105
        OwnedWeakReference<?> r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        while ((r = (OwnedWeakReference)queue.poll()) != null) {
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   107
            ActionPropertyChangeListener<?> oldPCL = r.getOwner();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Action oldAction = oldPCL.getAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (oldAction!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                oldAction.removePropertyChangeListener(oldPCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.target = new OwnedWeakReference<T>(c, queue, this);
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 T getTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // Will only happen if serialized and real target was null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return this.target.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Action getAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
          return action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        s.writeObject(getTarget());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                     throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        T target = (T)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            setTarget(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static class OwnedWeakReference<U extends JComponent> extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                              WeakReference<U> {
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   146
        private ActionPropertyChangeListener<?> owner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        OwnedWeakReference(U target, ReferenceQueue<? super U> queue,
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   149
                           ActionPropertyChangeListener<?> owner) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            super(target, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            this.owner = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   154
        public ActionPropertyChangeListener<?> getOwner() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}