1 /*
2 * Copyright (c) 1999, 2011, 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
33 /**
34 * A package-private PropertyChangeListener which listens for
35 * property changes on an Action and updates the properties
36 * of an ActionEvent source.
37 * <p>
38 * Subclasses must override the actionPropertyChanged method,
39 * which is invoked from the propertyChange method as long as
40 * the target is still valid.
41 * </p>
42 * <p>
43 * WARNING WARNING WARNING WARNING WARNING WARNING:<br>
44 * Do NOT create an annonymous inner class that extends this! If you do
45 * a strong reference will be held to the containing class, which in most
46 * cases defeats the purpose of this class.
47 *
48 * @param T the type of JComponent the underlying Action is attached to
49 *
50 * @author Georges Saab
51 * @see AbstractButton
52 */
53 abstract class ActionPropertyChangeListener<T extends JComponent>
54 implements PropertyChangeListener, Serializable {
55 private static ReferenceQueue<JComponent> queue;
56
57 // WeakReference's aren't serializable.
58 private transient OwnedWeakReference<T> target;
59 // The Component's that reference an Action do so through a strong
60 // reference, so that there is no need to check for serialized.
61 private Action action;
62
63 private static ReferenceQueue<JComponent> getQueue() {
64 synchronized(ActionPropertyChangeListener.class) {
65 if (queue == null) {
66 queue = new ReferenceQueue<JComponent>();
67 }
68 }
69 return queue;
70 }
71
72 public ActionPropertyChangeListener(T c, Action a) {
|
1 /*
2 * Copyright (c) 1999, 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
33 /**
34 * A package-private PropertyChangeListener which listens for
35 * property changes on an Action and updates the properties
36 * of an ActionEvent source.
37 * <p>
38 * Subclasses must override the actionPropertyChanged method,
39 * which is invoked from the propertyChange method as long as
40 * the target is still valid.
41 * </p>
42 * <p>
43 * WARNING WARNING WARNING WARNING WARNING WARNING:<br>
44 * Do NOT create an annonymous inner class that extends this! If you do
45 * a strong reference will be held to the containing class, which in most
46 * cases defeats the purpose of this class.
47 *
48 * @param T the type of JComponent the underlying Action is attached to
49 *
50 * @author Georges Saab
51 * @see AbstractButton
52 */
53 @SuppressWarnings("serial") // Bound of type variable is not serializable across versions
54 abstract class ActionPropertyChangeListener<T extends JComponent>
55 implements PropertyChangeListener, Serializable {
56 private static ReferenceQueue<JComponent> queue;
57
58 // WeakReference's aren't serializable.
59 private transient OwnedWeakReference<T> target;
60 // The Component's that reference an Action do so through a strong
61 // reference, so that there is no need to check for serialized.
62 private Action action;
63
64 private static ReferenceQueue<JComponent> getQueue() {
65 synchronized(ActionPropertyChangeListener.class) {
66 if (queue == null) {
67 queue = new ReferenceQueue<JComponent>();
68 }
69 }
70 return queue;
71 }
72
73 public ActionPropertyChangeListener(T c, Action a) {
|