001/* UndoableEditSupport.java -- 002 Copyright (C) 2002, 2003, 2004, 2005, 2006, Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package javax.swing.undo; 040 041import java.util.Iterator; 042import java.util.Vector; 043 044import javax.swing.event.UndoableEditEvent; 045import javax.swing.event.UndoableEditListener; 046 047/** 048 * A helper class for supporting {@link 049 * javax.swing.event.UndoableEditListener}. 050 * 051 * @author Andrew Selkirk (aselkirk@sympatico.ca) 052 * @author Sascha Brawer (brawer@dandelis.ch) 053 */ 054public class UndoableEditSupport 055{ 056 /** 057 * The number of times that {@link #beginUpdate()} has been called 058 * without a matching call to {@link #endUpdate()}. 059 */ 060 protected int updateLevel; 061 062 063 /** 064 * compoundEdit 065 */ 066 protected CompoundEdit compoundEdit; 067 068 069 /** 070 * The currently registered listeners. 071 */ 072 protected Vector<UndoableEditListener> listeners = 073 new Vector<UndoableEditListener>(); 074 075 076 /** 077 * The source of the broadcast UndoableEditEvents. 078 */ 079 protected Object realSource; 080 081 082 /** 083 * Constructs a new helper for broadcasting UndoableEditEvents. The 084 * events will indicate the newly constructed 085 * <code>UndoableEditSupport</code> instance as their source. 086 * 087 * @see #UndoableEditSupport(java.lang.Object) 088 */ 089 public UndoableEditSupport() 090 { 091 realSource = this; 092 } 093 094 095 /** 096 * Constructs a new helper for broadcasting UndoableEditEvents. 097 * 098 * @param realSource the source of the UndoableEditEvents that will 099 * be broadcast by this helper. If <code>realSource</code> is 100 * <code>null</code>, the events will indicate the newly constructed 101 * <code>UndoableEditSupport</code> instance as their source. 102 */ 103 public UndoableEditSupport(Object realSource) 104 { 105 if (realSource == null) 106 realSource = this; 107 this.realSource = realSource; 108 } 109 110 111 /** 112 * Returns a string representation of this object that may be useful 113 * for debugging. 114 */ 115 public String toString() 116 { 117 // Note that often, this.realSource == this. Therefore, dumping 118 // realSource without additional checks may lead to infinite 119 // recursion. See Classpath bug #7119. 120 return super.toString() + " updateLevel: " + updateLevel 121 + " listeners: " + listeners + " compoundEdit: " + compoundEdit; 122 } 123 124 125 /** 126 * Registers a listener. 127 * 128 * @param val the listener to be added. 129 */ 130 public synchronized void addUndoableEditListener(UndoableEditListener val) 131 { 132 listeners.add(val); 133 } 134 135 136 /** 137 * Unregisters a listener. 138 * @param val the listener to be removed. 139 */ 140 public synchronized void removeUndoableEditListener(UndoableEditListener val) 141 { 142 listeners.removeElement(val); 143 } 144 145 146 /** 147 * Returns an array containing the currently registered listeners. 148 */ 149 public synchronized UndoableEditListener[] getUndoableEditListeners() 150 { 151 UndoableEditListener[] result = new UndoableEditListener[listeners.size()]; 152 return listeners.toArray(result); 153 } 154 155 156 /** 157 * Notifies all registered listeners that an {@link 158 * UndoableEditEvent} has occured. 159 * 160 * <p><b>Lack of Thread Safety:</b> It is <em>not</em> safe to call 161 * this method from concurrent threads, unless the call is protected 162 * by a synchronization on this <code>UndoableEditSupport</code> 163 * instance. 164 * 165 * @param edit the edit action to be posted. 166 */ 167 protected void _postEdit(UndoableEdit edit) 168 { 169 UndoableEditEvent event; 170 Iterator iter; 171 172 // Do nothing if we have no listeners. 173 if (listeners.isEmpty()) 174 return; 175 176 event = new UndoableEditEvent(realSource, edit); 177 178 // We clone the vector because this allows listeners to register 179 // or unregister listeners in their undoableEditHappened method. 180 // Otherwise, this would throw exceptions (in the case of 181 // Iterator, a java.util.ConcurrentModificationException; in the 182 // case of a direct loop over the Vector elements, some 183 // index-out-of-bounds exception). 184 iter = ((Vector) listeners.clone()).iterator(); 185 while (iter.hasNext()) 186 ((UndoableEditListener) iter.next()).undoableEditHappened(event); 187 } 188 189 190 /** 191 * If {@link #beginUpdate} has been called (so that the current 192 * update level is greater than zero), adds the specified edit 193 * to {@link #compoundEdit}. Otherwise, notify listeners of the 194 * edit by calling {@link #_postEdit(UndoableEdit)}. 195 * 196 * <p><b>Thread Safety:</b> It is safe to call this method from any 197 * thread without external synchronization. 198 * 199 * @param edit the edit action to be posted. 200 */ 201 public synchronized void postEdit(UndoableEdit edit) 202 { 203 if (compoundEdit != null) 204 compoundEdit.addEdit(edit); 205 else 206 _postEdit(edit); 207 } 208 209 210 /** 211 * Returns the current update level. 212 */ 213 public int getUpdateLevel() 214 { 215 return updateLevel; 216 } 217 218 219 /** 220 * Starts a (possibly nested) update session. If the current update 221 * level is zero, {@link #compoundEdit} is set to the result of the 222 * {@link #createCompoundEdit} method. In any case, the update level 223 * is increased by one. 224 * 225 * <p><b>Thread Safety:</b> It is safe to call this method from any 226 * thread without external synchronization. 227 */ 228 public synchronized void beginUpdate() 229 { 230 if (compoundEdit == null) 231 compoundEdit = createCompoundEdit(); 232 ++updateLevel; 233 } 234 235 236 /** 237 * Creates a new instance of {@link CompoundEdit}. Called by {@link 238 * #beginUpdate}. If a subclass wants {@link #beginUpdate} to work 239 * on a specific {@link #compoundEdit}, it should override this 240 * method. 241 * 242 * @return a newly created instance of {@link CompoundEdit}. 243 */ 244 protected CompoundEdit createCompoundEdit() 245 { 246 return new CompoundEdit(); 247 } 248 249 250 /** 251 * Ends an update session. If the terminated session was the 252 * outermost session, {@link #compoundEdit} will receive an 253 * <code>end</code> message, and {@link #_postEdit} gets called in 254 * order to notify any listeners. Finally, the 255 * <code>compoundEdit</code> is discarded. 256 * 257 * <p><b>Thread Safety:</b> It is safe to call this method from any 258 * thread without external synchronization. 259 */ 260 public synchronized void endUpdate() 261 { 262 if (updateLevel == 0) 263 throw new IllegalStateException(); 264 265 if (--updateLevel > 0) 266 return; 267 268 compoundEdit.end(); 269 _postEdit(compoundEdit); 270 compoundEdit = null; 271 } 272}