1 7 8 package javax.swing.undo; 9 10 import javax.swing.event.*; 11 import java.util.*; 12 13 19 public class UndoableEditSupport { 20 protected int updateLevel; 21 protected CompoundEdit compoundEdit; 22 protected Vector<UndoableEditListener> listeners; 23 protected Object realSource; 24 25 28 public UndoableEditSupport() { 29 this(null); 30 } 31 32 37 public UndoableEditSupport(Object r) { 38 realSource = r == null ? this : r; 39 updateLevel = 0; 40 compoundEdit = null; 41 listeners = new Vector<UndoableEditListener>(); 42 } 43 44 51 public synchronized void addUndoableEditListener(UndoableEditListener l) { 52 listeners.addElement(l); 53 } 54 55 61 public synchronized void removeUndoableEditListener(UndoableEditListener l) 62 { 63 listeners.removeElement(l); 64 } 65 66 74 public synchronized UndoableEditListener[] getUndoableEditListeners() { 75 return (UndoableEditListener[])(listeners.toArray( 76 new UndoableEditListener[0])); 77 } 78 79 84 protected void _postEdit(UndoableEdit 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 98 public synchronized void postEdit(UndoableEdit e) { 99 if (updateLevel == 0) { 100 _postEdit(e); 101 } else { 102 compoundEdit.addEdit(e); 104 } 105 } 106 107 112 public int getUpdateLevel() { 113 return updateLevel; 114 } 115 116 119 public synchronized void beginUpdate() { 120 if (updateLevel == 0) { 121 compoundEdit = createCompoundEdit(); 122 } 123 updateLevel++; 124 } 125 126 130 protected CompoundEdit createCompoundEdit() { 131 return new CompoundEdit (); 132 } 133 134 139 public synchronized void endUpdate() { 140 updateLevel--; 141 if (updateLevel == 0) { 142 compoundEdit.end(); 143 _postEdit(compoundEdit); 144 compoundEdit = null; 145 } 146 } 147 148 154 public String toString() { 155 return super.toString() + 156 " updateLevel: " + updateLevel + 157 " listeners: " + listeners + 158 " compoundEdit: " + compoundEdit; 159 } 160 } 161 162 163 | Popular Tags |