1 19 package org.openide.awt; 20 21 import org.openide.util.RequestProcessor; 22 import org.openide.util.Task; 23 24 import java.util.LinkedList ; 25 26 import javax.swing.event.*; 27 import javax.swing.undo.*; 28 29 30 44 public interface UndoRedo { 45 48 public static final UndoRedo NONE = new Empty(); 49 50 53 public boolean canUndo(); 54 55 58 public boolean canRedo(); 59 60 63 public void undo() throws CannotUndoException; 64 65 68 public void redo() throws CannotRedoException; 69 70 75 public void addChangeListener(ChangeListener l); 76 77 81 public void removeChangeListener(ChangeListener l); 82 83 87 public String getUndoPresentationName(); 88 89 93 public String getRedoPresentationName(); 94 95 97 public static class Manager extends UndoManager implements UndoRedo { 98 static final long serialVersionUID = 6721367974521509720L; 99 100 101 private final EventListenerList list = new EventListenerList(); 102 103 104 private LinkedList <UndoableEditEvent> runus = new LinkedList <UndoableEditEvent>(); 106 107 private Task task = Task.EMPTY; 109 110 private void superUndoableEditHappened(UndoableEditEvent ue) { 111 super.undoableEditHappened(ue); 112 } 113 114 115 private void superDiscardAllEdits() { 116 super.discardAllEdits(); 117 } 118 119 123 public void undoableEditHappened(final UndoableEditEvent ue) { 124 128 synchronized (runus) { 129 runus.add(ue); 130 } 131 132 updateTask(); 133 } 134 135 136 public void discardAllEdits() { 137 synchronized (runus) { 138 runus.add(null); 139 } 140 141 updateTask(); 142 } 143 144 public boolean canUndo() { 145 148 boolean empty; 149 150 synchronized (runus) { 151 empty = runus.isEmpty(); 152 } 153 154 if (!empty) { 155 task.waitFinished(); 156 } 157 158 return super.canUndo(); 159 } 160 161 private void fireChange() { 162 Object [] l = list.getListenerList(); 163 164 if (l.length == 0) { 165 return; 166 } 167 168 ChangeEvent ev = new ChangeEvent(this); 169 170 for (int i = l.length - 1; i >= 0; i -= 2) { 171 ((ChangeListener) l[i]).stateChanged(ev); 172 } 173 } 174 175 private void updateTask() { 176 179 class R implements Runnable { 180 public void run() { 181 for (;;) { 182 UndoableEditEvent ue; 183 184 synchronized (runus) { 185 if (runus.isEmpty()) { 186 break; 187 } 188 189 ue = runus.removeFirst(); 190 } 191 192 if (ue == null) { 193 superDiscardAllEdits(); 194 } else { 195 superUndoableEditHappened(ue); 196 } 197 198 fireChange(); 199 } 200 } 201 } 202 203 R r = new R(); 204 r.run(); 205 206 } 209 210 214 215 public void addChangeListener(ChangeListener l) { 217 list.add(ChangeListener.class, l); 218 } 219 220 222 public void removeChangeListener(ChangeListener l) { 223 list.remove(ChangeListener.class, l); 224 } 225 226 public String getUndoPresentationName() { 227 return this.canUndo() ? super.getUndoPresentationName() : ""; } 229 230 public String getRedoPresentationName() { 231 return this.canRedo() ? super.getRedoPresentationName() : ""; } 233 } 234 235 237 240 public static final class Empty extends Object implements UndoRedo { 241 public boolean canUndo() { 242 return false; 243 } 244 245 public boolean canRedo() { 246 return false; 247 } 248 249 public void undo() throws CannotUndoException { 250 throw new CannotUndoException(); 251 } 252 253 public void redo() throws CannotRedoException { 254 throw new CannotRedoException(); 255 } 256 257 public void addChangeListener(ChangeListener l) { 258 } 259 260 public void removeChangeListener(ChangeListener l) { 261 } 262 263 public String getUndoPresentationName() { 264 return ""; } 266 267 public String getRedoPresentationName() { 268 return ""; } 270 } 271 } 272 | Popular Tags |