Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / project / TableModel.java @ 6478

History | View | Annotate | Download (5.53 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.gui.project;
42

    
43

    
44
import java.util.BitSet;
45

    
46
import javax.swing.table.AbstractTableModel;
47

    
48
import com.hardcode.driverManager.DriverLoadException;
49
import com.hardcode.gdbms.engine.data.driver.DriverException;
50
import com.iver.andami.PluginServices;
51
import com.iver.andami.messages.NotificationManager;
52
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
53
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
54
import com.iver.cit.gvsig.project.ProjectTable;
55

    
56

    
57
/**
58
 * Modelo para la JTable de MapProperties
59
 *
60
 * @author Fernando Gonz?lez Cort?s
61
 */
62
public class TableModel extends AbstractTableModel {
63
    private BitSet visibles = new BitSet();
64
    private String[] alias;
65
    private ProjectTable table;
66

    
67
    /**
68
     * Creates a new MyTableModel object.
69
     *
70
     * @param t Tabla del proyecto que se quiere mostrar
71
     * @throws DriverException
72
     */
73
    public TableModel(ProjectTable t) throws DriverException {
74
        IEditableSource ds = t.getOriginal();
75
        if (ds == null) {
76
            ds = t.getModelo();
77
        }
78
        alias = t.getAliases();
79
        int[] mapping = t.getMapping();
80
        visibles = new BitSet();
81
        for (int i = 0; i < mapping.length; i++) {
82
            visibles.set(mapping[i]);
83
        }
84

    
85
        table = t;
86
    }
87

    
88
    /**
89
     * @see javax.swing.table.TableModel#getColumnCount()
90
     */
91
    public int getColumnCount() {
92
        return 3;
93
    }
94

    
95
    /* (non-Javadoc)
96
     * @see javax.swing.table.TableModel#getRowCount()
97
     */
98
    public int getRowCount() {
99
       try {
100
                return table.getModelo().getRecordset().getFieldCount();
101
            } catch (DriverException e) {
102
                NotificationManager.addError("No se pudo leer la informaci?n de la tabla", e);
103
                return 0;
104
            } catch (DriverLoadException e) {
105
                    NotificationManager.addError("No se pudo leer la informaci?n de la tabla", e);
106
                        return 0;
107
                }
108
    }
109

    
110
    /* (non-Javadoc)
111
     * @see javax.swing.table.TableModel#getValueAt(int, int)
112
     */
113
    public Object getValueAt(int rowIndex, int columnIndex) {
114
        if (columnIndex == 0) {
115
            return new Boolean(visibles.get(rowIndex));
116
        } else if (columnIndex == 1) {
117
                try {
118
                return table.getModelo().getRecordset().getFieldName(rowIndex);
119
            } catch (DriverException e) {
120
                NotificationManager.addError("Error accediendo al nombre del campo", e);
121
                return "error!";
122
            } catch (DriverLoadException e) {
123
                     NotificationManager.addError("Error accediendo al nombre del campo", e);
124
                 return "error!";
125
                        }
126
        } else {
127
                // rowIndex++;
128
                return alias[rowIndex];
129
        }
130
    }
131

    
132
    /* (non-Javadoc)
133
     * @see javax.swing.table.TableModel#getColumnClass(int)
134
     */
135
    public Class getColumnClass(int columnIndex) {
136
        if (columnIndex == 0) {
137
            return Boolean.class;
138
        }
139

    
140
        return String.class;
141
    }
142

    
143
    /* (non-Javadoc)
144
     * @see javax.swing.table.TableModel#isCellEditable(int, int)
145
     */
146
    public boolean isCellEditable(int rowIndex, int columnIndex) {
147
        return (columnIndex != 1);
148
    }
149

    
150
    /* (non-Javadoc)
151
     * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
152
     */
153
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
154
        if (columnIndex == 0) {
155
            visibles.set(rowIndex, ((Boolean) aValue).booleanValue());
156
        } else {
157
            alias[rowIndex] = aValue.toString();
158
        }
159
    }
160

    
161
    /* (non-Javadoc)
162
     * @see javax.swing.table.TableModel#getColumnName(int)
163
     */
164
    public String getColumnName(int column) {
165
        String ret = null;
166

    
167
        switch (column) {
168
        case 0:
169
            ret = PluginServices.getText(this, "visible");
170

    
171
            break;
172

    
173
        case 1:
174
            ret = PluginServices.getText(this, "campo");
175

    
176
            break;
177

    
178
        case 2:
179
            ret = PluginServices.getText(this, "alias");
180

    
181
            break;
182
        }
183

    
184
        return ret;
185
    }
186

    
187
    public String[] getAliases() {
188
        // return table.getAliases();
189
            return alias;
190
    }
191

    
192
    public int[] getFieldMapping() {
193
        int[] mapping = new int[visibles.cardinality()];
194
        int index = 0;
195
        for(int i=visibles.nextSetBit(0); i>=0; i=visibles.nextSetBit(i+1)) {
196
            mapping[index] = i;
197
            index++;
198
        }
199

    
200
        return mapping;
201
    }
202
}