Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / commands / AbstractCommandsRecord.java @ 24496

History | View | Annotate | Download (6.56 KB)

1
package org.gvsig.fmap.dal.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.dal.exceptions.DataException;
8
import org.gvsig.fmap.dal.feature.*;
9
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
10
import org.gvsig.fmap.dal.feature.impl.commands.implementation.FeatureCommand;
11
import org.gvsig.tools.observer.Observer;
12
import org.gvsig.tools.observer.WeakReferencingObservable;
13
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
14

    
15
/**
16
 * Clase en memoria para registrar y gestionar los comandos que vamos
17
 * realizando. La forma en que ha sido implementada esta clase, en vez de una
18
 * ?nica lista para albergar los comandos de deshacer(undos) y los de
19
 * rehacer(redos), se ha optado por dos pilas una para deshacer(undos) y otra
20
 * para rehacer(redos), de esta forma :  Cuando se a?ade un nuevo comando, se
21
 * inserta este a la pila de deshacer(undos) y se borra de la de
22
 * rehacer(redos). Si se realiza un deshacer se desapila este comando de la
23
 * pila deshacer(undos) y se apila en la de rehacer(redos). Y de la misma
24
 * forma cuando se realiza un rehacer se desapila este comando de la pila de
25
 * rehacer(redos) y pasa a la de deshacer(undos).
26
 *
27
 * @author Vicente Caballero Navarro
28
 */
29
public abstract class AbstractCommandsRecord implements CommandsRecord, WeakReferencingObservable{
30
        private Stack undos = new Stack();
31
        private Stack redos = new Stack();
32
        private boolean refresh=true;
33
        private int undosCount=0;
34
        private DelegateWeakReferencingObservable delegateObservable = new DelegateWeakReferencingObservable(this);
35
        private boolean complex=false;
36
        private CommandCollection collection=null;
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
                        delegateObservable.notifyObservers(new DefaultCommandNotification(command,
46
                    CommandNotification.ADD));
47
                }
48
        }
49

    
50
        public void undo() throws DataException {
51
                AbstractCommand command = (AbstractCommand) undos.pop();
52
                command.undo();
53
                redos.add(command);
54
                delegateObservable.notifyObservers(new DefaultCommandNotification(command,
55
                CommandNotification.UNDO));
56
        }
57

    
58
        public void redo() throws DataException {
59
                AbstractCommand command = (AbstractCommand) redos.pop();
60
                command.redo();
61
                undos.add(command);
62
                delegateObservable.notifyObservers(new DefaultCommandNotification(command,
63
                CommandNotification.REDO));
64
        }
65

    
66
        public boolean moreUndoCommands() {
67
                return (!undos.isEmpty());
68
        }
69

    
70
        public boolean moreRedoCommands() {
71
                return (!redos.isEmpty());
72
        }
73

    
74
        public List getUndoCommands(){
75
                Stack clonedUndos=(Stack)undos.clone();
76

    
77
                ArrayList commands=new ArrayList();
78
                while (!clonedUndos.isEmpty()){
79
                        commands.add(clonedUndos.pop());
80
                }
81

    
82
                return commands;
83
        }
84

    
85
        public List getRedoCommands(){
86
                Stack clonedRedos=(Stack)redos.clone();
87

    
88
                ArrayList commands=new ArrayList();
89
                while (!clonedRedos.isEmpty()){
90
                        commands.add(clonedRedos.pop());
91
                }
92
                return commands;
93
        }
94

    
95
        public int getPointer(){
96
                if (refresh){
97
                        undosCount=undos.size()-1;
98
                }
99
                return undosCount;
100
        }
101

    
102
        public void setPointer(int newpos) throws DataException {
103
                int pos=getPointer();
104
                if (newpos>pos){
105
                        for (int i=pos;i<newpos;i++){
106
                                redo();
107
                                System.out.println("redos = "+i);
108
                        }
109
                }else if (pos>newpos){
110
                        for (int i=pos-1;i>=newpos;i--){
111
                                undo();
112
                                System.out.println("undos = "+i);
113
                        }
114
                }
115
        }
