1 15 16 package swingwtx.swing.undo; 17 18 import swingwtx.swing.event.EventListenerList; 19 import swingwtx.swing.event.UndoableEditEvent; 20 import swingwtx.swing.event.UndoableEditListener; 21 22 28 public class UndoableEditSupport 29 { 30 protected CompoundEdit compoundEdit = null; 31 protected Object realSource = null; 32 33 public UndoableEditSupport() 34 { 35 this(null); 36 } 37 38 public UndoableEditSupport(Object source) 39 { 40 if (source == null) realSource = this; 41 else realSource = source; 42 } 43 44 protected int updateLevel = 0; 45 public int getUpdateLevel() 46 { 47 return updateLevel; 48 } 49 50 private void fireUndoableEditHappened(UndoableEdit undoableEdit) 51 { 52 UndoableEditEvent undoableEditEvent = new UndoableEditEvent(realSource, undoableEdit); 53 Object [] listeners = listenerList.getListenerList(); 54 for (int i=0; i<listeners.length; i++) 55 { 56 ((UndoableEditListener)listeners[i]).undoableEditHappened(undoableEditEvent); 57 } 58 } 59 60 public synchronized void postEdit(UndoableEdit undoableEdit) 61 { 62 if (getUpdateLevel() == 0) 64 fireUndoableEditHappened(undoableEdit); 65 else 67 compoundEdit.addEdit(undoableEdit); 68 } 69 70 protected CompoundEdit createCompoundEdit() 71 { 72 return new CompoundEdit(); 73 } 74 75 public synchronized void beginUpdate() 76 { 77 if (getUpdateLevel() == 0) 79 compoundEdit = createCompoundEdit(); 80 81 updateLevel++; 83 } 84 85 public synchronized void endUpdate() 86 { 87 updateLevel--; 89 90 if (getUpdateLevel() == 0) 91 { 92 compoundEdit.end(); 94 95 fireUndoableEditHappened(compoundEdit); 97 } 98 } 99 100 103 protected EventListenerList listenerList = new EventListenerList(); 104 105 public synchronized void addUndoableEditListener(UndoableEditListener undoableEditListener) 106 { 107 listenerList.add(UndoableEditListener.class, undoableEditListener); 108 } 109 110 public synchronized void removeUndoableEditListener(UndoableEditListener undoableEditListener) 111 { 112 listenerList.remove(UndoableEditListener.class, undoableEditListener); 113 } 114 115 public synchronized UndoableEditListener[] getUndoableEditListeners() 116 { 117 return (UndoableEditListener[]) listenerList.getListenerList(); 118 } 119 } 120 | Popular Tags |