1 19 20 package org.netbeans.modules.xml.xam.ui.undo; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.swing.event.UndoableEditListener ; 26 import javax.swing.text.AbstractDocument ; 27 import javax.swing.undo.CannotRedoException ; 28 import javax.swing.undo.CannotUndoException ; 29 import org.netbeans.modules.xml.xam.Model; 30 import org.openide.awt.UndoRedo; 31 32 52 public class QuietUndoManager extends CompoundUndoManager { 53 54 private static final long serialVersionUID = 1L; 55 56 private AbstractDocument document; 57 58 private Model model; 59 private List <Model> otherModels = new ArrayList <Model>(); 60 61 66 public QuietUndoManager(UndoRedo.Manager original) { 67 super(original); 68 } 69 70 75 private void finish(UndoableEditListener [] listeners) { 76 if (model != null) { 77 model.addUndoableEditListener(this); 80 } 81 if (listeners != null && listeners.length > 0) { 82 for (UndoableEditListener uel : listeners) { 83 document.addUndoableEditListener(uel); 84 } 85 } 86 } 87 88 93 private UndoableEditListener [] prepare() { 94 if (model != null) { 95 model.removeUndoableEditListener(this); 100 } 101 if (document == null) { 102 return null; 103 } 104 UndoableEditListener [] listeners = document.getUndoableEditListeners(); 105 if (listeners != null && listeners.length > 0) { 106 for (UndoableEditListener uel : listeners) { 107 document.removeUndoableEditListener(uel); 108 } 109 } 110 return listeners; 111 } 112 113 119 public void setDocument(AbstractDocument document) { 120 this.document = document; 121 } 122 123 131 public void setModel(Model model) { 132 this.model = model; 133 } 134 135 141 private void syncModel() { 142 if (model != null) { 143 try { 144 model.sync(); 149 for(Model m: otherModels) 150 m.sync(); 151 } catch (IOException ioe) { 152 } 154 } 155 } 156 157 public void redo() throws CannotRedoException { 158 UndoableEditListener [] listeners = prepare(); 159 super.redo(); 160 syncModel(); 161 finish(listeners); 162 } 163 164 public void undo() throws CannotUndoException { 165 UndoableEditListener [] listeners = prepare(); 166 super.undo(); 167 syncModel(); 168 finish(listeners); 169 } 170 171 174 public void addWrapperModel(Model model) { 175 if(!otherModels.contains(model)) 176 otherModels.add(model); 177 } 178 179 182 public void removeWrapperModel(Model model) { 183 otherModels.remove(model); 184 } 185 } 186 | Popular Tags |