116

    
117
        public int size() {
118
                return undos.size()+redos.size();
119
        }
120

    
121
        public void clear() {
122
                redos.clear();
123
                undos.clear();
124
        }
125

    
126
        public Command getNextUndoCommand() {
127
                return (Command)undos.peek();
128
        }
129

    
130
        public Command getNextRedoCommand() {
131
                return (Command)redos.peek();
132
        }
133

    
134
        public void addObserver(Observer o) {
135
                delegateObservable.addObserver(o);
136

    
137
        }
138

    
139
        public void deleteObserver(Observer o) {
140
                delegateObservable.deleteObserver(o);
141
        }
142

    
143
        public void deleteObservers() {
144
                delegateObservable.deleteObservers();
145
        }
146
/**
147
 * @deprecated
148
 * @return
149
 */
150
        public List getCommandsFeatureDeleted() { // List of Feature
151
                Stack clonedUndos=(Stack)undos.clone();
152

    
153
                ArrayList commands=new ArrayList();
154
                while (!clonedUndos.isEmpty()){
155
                        Command command=(Command)clonedUndos.pop();
156
                        if (command.getType().equals(Command.DELETE) && command instanceof FeatureCommand) {
157
                                commands.add(command);
158
                        }
159
                }
160

    
161
                return commands;
162
        }
163
        /**
164
         * @deprecated
165
         * @return
166
         */
167
        public List getCommandsFeatureUpdated() { // List of Feature
168
                Stack clonedUndos=(Stack)undos.clone();
169

    
170
                ArrayList commands=new ArrayList();
171
                while (!clonedUndos.isEmpty()){
172
                        Command command=(Command)clonedUndos.pop();
173
                        if (command.getType().equals(Command.UPDATE) && command instanceof FeatureCommand) {
174
                                commands.add(command);
175
                        }
176
                }
177

    
178
                return commands;
179
        }
180
        /**
181
         * @deprecated
182
         * @return
183
         */
184
        public List getCommandsFeatureInserted() { // List of Feature
185
                Stack clonedUndos=(Stack)undos.clone();
186

    
187
                ArrayList commands=new ArrayList();
188
                while (!clonedUndos.isEmpty()){
189
                        Command command=(Command)clonedUndos.pop();
190
                        if (command.getType().equals(Command.INSERT) && command instanceof FeatureCommand) {
191
                                commands.add(command);
192
                        }
193
                }
194

    
195
                return commands;
196
        }
197

    
198
        public void endComplex() {
199
                if (collection.isEmpty()) {
200
                complex = false;
201
                return;
202
        }
203
                complex=false;
204
                undos.add(collection);
205
                redos.clear();
206
                refresh=true;
207
                delegateObservable.notifyObservers(new DefaultCommandNotification(collection,
208
                CommandNotification.ADD));
209

    
210
        }
211

    
212
        public void startComplex(String description) {
213
                collection=new CommandCollection(description);
214
                complex=true;
215
        }
216

    
217
        public abstract void delete(Object obj);
218

    
219
        public abstract void insert(Object obj);
220

    
221
        public abstract void update(Object obj, Object oldObj);
222

    
223
        public abstract void select(
224
                        FeatureReferenceSelection selection,
225
                        FeatureReference reference);
226

    
227
        public abstract void deselect(
228
                        FeatureReferenceSelection selection,
229
                        FeatureReference reference);
230

    
231
        public abstract void selectAll(
232
                        FeatureReferenceSelection selection)
233
            throws DataException;
234

    
235
        public abstract void deselectAll(
236
                        FeatureReferenceSelection selection)
237
            throws DataException;
238

    
239
        public abstract void selectionReverse(
240
                        FeatureReferenceSelection selection);
241

    
242
        public abstract void selectionSet(DefaultFeatureStore store,
243
                        FeatureReferenceSelection oldSelection,
244
                        FeatureReferenceSelection newSelection);
245
}