Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / gui / command / CommandTableModel.java @ 40558

History | View | Annotate | Download (2.92 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.gui.command;
25

    
26
import javax.swing.table.AbstractTableModel;
27

    
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30

    
31
import org.gvsig.tools.undo.RedoException;
32
import org.gvsig.tools.undo.UndoException;
33
import org.gvsig.tools.undo.UndoRedoStack;
34
import org.gvsig.tools.undo.command.Command;
35

    
36

    
37
@SuppressWarnings("serial")
38
public class CommandTableModel extends AbstractTableModel{
39
    private static final Logger LOG = 
40
        LoggerFactory.getLogger(CommandTableModel.class);
41
    
42
    private UndoRedoStack undoRedoStack;
43
        
44
    public CommandTableModel(UndoRedoStack undoRedoStack) {
45
        this.undoRedoStack = undoRedoStack;
46
        }
47

    
48
        public int getPos() {
49
                if ((undoRedoStack == null) || undoRedoStack.getUndoInfos() == null){
50
                    return 0;
51
                }
52
            return undoRedoStack.getUndoInfos().size() - 1;
53
        }
54
        
55
        public int getColumnCount() {
56
                return 1;
57
        }
58
        
59
        public int getRowCount() {
60
                if ((undoRedoStack == null) || undoRedoStack.getUndoInfos() == null || undoRedoStack.getRedoInfos() == null){
61
                    return 0;
62
                }
63
                return undoRedoStack.getRedoInfos().size() + undoRedoStack.getUndoInfos().size();
64
        }
65
        
66
        @SuppressWarnings("unchecked")
67
    public Object getValueAt(int i, int j) {
68
                Command[] undos=(Command[])undoRedoStack.getUndoInfos().toArray(new Command[0]);
69
                Command[] redos=(Command[])undoRedoStack.getRedoInfos().toArray(new Command[0]);
70
                if (i<undos.length){
71
                        //System.out.println("undo i=" + i + " index=" + (undos.length-1-i));
72
                        return undos[undos.length-1-i];
73
                }else{
74
                        //System.out.println("redo i=" + i + " index=" + (i-undos.length));
75
                        return redos[i-undos.length];
76
                }
77
        }
78
        
79
        public void setPos(int newpos) {
80
                try {
81
                    int currentPos = getPos();
82
                        if (newpos > currentPos) {
83
                                undoRedoStack.redo(newpos - currentPos);
84
                        }else if (newpos < getPos()) {
85
                                undoRedoStack.undo(currentPos - newpos);
86
                        }
87
                        
88
                } catch (RedoException e) {
89
                        LOG.error("Error executing the command", e);
90
                } catch (UndoException e) {
91
                    LOG.error("Error executing the command", e);
92
                }
93
        }
94
}