Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / commands / MemoryCommandRecord.java @ 19509

History | View | Annotate | Download (5.84 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.edition.commands;
42

    
43
import java.io.IOException;
44
import java.util.ArrayList;
45
import java.util.Stack;
46

    
47
import com.iver.cit.gvsig.exceptions.commands.EditionCommandException;
48
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
49

    
50

    
51
/**
52
 * Clase en memoria para registrar y gestionar los comandos que vamos
53
 * realizando. La forma en que ha sido implementada esta clase, en vez de una
54
 * ?nica lista para albergar los comandos de deshacer(undos) y los de
55
 * rehacer(redos), se ha optado por dos pilas una para deshacer(undos) y otra
56
 * para rehacer(redos), de esta forma :  Cuando se a?ade un nuevo comando, se
57
 * inserta este a la pila de deshacer(undos) y se borra de la de
58
 * rehacer(redos). Si se realiza un deshacer se desapila este comando de la
59
 * pila deshacer(undos) y se apila en la de rehacer(redos). Y de la misma
60
 * forma cuando se realiza un rehacer se desapila este comando de la pila de
61
 * rehacer(redos) y pasa a la de deshacer(undos).
62
 *
63
 * @author Vicente Caballero Navarro
64
 */
65
public class MemoryCommandRecord implements CommandRecord {
66
        private Stack undos = new Stack();
67
        private Stack redos = new Stack();
68
        private ArrayList commandsListener=new ArrayList();
69
        private boolean refresh=true;
70
        private int undosCount=0;
71

    
72
        /**
73
         * A?ade el comando a la pila de deshacer y borra todo el contenido de la
74
         * pila rehacer.
75
         *
76
         * @param command Comando a a?adir.
77
         *
78
         * @throws IOException
79
         * @throws DriverIOException
80
         *
81
         * @see com.iver.cit.gvsig.fmap.edition.CommandRecord#addCommand(com.iver.cit.gvsig.fmap.edition.Command)
82
         */
83
        public void pushCommand(Command command){
84
                undos.add(command);
85
                redos.clear();
86
                refresh=true;
87
                fireCommandRefresh();
88
        }
89

    
90
        /**
91
         * Realiza la operaci?n del ?ltimo comando de deshacer y lo apila en los de
92
         * rehacer.
93
         * @throws EditionCommandException
94
         *
95
         * @throws DriverIOException
96
         * @throws IOException
97
         *
98
         * @see com.iver.cit.gvsig.fmap.edition.CommandRecord#delCommand(com.iver.cit.gvsig.fmap.edition.Command)
99
         */
100
        public void undoCommand() throws EditionCommandException {
101
                Command command = (Command) undos.pop();
102
                command.undo();
103
                redos.add(command);
104
                fireCommandsRepaint(new CommandEvent(command));
105
        }
106

    
107
        /**
108
         * Realiza la operaci?n de rehacer el ?ltimo comando apilado y lo a?ade a
109
         * la pila deshacer.
110
         *
111
         * @throws DriverIOException
112
         * @throws IOException
113
         *
114
         * @see com.iver.cit.gvsig.fmap.edition.CommandRecord#redoCommand()
115
         */
116
        public void redoCommand() throws EditionCommandException {
117
                Command command = (Command) redos.pop();
118
                command.redo();
119
                undos.add(command);
120
                fireCommandsRepaint(new CommandEvent(command));
121
        }
122

    
123
        /**
124
         * @see com.iver.cit.gvsig.fmap.edition.CommandRecord#moreUndoCommands()
125
         */
126
        public boolean moreUndoCommands() {
127
                return (!undos.isEmpty());
128
        }
129

    
130
        /**
131
         * @see com.iver.cit.gvsig.fmap.edition.CommandRecord#moreRedoCommands()
132
         */
133
        public boolean moreRedoCommands() {
134
                return (!redos.isEmpty());
135
        }
136
        public Command[] getUndoCommands(){
137
                Stack clonedUndos=(Stack)undos.clone();
138

    
139
                ArrayList commands=new ArrayList();
140
                while (!clonedUndos.isEmpty()){
141
                        commands.add(clonedUndos.pop());
142
                }
143

    
144
                return (Command[])commands.toArray(new Command[0]);
145
        }
146
        public Command[] getRedoCommands(){
147
                Stack clonedRedos=(Stack)redos.clone();
148

    
149
                ArrayList commands=new ArrayList();
150
                while (!clonedRedos.isEmpty()){
151
                        commands.add(clonedRedos.pop());
152
                }
153
                return (Command[])commands.toArray(new Command[0]);
154
        }
155
        public int getPos(){
156
                if (refresh){
157
                        undosCount=undos.size()-1;
158
                }
159
                return undosCount;
160
        }
161
        public void setPos(int newpos) throws EditionCommandException{
162
                int pos=getPos();
163
                if (newpos>pos){
164
                        for (int i=pos;i<newpos;i++){
165
                                redoCommand();
166
                                System.out.println("redos = "+i);
167
                        }
168
                }else if (pos>newpos){
169
                        for (int i=pos-1;i>=newpos;i--){
170
                                undoCommand();
171
                                System.out.println("undos = "+i);
172
                        }
173
                }
174
        }
175

    
176
        public void addCommandListener(CommandListener cl) {
177
                if (!commandsListener.contains(cl))
178
                        commandsListener.add(cl);
179
        }
180

    
181
        public void fireCommandsRepaint(CommandEvent e) {
182
                for (int i=0;i<commandsListener.size();i++){
183
                        ((CommandListener)commandsListener.get(i)).commandRepaint();
184
                }
185
        }
186
        public void fireCommandRefresh(){
187
                for (int i=0;i<commandsListener.size();i++){
188
                        ((CommandListener)commandsListener.get(i)).commandRefresh();
189
                }
190
        }
191

    
192
        public int getCommandCount() {
193
                return undos.size()+redos.size();
194
        }
195

    
196
        public void removeCommandListener(CommandListener e) {
197
                commandsListener.remove(e);
198
        }
199

    
200
        public void clearAll() {
201
                redos.clear();
202
                undos.clear();
203

    
204
        }
205
}