1 19 20 package org.netbeans.modules.java.source.builder; 21 22 import org.netbeans.api.java.source.transform.UndoList; 23 import org.netbeans.api.java.source.transform.UndoEntry; 24 import com.sun.tools.javac.util.Context; 25 26 29 public final class UndoListService implements UndoList { 30 31 protected static final Context.Key<UndoListService> undolistKey = 32 new Context.Key<UndoListService>(); 33 34 public static synchronized UndoListService instance(Context context) { 35 UndoListService instance = context.get(undolistKey); 36 if (instance == null) { 37 instance = new UndoListService(); 38 setInstance(context, instance); 39 } 40 return instance; 41 } 42 43 46 public static void setInstance(Context context, UndoListService undoList) { 47 context.put(undolistKey, undoList); 48 } 49 50 static final UndoEntry nullUndo = new UndoEntry() { 51 public void undo() { throw new Error ("Not Undoable"); } 52 public void redo() { throw new Error ("Not Redoable"); } 53 { endCommand = true; } 54 }; 55 56 UndoEntry undoList = nullUndo; 57 58 public final void add(UndoEntry u) { 59 if (u == null) 60 return; 61 u.addAfter(undoList); 62 undoList = u; 63 } 64 65 70 public final void addAndApply(UndoEntry u) { 71 add(u); 72 u.redo(); 73 } 74 75 public final void undo() { 76 if (canUndo()) 77 do { 78 undoList.undo(); 79 undoList = undoList.prev; 80 } while (!undoList.isEndCommand()); 81 } 82 83 public final void redo() { 84 if (canRedo()) 85 do { 86 undoList = undoList.next; 87 undoList.redo(); 88 } while (canRedo() && !undoList.isEndCommand()); 89 } 90 91 public final boolean canUndo() { 92 return undoList != nullUndo; 93 } 94 95 public final boolean canRedo() { 96 return undoList.next != null; 97 } 98 99 public final void setEndCommand(boolean b) { 100 undoList.setEndCommand(b); 101 } 102 103 public final void clearRedo() { 104 undoList.next = null; 105 } 106 107 public final void reset() { 108 undoList = nullUndo; 109 undoList.next = undoList.prev = null; 110 } 111 112 116 public final boolean atEndCommand() { 117 return undoList.isEndCommand(); 118 } 119 120 public final <T> T getOld(T o) { 121 UndoEntry ue = undoList; 122 while (ue.prev != null) 123 ue = ue.prev; while (ue != null) { 125 T po = ue.getOld(o); 126 if (po != null) 127 return po; 128 ue = ue.next; 129 } 130 return null; 131 } 132 } 133 | Popular Tags |