KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2    SwingWT
3    Copyright(c)2003-2004 Daniel Naab
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: dannaab@users.sourceforge.net
9
10    $Log: UndoableEditSupport.java,v $
11    Revision 1.2 2004/04/16 22:45:50 dannaab
12    Add copyright msg
13
14 */

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 /**
23  * UndoableEditSupport
24  *
25  * @author Naab
26  * @version %I%, %G%
27  */

28 public class UndoableEditSupport
29 {
30     protected CompoundEdit compoundEdit = null;
31     protected Object JavaDoc realSource = null;
32
33     public UndoableEditSupport()
34     {
35         this(null);
36     }
37
38     public UndoableEditSupport(Object JavaDoc 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 JavaDoc[] 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 not updating
63
if (getUpdateLevel() == 0)
64             fireUndoableEditHappened(undoableEdit);
65         // otherwise add this edit to the compound edit queue
66
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         // Create a new compound edit if we don't have one
78
if (getUpdateLevel() == 0)
79             compoundEdit = createCompoundEdit();
80
81         // increment the count of edits
82
updateLevel++;
83     }
84
85     public synchronized void endUpdate()
86     {
87         // decrement the counter
88
updateLevel--;
89
90         if (getUpdateLevel() == 0)
91         {
92             // Finish the edit
93
compoundEdit.end();
94
95             // Notify listeners
96
fireUndoableEditHappened(compoundEdit);
97         }
98     }
99
100     /**
101      * Listener support
102      */

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