KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > UndoListService


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.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 /**
27  * Singleton instance of UndoList.
28  */

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     /*
44      * Called from reattributor.
45      */

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 JavaDoc("Not Undoable"); }
52     public void redo() { throw new Error JavaDoc("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     /**
66      * Adds an UndoEntry to the UndoList and executes its redo() method.
67      * This execution eliminates the duplicate code often found in redo
68      * and when the caller is run.
69      */

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     /**
113      * Returns true if the last operation was setting the end command.
114      * This method is typically used during cleanup when an operation fails.
115      */

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; // rewind
124
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