Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / command / CommandTableModel.java @ 37597

History | View | Annotate | Download (1.6 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
    private UndoRedoStack cr;
19
    
20
    public CommandTableModel(UndoRedoStack cr) {
21
        this.cr = cr;
22
        }
23

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