KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > undo > UndoableEditSupport


1 /*
2  * @(#)UndoableEditSupport.java 1.20 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.undo;
9
10 import javax.swing.event.*;
11 import java.util.*;
12
13 /**
14  * A support class used for managing <code>UndoableEdit</code> listeners.
15  *
16  * @author Ray Ryan
17  * @version 1.20 05/05/04
18  */

19 public class UndoableEditSupport {
20     protected int updateLevel;
21     protected CompoundEdit JavaDoc compoundEdit;
22     protected Vector<UndoableEditListener> listeners;
23     protected Object JavaDoc realSource;
24
25     /**
26      * Constructs an <code>UndoableEditSupport</code> object.
27      */

28     public UndoableEditSupport() {
29     this(null);
30     }
31
32     /**
33      * Constructs an <code>UndoableEditSupport</code> object.
34      *
35      * @param r an <code>Object</code>
36      */

37     public UndoableEditSupport(Object JavaDoc r) {
38     realSource = r == null ? this : r;
39     updateLevel = 0;
40     compoundEdit = null;
41     listeners = new Vector<UndoableEditListener>();
42     }
43
44     /**
45      * Registers an <code>UndoableEditListener</code>.
46      * The listener is notified whenever an edit occurs which can be undone.
47      *
48      * @param l an <code>UndoableEditListener</code> object
49      * @see #removeUndoableEditListener
50      */

51     public synchronized void addUndoableEditListener(UndoableEditListener l) {
52     listeners.addElement(l);
53     }
54
55     /**
56      * Removes an <code>UndoableEditListener</code>.
57      *
58      * @param l the <code>UndoableEditListener</code> object to be removed
59      * @see #addUndoableEditListener
60      */

61     public synchronized void removeUndoableEditListener(UndoableEditListener l)
62     {
63     listeners.removeElement(l);
64     }
65
66     /**
67      * Returns an array of all the <code>UndoableEditListener</code>s added
68      * to this UndoableEditSupport with addUndoableEditListener().
69      *
70      * @return all of the <code>UndoableEditListener</code>s added or an empty
71      * array if no listeners have been added
72      * @since 1.4
73      */

74     public synchronized UndoableEditListener[] getUndoableEditListeners() {
75         return (UndoableEditListener[])(listeners.toArray(
76                 new UndoableEditListener[0]));
77     }
78
79     /**
80      * Called only from <code>postEdit</code> and <code>endUpdate</code>. Calls
81      * <code>undoableEditHappened</code> in all listeners. No synchronization
82      * is performed here, since the two calling methods are synchronized.
83      */

84     protected void _postEdit(UndoableEdit JavaDoc e) {
85     UndoableEditEvent ev = new UndoableEditEvent(realSource, e);
86     Enumeration cursor = ((Vector)listeners.clone()).elements();
87     while (cursor.hasMoreElements()) {
88         ((UndoableEditListener)cursor.nextElement()).
89         undoableEditHappened(ev);
90     }
91     }
92     
93     /**
94      * DEADLOCK WARNING: Calling this method may call
95      * <code>undoableEditHappened</code> in all listeners.
96      * It is unwise to call this method from one of its listeners.
97      */

98     public synchronized void postEdit(UndoableEdit JavaDoc e) {
99     if (updateLevel == 0) {
100         _postEdit(e);
101     } else {
102         // PENDING(rjrjr) Throw an exception if this fails?
103
compoundEdit.addEdit(e);
104     }
105     }
106
107     /**
108      * Returns the update level value.
109      *
110      * @return an integer representing the update level
111      */

112     public int getUpdateLevel() {
113     return updateLevel;
114     }
115
116     /**
117      *
118      */

119     public synchronized void beginUpdate() {
120     if (updateLevel == 0) {
121         compoundEdit = createCompoundEdit();
122     }
123     updateLevel++;
124     }
125
126     /**
127      * Called only from <code>beginUpdate</code>.
128      * Exposed here for subclasses' use.
129      */

130     protected CompoundEdit JavaDoc createCompoundEdit() {
131     return new CompoundEdit JavaDoc();
132     }
133
134     /**
135      * DEADLOCK WARNING: Calling this method may call
136      * <code>undoableEditHappened</code> in all listeners.
137      * It is unwise to call this method from one of its listeners.
138      */

139     public synchronized void endUpdate() {
140     updateLevel--;
141     if (updateLevel == 0) {
142         compoundEdit.end();
143         _postEdit(compoundEdit);
144         compoundEdit = null;
145     }
146     }
147     
148     /**
149      * Returns a string that displays and identifies this
150      * object's properties.
151      *
152      * @return a <code>String</code> representation of this object
153      */

154     public String JavaDoc toString() {
155     return super.toString() +
156         " updateLevel: " + updateLevel +
157         " listeners: " + listeners +
158         " compoundEdit: " + compoundEdit;
159     }
160 }
161
162
163
Popular Tags