Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / undo / command / impl / DefaultUndoRedoCommandStack.java @ 24961

History | View | Annotate | Download (4.54 KB)

1
package org.gvsig.tools.undo.command.impl;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Stack;
6

    
7
import org.gvsig.tools.observer.Observer;
8
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
9
import org.gvsig.tools.undo.RedoException;
10
import org.gvsig.tools.undo.UndoException;
11
import org.gvsig.tools.undo.command.Command;
12
import org.gvsig.tools.undo.command.CommandNotification;
13
import org.gvsig.tools.undo.command.UndoRedoCommandStack;
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 rehacer(redos).
22
 * Si se realiza un deshacer se desapila este comando de la pila deshacer(undos)
23
 * y se apila en la de rehacer(redos). Y de la misma forma cuando se realiza un
24
 * rehacer se desapila este comando de la pila de rehacer(redos) y pasa a la de
25
 * deshacer(undos).
26
 * 
27
 * @author Vicente Caballero Navarro
28
 */
29
public class DefaultUndoRedoCommandStack implements UndoRedoCommandStack {
30
        private Stack undos = new Stack();
31
    private Stack redos = new Stack();
32
    private DelegateWeakReferencingObservable delegateObservable = new DelegateWeakReferencingObservable(
33
            this);
34
    private boolean complex = false;
35
    private CompoundCommand collection = null;
36

    
37
        public void add(Command command) {
38
        if (complex) {
39
            collection.add(command);
40
        } else {
41
            undos.add(command);
42
            redos.clear();
43
            delegateObservable.notifyObservers(new DefaultCommandNotification(
44
                    command, CommandNotification.ADD));
45
        }
46
    }
47

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

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

    
64
    public void redo(int commands) throws RedoException {
65
        for (int i = 0; i < commands; i++) {
66
            redo();
67
        }
68
    }
69

    
70
    public void undo(int commands) throws UndoException {
71
        for (int i = 0; i < commands; i++) {
72
            undo();
73
        }
74
    }
75
    
76
        public boolean canUndo() {
77
        return (!undos.isEmpty());
78
    }
79

    
80
        public boolean canRedo() {
81
        return (!redos.isEmpty());
82
    }
83

    
84
        public List getUndoInfos() {
85
        Stack clonedUndos = (Stack) undos.clone();
86

    
87
                ArrayList commands = new ArrayList();
88
        while (!clonedUndos.isEmpty()) {
89
            commands.add(clonedUndos.pop());
90
        }
91

    
92
                return commands;
93
    }
94

    
95
        public List getRedoInfos() {
96
        Stack clonedRedos = (Stack) redos.clone();
97

    
98
                ArrayList commands = new ArrayList();
99
        while (!clonedRedos.isEmpty()) {
100
            commands.add(clonedRedos.pop());
101
        }
102
        return commands;
103
    }
104

    
105
        public int size() {
106
        return undos.size() + redos.size();
107
    }
108

    
109
        public void clear() {
110
        redos.clear();
111
        undos.clear();
112
    }
113

    
114
        public Command getNextUndoCommand() {
115
        return (Command) undos.peek();
116
    }
117

    
118
        public Command getNextRedoCommand() {
119
        return (Command) redos.peek();
120
    }
121

    
122
        public void addObserver(Observer o) {
123
        delegateObservable.addObserver(o);
124

    
125
        }
126

    
127
        public void deleteObserver(Observer o) {
128
        delegateObservable.deleteObserver(o);
129
    }
130

    
131
        public void deleteObservers() {
132
        delegateObservable.deleteObservers();
133
    }
134

    
135
        public void endComplex() {
136
        if (collection.isEmpty()) {
137
            complex = false;
138
            return;
139
        }
140
                complex = false;
141
        undos.add(collection);
142
        redos.clear();
143
        delegateObservable.notifyObservers(new DefaultCommandNotification(
144
                collection, CommandNotification.ADD));
145

    
146
        }
147

    
148
        public void startComplex(String description) {
149
        collection = new CompoundCommand(description);
150
        complex = true;
151
    }
152

    
153
}