svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / impl / commands / AbstractCommandsRecord.java @ 24017
History | View | Annotate | Download (5.41 KB)
1 |
package org.gvsig.fmap.data.feature.impl.commands; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import java.util.Stack; |
6 |
|
7 |
import org.gvsig.fmap.data.feature.Command; |
8 |
import org.gvsig.fmap.data.feature.CommandsRecord; |
9 |
import org.gvsig.fmap.data.feature.impl.commands.implementation.FeatureCommand; |
10 |
import org.gvsig.tools.observer.DefaultObservable; |
11 |
import org.gvsig.tools.observer.Observer; |
12 |
|
13 |
/**
|
14 |
* Clase en memoria para registrar y gestionar los comandos que vamos
|
15 |
* realizando. La forma en que ha sido implementada esta clase, en vez de una
|
16 |
* ?nica lista para albergar los comandos de deshacer(undos) y los de
|
17 |
* rehacer(redos), se ha optado por dos pilas una para deshacer(undos) y otra
|
18 |
* para rehacer(redos), de esta forma : Cuando se a?ade un nuevo comando, se
|
19 |
* inserta este a la pila de deshacer(undos) y se borra de la de
|
20 |
* rehacer(redos). Si se realiza un deshacer se desapila este comando de la
|
21 |
* pila deshacer(undos) y se apila en la de rehacer(redos). Y de la misma
|
22 |
* forma cuando se realiza un rehacer se desapila este comando de la pila de
|
23 |
* rehacer(redos) y pasa a la de deshacer(undos).
|
24 |
*
|
25 |
* @author Vicente Caballero Navarro
|
26 |
*/
|
27 |
public abstract class AbstractCommandsRecord implements CommandsRecord { |
28 |
private Stack undos = new Stack(); |
29 |
private Stack redos = new Stack(); |
30 |
private boolean refresh=true; |
31 |
private int undosCount=0; |
32 |
private DefaultObservable observable=new DefaultObservable(); |
33 |
private boolean complex=false; |
34 |
private CommandCollection collection=null; |
35 |
|
36 |
public void add(Command command){ |
37 |
if (complex){
|
38 |
collection.add(command); |
39 |
}else{
|
40 |
undos.add(command); |
41 |
redos.clear(); |
42 |
refresh=true;
|
43 |
observable.notifyObservers(this,new CommandNotification(command,CommandNotification.ADD)); |
44 |
} |
45 |
} |
46 |
|
47 |
public void undo() { |
48 |
AbstractCommand command = (AbstractCommand) undos.pop(); |
49 |
command.undo(); |
50 |
redos.add(command); |
51 |
observable.notifyObservers(this,new CommandNotification(command,CommandNotification.UNDO)); |
52 |
} |
53 |
|
54 |
public void redo() { |
55 |
AbstractCommand command = (AbstractCommand) redos.pop(); |
56 |
command.redo(); |
57 |
undos.add(command); |
58 |
observable.notifyObservers(this,new CommandNotification(command,CommandNotification.REDO)); |
59 |
} |
60 |
|
61 |
public boolean moreUndoCommands() { |
62 |
return (!undos.isEmpty());
|
63 |
} |
64 |
|
65 |
public boolean moreRedoCommands() { |
66 |
return (!redos.isEmpty());
|
67 |
} |
68 |
|
69 |
public List getUndoCommands(){ |
70 |
Stack clonedUndos=(Stack)undos.clone(); |
71 |
|
72 |
ArrayList commands=new ArrayList(); |
73 |
while (!clonedUndos.isEmpty()){
|
74 |
commands.add(clonedUndos.pop()); |
75 |
} |
76 |
|
77 |
return commands;
|
78 |
} |
79 |
|
80 |
public List getRedoCommands(){ |
81 |
Stack clonedRedos=(Stack)redos.clone(); |
82 |
|
83 |
ArrayList commands=new ArrayList(); |
84 |
while (!clonedRedos.isEmpty()){
|
85 |
commands.add(clonedRedos.pop()); |
86 |
} |
87 |
return commands;
|
88 |
} |
89 |
|
90 |
public int getPointer(){ |
91 |
if (refresh){
|
92 |
undosCount=undos.size()-1;
|
93 |
} |
94 |
return undosCount;
|
95 |
} |
96 |
|
97 |
public void setPointer(int newpos){ |
98 |
int pos=getPointer();
|
99 |
if (newpos>pos){
|
100 |
for (int i=pos;i<newpos;i++){ |
101 |
redo(); |
102 |
System.out.println("redos = "+i); |
103 |
} |
104 |
}else if (pos>newpos){ |
105 |
for (int i=pos-1;i>=newpos;i--){ |
106 |
undo(); |
107 |
System.out.println("undos = "+i); |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
public int size() { |
113 |
return undos.size()+redos.size();
|
114 |
} |
115 |
|
116 |
public void clear() { |
117 |
redos.clear(); |
118 |
undos.clear(); |
119 |
} |
120 |
|
121 |
public Command getNextUndoCommand() {
|
122 |
return (Command)undos.peek();
|
123 |
} |
124 |
|
125 |
public Command getNextRedoCommand() {
|
126 |
return (Command)redos.peek();
|
127 |
} |
128 |
|
129 |
public void addObserver(Observer o) { |
130 |
observable.addObserver(o); |
131 |
|
132 |
} |
133 |
|
134 |
public void deleteObserver(Observer o) { |
135 |
observable.deleteObserver(o); |
136 |
} |
137 |
|
138 |
public void deleteObservers() { |
139 |
observable.deleteObservers(); |
140 |
} |
141 |
|
142 |
public List getCommandsFeatureDeleted() { // List of Feature |
143 |
Stack clonedUndos=(Stack)undos.clone(); |
144 |
|
145 |
ArrayList commands=new ArrayList(); |
146 |
while (!clonedUndos.isEmpty()){
|
147 |
Command command=(Command)clonedUndos.pop(); |
148 |
if (command.getType().equals(Command.DELETE) && command instanceof FeatureCommand) { |
149 |
commands.add(command); |
150 |
} |
151 |
} |
152 |
|
153 |
return commands;
|
154 |
} |
155 |
|
156 |
public List getCommandsFeatureUpdated() { // List of Feature |
157 |
Stack clonedUndos=(Stack)undos.clone(); |
158 |
|
159 |
ArrayList commands=new ArrayList(); |
160 |
while (!clonedUndos.isEmpty()){
|
161 |
Command command=(Command)clonedUndos.pop(); |
162 |
if (command.getType().equals(Command.UPDATE) && command instanceof FeatureCommand) { |
163 |
commands.add(command); |
164 |
} |
165 |
} |
166 |
|
167 |
return commands;
|
168 |
} |
169 |
public List getCommandsFeatureInserted() { // List of Feature |
170 |
Stack clonedUndos=(Stack)undos.clone(); |
171 |
|
172 |
ArrayList commands=new ArrayList(); |
173 |
while (!clonedUndos.isEmpty()){
|
174 |
Command command=(Command)clonedUndos.pop(); |
175 |
if (command.getType().equals(Command.INSERT) && command instanceof FeatureCommand) { |
176 |
commands.add(command); |
177 |
} |
178 |
} |
179 |
|
180 |
return commands;
|
181 |
} |
182 |
|
183 |
public void endComplex(String description) { |
184 |
if (collection.isEmpty()) {
|
185 |
complex = false;
|
186 |
return;
|
187 |
} |
188 |
complex=false;
|
189 |
collection.setDescription(description); |
190 |
undos.add(collection); |
191 |
redos.clear(); |
192 |
refresh=true;
|
193 |
observable.notifyObservers(this,new CommandNotification(collection,CommandNotification.ADD)); |
194 |
|
195 |
} |
196 |
|
197 |
public void startComplex() { |
198 |
collection=new CommandCollection();
|
199 |
complex=true;
|
200 |
} |
201 |
|
202 |
public abstract void delete(Object obj); |
203 |
|
204 |
public abstract void insert(Object obj); |
205 |
|
206 |
public abstract void update(Object obj, Object oldObj); |
207 |
|
208 |
} |