Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / undo / UndoableEditSupport.java
blob918e735231352567dd2981ca568fb3fa4a4519bc
1 /* UndoableEditSupport.java --
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.undo;
41 import java.util.Iterator;
42 import java.util.Vector;
44 import javax.swing.event.UndoableEditEvent;
45 import javax.swing.event.UndoableEditListener;
47 /**
48 * A helper class for supporting {@link
49 * javax.swing.event.UndoableEditListener}.
51 * @author Andrew Selkirk (aselkirk@sympatico.ca)
52 * @author Sascha Brawer (brawer@dandelis.ch)
54 public class UndoableEditSupport
56 /**
57 * The number of times that {@link #beginUpdate()} has been called
58 * without a matching call to {@link #endUpdate()}.
60 protected int updateLevel;
63 /**
64 * compoundEdit
66 protected CompoundEdit compoundEdit;
69 /**
70 * The currently registered listeners.
72 protected Vector listeners = new Vector();
75 /**
76 * The source of the broadcast UndoableEditEvents.
78 protected Object realSource;
81 /**
82 * Constructs a new helper for broadcasting UndoableEditEvents. The
83 * events will indicate the newly constructed
84 * <code>UndoableEditSupport</code> instance as their source.
86 * @see #UndoableEditSupport(java.lang.Object)
88 public UndoableEditSupport()
90 realSource = this;
94 /**
95 * Constructs a new helper for broadcasting UndoableEditEvents.
97 * @param realSource the source of the UndoableEditEvents that will
98 * be broadcast by this helper. If <code>realSource</code> is
99 * <code>null</code>, the events will indicate the newly constructed
100 * <code>UndoableEditSupport</code> instance as their source.
102 public UndoableEditSupport(Object realSource)
104 if (realSource == null)
105 realSource = this;
106 this.realSource = realSource;
111 * Returns a string representation of this object that may be useful
112 * for debugging.
114 public String toString()
116 // Note that often, this.realSource == this. Therefore, dumping
117 // realSource without additional checks may lead to infinite
118 // recursion. See Classpath bug #7119.
119 return super.toString() + " updateLevel: " + updateLevel
120 + " listeners: " + listeners + " compoundEdit: " + compoundEdit;
125 * Registers a listener.
127 * @param val the listener to be added.
129 public synchronized void addUndoableEditListener(UndoableEditListener val)
131 listeners.add(val);
136 * Unregisters a listener.
137 * @param val the listener to be removed.
139 public synchronized void removeUndoableEditListener(UndoableEditListener val)
141 listeners.removeElement(val);
146 * Returns an array containing the currently registered listeners.
148 public synchronized UndoableEditListener[] getUndoableEditListeners()
150 UndoableEditListener[] result = new UndoableEditListener[listeners.size()];
151 return (UndoableEditListener[]) listeners.toArray(result);
156 * Notifies all registered listeners that an {@link
157 * UndoableEditEvent} has occured.
159 * <p><b>Lack of Thread Safety:</b> It is <em>not</em> safe to call
160 * this method from concurrent threads, unless the call is protected
161 * by a synchronization on this <code>UndoableEditSupport</code>
162 * instance.
164 * @param edit the edit action to be posted.
166 protected void _postEdit(UndoableEdit edit)
168 UndoableEditEvent event;
169 Iterator iter;
171 // Do nothing if we have no listeners.
172 if (listeners.isEmpty())
173 return;
175 event = new UndoableEditEvent(realSource, edit);
177 // We clone the vector because this allows listeners to register
178 // or unregister listeners in their undoableEditHappened method.
179 // Otherwise, this would throw exceptions (in the case of
180 // Iterator, a java.util.ConcurrentModificationException; in the
181 // case of a direct loop over the Vector elements, some
182 // index-out-of-bounds exception).
183 iter = ((Vector) listeners.clone()).iterator();
184 while (iter.hasNext())
185 ((UndoableEditListener) iter.next()).undoableEditHappened(event);
190 * If {@link #beginUpdate} has been called (so that the current
191 * update level is greater than zero), adds the specified edit
192 * to {@link #compoundEdit}. Otherwise, notify listeners of the
193 * edit by calling {@link #_postEdit(UndoableEdit)}.
195 * <p><b>Thread Safety:</b> It is safe to call this method from any
196 * thread without external synchronization.
198 * @param edit the edit action to be posted.
200 public synchronized void postEdit(UndoableEdit edit)
202 if (compoundEdit != null)
203 compoundEdit.addEdit(edit);
204 else
205 _postEdit(edit);
210 * Returns the current update level.
212 public int getUpdateLevel()
214 return updateLevel;
219 * Starts a (possibly nested) update session. If the current update
220 * level is zero, {@link #compoundEdit} is set to the result of the
221 * {@link #createCompoundEdit} method. In any case, the update level
222 * is increased by one.
224 * <p><b>Thread Safety:</b> It is safe to call this method from any
225 * thread without external synchronization.
227 public synchronized void beginUpdate()
229 if (compoundEdit == null)
230 compoundEdit = createCompoundEdit();
231 ++updateLevel;
236 * Creates a new instance of {@link CompoundEdit}. Called by {@link
237 * #beginUpdate}. If a subclass wants {@link #beginUpdate} to work
238 * on a specific {@link #compoundEdit}, it should override this
239 * method.
241 * @returns a newly created instance of {@link CompoundEdit}.
243 protected CompoundEdit createCompoundEdit()
245 return new CompoundEdit();
250 * Ends an update session. If the terminated session was the
251 * outermost session, {@link #compoundEdit} will receive an
252 * <code>end</code> message, and {@link #_postEdit} gets called in
253 * order to notify any listeners. Finally, the
254 * <code>compoundEdit</code> is discarded.
256 * <p><b>Thread Safety:</b> It is safe to call this method from any
257 * thread without external synchronization.
259 public synchronized void endUpdate()
261 if (updateLevel == 0)
262 throw new IllegalStateException();
264 if (--updateLevel > 0)
265 return;
267 compoundEdit.end();
268 _postEdit(compoundEdit);
269 compoundEdit = null;