Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / controls / dnd / JDnDTableModel.java @ 40561

History | View | Annotate | Download (4.47 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
/* CVS MESSAGES:
25
*
26
* $Id: JDnDTableModel.java 13655 2007-09-12 16:28:55Z bsanchez $
27
* $Log$
28
* Revision 1.2  2007-09-12 16:28:23  bsanchez
29
* *** empty log message ***
30
*
31
* Revision 1.1  2007/08/20 08:34:46  evercher
32
* He fusionado LibUI con LibUIComponents
33
*
34
* Revision 1.2  2006/09/27 13:34:57  jaume
35
* *** empty log message ***
36
*
37
* Revision 1.1  2006/09/21 16:35:12  jaume
38
* *** empty log message ***
39
*
40
*
41
*/
42
package org.gvsig.gui.beans.controls.dnd;
43

    
44
import java.util.ArrayList;
45

    
46
import javax.swing.table.AbstractTableModel;
47

    
48
public class JDnDTableModel extends AbstractTableModel{
49
  private static final long serialVersionUID = 7508610018581206163L;
50
                public static final short ONLY_ALLOW_MOVING_COLUMNS = JDnDTable.ONLY_ALLOW_MOVING_COLUMNS;
51
    public static final short ONLY_ALLOW_MOVING_ROWS = JDnDTable.ONLY_ALLOW_MOVING_ROWS;
52
        private static final short FREE_CELL_MOVING = JDnDTable.FREE_CELL_MOVING;
53

    
54
    private short mode = ONLY_ALLOW_MOVING_COLUMNS;
55

    
56
    private String[] colNames;
57
    private Object[][] values;
58

    
59
    public JDnDTableModel(Object[][] values, String[] colNames) {
60
        super();
61
        this.colNames = colNames;
62
        this.values = values;
63
    }
64

    
65
    public int getColumnCount() {
66
        // TODO Auto-generated method stub
67
        return colNames.length;
68
    }
69

    
70
    public int getRowCount() {
71
        // TODO Auto-generated method stub
72
        return values[0].length;
73
    }
74

    
75
    public Object getValueAt(int rowIndex, int columnIndex) {
76
        // TODO Auto-generated method stub
77
        return values[rowIndex][columnIndex];
78
    }
79

    
80
    public void itemsMoved(CellCoordinates newCellCoord, CellCoordinates[] coordinates) {
81
        // TODO Auto-generated method stub
82
        System.out.println("Model itemsMoved");
83

    
84

    
85
        switch (mode) {
86
        case ONLY_ALLOW_MOVING_COLUMNS:
87

    
88
                // last selected cell defines which column
89
                int srcColumnIndex = coordinates[coordinates.length-1].i;
90
                int dstColumnIndex = newCellCoord.i;
91
                System.out.println("movent columna "+ srcColumnIndex + " a columna "+dstColumnIndex);
92
            for (int i = 0; i < getRowCount(); i++) {
93
                    CellCoordinates srcCoord = new CellCoordinates(i, srcColumnIndex);
94
                    CellCoordinates dstCoord = new CellCoordinates(i, dstColumnIndex);
95
                    swapCells(srcCoord, dstCoord);
96
            }
97
            swapColumnNames(dstColumnIndex, srcColumnIndex);
98
            break;
99
        case ONLY_ALLOW_MOVING_ROWS:
100
            // last selected cell defines which row
101
            int rowIndex = coordinates[coordinates.length-1].i;
102
            coordinates = new CellCoordinates[getColumnCount()];
103
            for (int i = 0; i < coordinates.length; i++) {
104
                coordinates[i].i = rowIndex;
105
                coordinates[i].j = i;
106
            }
107
            newCellCoord.j = 0;
108
            break;
109
        case FREE_CELL_MOVING:
110
                // TODO implement it
111
                break;
112
        }
113
        this.fireTableDataChanged();
114
    }
115

    
116
    private void swapColumnNames(int ind1, int ind2) {
117
        String aux = colNames[ind2];
118
        colNames[ind2] = colNames[ind1];
119
        colNames[ind1] = aux;
120
    }
121

    
122
    private void swapCells(CellCoordinates cell1, CellCoordinates cell2) {
123
            Object aux = values[cell1.i][cell1.j];
124
            values[cell1.i][cell1.j] = values[cell2.i][cell2.j];
125
            values[cell2.i][cell2.j] = aux;
126
    }
127

    
128
    public void insertItems(CellCoordinates cellCoord, ArrayList items) {
129
        // TODO Auto-generated method stub
130
        System.out.println("Model inserItems");
131
    }
132

    
133
        public void setSelectionMode(short mode) {
134
                this.mode = mode;
135
        }
136

    
137
}