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