jdk/src/java.desktop/share/classes/javax/swing/undo/UndoableEditSupport.java
author darcy
Fri, 03 Apr 2015 10:41:34 -0700
changeset 29894 3e16b51732f5
parent 25859 3317bb8137f4
permissions -rw-r--r--
8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}} Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.undo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A support class used for managing <code>UndoableEdit</code> listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Ray Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class UndoableEditSupport {
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    37
    /**
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    38
     * The update level.
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    39
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected int updateLevel;
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    41
    /**
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    42
     * The compound edit.
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    43
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected CompoundEdit compoundEdit;
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    45
    /**
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    46
     * The list of listeners.
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    47
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    protected Vector<UndoableEditListener> listeners;
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    49
    /**
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    50
     * The real source.
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    51
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected Object realSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Constructs an <code>UndoableEditSupport</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public UndoableEditSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Constructs an <code>UndoableEditSupport</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param r  an <code>Object</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public UndoableEditSupport(Object r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        realSource = r == null ? this : r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        updateLevel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        compoundEdit = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        listeners = new Vector<UndoableEditListener>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Registers an <code>UndoableEditListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The listener is notified whenever an edit occurs which can be undone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param l  an <code>UndoableEditListener</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @see #removeUndoableEditListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public synchronized void addUndoableEditListener(UndoableEditListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        listeners.addElement(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Removes an <code>UndoableEditListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param l  the <code>UndoableEditListener</code> object to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see #addUndoableEditListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public synchronized void removeUndoableEditListener(UndoableEditListener l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        listeners.removeElement(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Returns an array of all the <code>UndoableEditListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * to this UndoableEditSupport with addUndoableEditListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return all of the <code>UndoableEditListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public synchronized UndoableEditListener[] getUndoableEditListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   104
        return listeners.toArray(new UndoableEditListener[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Called only from <code>postEdit</code> and <code>endUpdate</code>. Calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <code>undoableEditHappened</code> in all listeners. No synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * is performed here, since the two calling methods are synchronized.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 5506
diff changeset
   111
     *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 5506
diff changeset
   112
     * @param e edit to be verified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected void _postEdit(UndoableEdit e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        UndoableEditEvent ev = new UndoableEditEvent(realSource, e);
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 24495
diff changeset
   116
        @SuppressWarnings("unchecked")
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 24495
diff changeset
   117
        Enumeration<UndoableEditListener> cursor =
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 24495
diff changeset
   118
            ((Vector<UndoableEditListener>)listeners.clone()).elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        while (cursor.hasMoreElements()) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 24495
diff changeset
   120
            cursor.nextElement().undoableEditHappened(ev);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * DEADLOCK WARNING: Calling this method may call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <code>undoableEditHappened</code> in all listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * It is unwise to call this method from one of its listeners.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 5506
diff changeset
   128
     *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 5506
diff changeset
   129
     * @param e edit to be posted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public synchronized void postEdit(UndoableEdit e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (updateLevel == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            _postEdit(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // PENDING(rjrjr) Throw an exception if this fails?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            compoundEdit.addEdit(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Returns the update level value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return an integer representing the update level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public int getUpdateLevel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return updateLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public synchronized void beginUpdate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (updateLevel == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            compoundEdit = createCompoundEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        updateLevel++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Called only from <code>beginUpdate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Exposed here for subclasses' use.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 5506
diff changeset
   162
     *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 5506
diff changeset
   163
     * @return new created {@code CompoundEdit} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected CompoundEdit createCompoundEdit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return new CompoundEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * DEADLOCK WARNING: Calling this method may call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>undoableEditHappened</code> in all listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * It is unwise to call this method from one of its listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public synchronized void endUpdate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        updateLevel--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (updateLevel == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            compoundEdit.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            _postEdit(compoundEdit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            compoundEdit = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns a string that displays and identifies this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * object's properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return a <code>String</code> representation of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return super.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            " updateLevel: " + updateLevel +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            " listeners: " + listeners +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            " compoundEdit: " + compoundEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}