Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / command / CommandTableModel.java @ 39212

History | View | Annotate | Download (1.99 KB)

1
package org.gvsig.app.gui.command;
2

    
3
import javax.swing.table.AbstractTableModel;
4

    
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7

    
8
import org.gvsig.tools.undo.RedoException;
9
import org.gvsig.tools.undo.UndoException;
10
import org.gvsig.tools.undo.UndoRedoStack;
11
import org.gvsig.tools.undo.command.Command;
12

    
13

    
14
@SuppressWarnings("serial")
15
public class CommandTableModel extends AbstractTableModel{
16
    private static final Logger LOG = 
17
        LoggerFactory.getLogger(CommandTableModel.class);
18
    
19
    private UndoRedoStack undoRedoStack;
20
        
21
    public CommandTableModel(UndoRedoStack undoRedoStack) {
22
        this.undoRedoStack = undoRedoStack;
23
        }
24

    
25
        public int getPos() {
26
                if ((undoRedoStack == null) || undoRedoStack.getUndoInfos() == null){
27
                    return 0;
28
                }
29
            return undoRedoStack.getUndoInfos().size() - 1;
30
        }
31
        
32
        public int getColumnCount() {
33
                return 1;
34
        }
35
        
36
        public int getRowCount() {
37
                if ((undoRedoStack == null) || undoRedoStack.getUndoInfos() == null || undoRedoStack.getRedoInfos() == null){
38
                    return 0;
39
                }
40
                return undoRedoStack.getRedoInfos().size() + undoRedoStack.getUndoInfos().size();
41
        }
42
        
43
        @SuppressWarnings("unchecked")
44
    public Object getValueAt(int i, int j) {
45
                Command[] undos=(Command[])undoRedoStack.getUndoInfos().toArray(new Command[0]);
46
                Command[] redos=(Command[])undoRedoStack.getRedoInfos().toArray(new Command[0]);
47
                if (i<undos.length){
48
                        //System.out.println("undo i=" + i + " index=" + (undos.length-1-i));
49
                        return undos[undos.length-1-i];
50
                }else{
51
                        //System.out.println("redo i=" + i + " index=" + (i-undos.length));
52
                        return redos[i-undos.length];
53
                }
54
        }
55
        
56
        public void setPos(int newpos) {
57
                try {
58
                    int currentPos = getPos();
59
                        if (newpos > currentPos) {
60
                                undoRedoStack.redo(newpos - currentPos);
61
                        }else if (newpos < getPos()) {
62
                                undoRedoStack.undo(currentPos - newpos);
63
                        }
64
                        
65
                } catch (RedoException e) {
66
                        LOG.error("Error executing the command", e);
67
                } catch (UndoException e) {
68
                    LOG.error("Error executing the command", e);
69
                }
70
        }
71
}