1 19 20 package org.netbeans.modules.xml.xdm; 21 22 import javax.swing.undo.AbstractUndoableEdit ; 23 import javax.swing.undo.CannotRedoException ; 24 import javax.swing.undo.CannotUndoException ; 25 import javax.swing.undo.UndoableEdit ; 26 import org.netbeans.modules.xml.xdm.nodes.Document; 27 28 29 33 class XDMModelUndoableEdit extends AbstractUndoableEdit { 34 38 private static final long serialVersionUID = -4513245871320808368L; 39 40 public XDMModelUndoableEdit(Document oldDoc, Document newDoc, XDMModel model) { 41 oldDocument = oldDoc; 42 newDocument = newDoc; 43 this.model = model; 44 } 45 46 @Override 47 public void redo() throws CannotRedoException { 48 super.redo(); 49 try { 50 model.resetDocument(newDocument); 51 } catch (RuntimeException ex) { 52 if (newDocument != model.getCurrentDocument()) { 53 CannotRedoException e = new CannotRedoException (); 54 e.initCause(ex); 55 throw e; 56 } else { 57 throw ex; 58 } 59 } 60 } 61 62 @Override 63 public void undo() throws CannotUndoException { 64 super.undo(); 65 try { 66 model.resetDocument(oldDocument); 67 } catch (RuntimeException ex) { 68 if (oldDocument != model.getCurrentDocument()) { 69 CannotUndoException e = new CannotUndoException (); 70 e.initCause(ex); 71 throw e; 72 } else { 73 throw ex; 74 } 75 } 76 } 77 78 @Override 79 public boolean addEdit(UndoableEdit anEdit) { 80 if (anEdit instanceof XDMModelUndoableEdit) { 81 XDMModelUndoableEdit theEdit = (XDMModelUndoableEdit) anEdit; 82 if (newDocument == theEdit.oldDocument) { 83 newDocument = theEdit.newDocument; 84 return true; 85 } 86 } 87 return false; 88 } 89 90 private Document oldDocument; 91 private Document newDocument; 92 private XDMModel model; 93 94 } 95 | Popular Tags |