KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > XDMModelUndoableEdit


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xdm;
21
22 import javax.swing.undo.AbstractUndoableEdit JavaDoc;
23 import javax.swing.undo.CannotRedoException JavaDoc;
24 import javax.swing.undo.CannotUndoException JavaDoc;
25 import javax.swing.undo.UndoableEdit JavaDoc;
26 import org.netbeans.modules.xml.xdm.nodes.Document;
27
28
29 /**
30  *
31  * @author Chris Webster
32  */

33 class XDMModelUndoableEdit extends AbstractUndoableEdit JavaDoc {
34     // Even though AbstractUndoableEdit is serializable this class is not. The
35
// UndoableEdit interface is not Serializable, so this is not required but
36
// just an implementation detail.
37

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 JavaDoc
47     public void redo() throws CannotRedoException JavaDoc {
48         super.redo();
49         try {
50             model.resetDocument(newDocument);
51         } catch (RuntimeException JavaDoc ex) {
52             if (newDocument != model.getCurrentDocument()) {
53                 CannotRedoException JavaDoc e = new CannotRedoException JavaDoc();
54                 e.initCause(ex);
55                 throw e;
56             } else {
57                 throw ex;
58             }
59         }
60     }
61
62     @Override JavaDoc
63     public void undo() throws CannotUndoException JavaDoc {
64         super.undo();
65         try {
66             model.resetDocument(oldDocument);
67         } catch (RuntimeException JavaDoc ex) {
68             if (oldDocument != model.getCurrentDocument()) {
69                 CannotUndoException JavaDoc e = new CannotUndoException JavaDoc();
70                 e.initCause(ex);
71                 throw e;
72             } else {
73                 throw ex;
74             }
75         }
76     }
77     
78         @Override JavaDoc
79         public boolean addEdit(UndoableEdit JavaDoc 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