Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / table / TableContainer.java @ 10741

History | View | Annotate | Download (10.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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
package org.gvsig.gui.beans.table;
20

    
21
import java.awt.Color;
22
import java.awt.FlowLayout;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25

    
26
import javax.swing.JPanel;
27
import javax.swing.table.DefaultTableModel;
28

    
29
import org.gvsig.gui.beans.BaseComponent;
30
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
31
import org.gvsig.gui.beans.table.listeners.TableListener;
32

    
33
/**
34
 * Contenedor para los componentes de la tabla. Incluye la tabla  y el panel
35
 * de control.
36
 * 
37
 * @author Nacho Brodin (brodin_ign@gva.es)
38
 *
39
 */
40
public class TableContainer extends BaseComponent{
41

    
42
        private static final int                INTERNAL_MARGIN = 10;
43
        private  int                                        HEIGHT_CONTROL = 24;
44
                        
45
        private Table                                         pTable = null;
46
        private JPanel                                         pControl = null;
47
        private int                                         width = 400, height = 200;
48
        private TableControlerPanel         pTableControl = null;
49
        private JPanel                                         pGeneral = null;
50
    private String[]                                columnNames = null;
51
    private int[]                                        columnWidths = null;
52
    private TableListener                        tableListener = null;
53
    //Variable que indica si la tabla ha sido inicializada
54
    private boolean                                        initTable = false;
55
    private String                                        tableModelClass = "ListModel";
56
    private int                                                lastAssignW, lastAssignH;
57
        
58
    /**
59
     * 
60
     * @param width Ancho de la tabla en pixeles
61
     * @param height Alto de la tabla en pixeles
62
     * @param columnNames Vector de nombres de columna
63
     * @param columnsWidth        Vector de anchos para cada columna. Ha de tener el mismo n?mero de elementos que columnNames. 
64
     * Si vale null las columnas se pondr?n equidistantes. 
65
     */
66
        public TableContainer(int width, int height, String[] columnNames, int[] columnWidths) {
67
                this.width = width;
68
                this.height = height;
69
                this.columnNames = columnNames;
70
                this.columnWidths = columnWidths;
71
        }
72
                
73
        /**
74
         * This method initializes this
75
         * 
76
         */
77
        public void initialize() {
78
        initTable = true;
79
                this.tableListener = new TableListener(this);
80
        this.add(getPGeneral(), new GridBagLayout());
81
        }
82
        
83
        /**
84
         * Asigna el tama?o del componente
85
         * @param w Nuevo ancho del componente padre
86
         * @param h Nuevo alto del componente padre
87
         */
88
        public void setComponentSize(int w, int h){
89
                lastAssignW = w;
90
                lastAssignH = h;
91
                //getPControl().setSize(w - INTERNAL_MARGIN, HEIGHT_CONTROL);
92
                getPControl().setPreferredSize(new java.awt.Dimension(w - INTERNAL_MARGIN, HEIGHT_CONTROL));
93
                getPGeneral().setPreferredSize(new java.awt.Dimension(w, h));
94
                getPTable().setComponentSize(w - INTERNAL_MARGIN, h - INTERNAL_MARGIN - HEIGHT_CONTROL - (INTERNAL_MARGIN / 2));
95
        }
96
        
97
        /**
98
         * This method initializes jPanel        
99
         *         
100
         * @return javax.swing.JPanel        
101
         */
102
        public Table getPTable() {
103
                if (pTable == null) {
104
                        pTable = new Table(        width - INTERNAL_MARGIN , 
105
                                                                height - INTERNAL_MARGIN - HEIGHT_CONTROL - (INTERNAL_MARGIN / 2),
106
                                                                columnNames,
107
                                                                columnWidths,
108
                                                                tableListener,
109
                                                                tableModelClass);
110
                        pTable.setTableContainer(this);
111
                        FlowLayout flowLayout = new FlowLayout();
112
                        flowLayout.setHgap(0);
113
                        flowLayout.setVgap(0);
114
                        pTable.setLayout(flowLayout);
115
                }
116
                return pTable;
117
        }
118

    
119
        /**
120
         * This method initializes jPanel1
121
         *         
122
         * @return javax.swing.JPanel        
123
         */
124
        private JPanel getPControl() {
125
                if (pControl == null) {
126
                        FlowLayout flowLayout = new FlowLayout();
127
                        flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
128
                        flowLayout.setVgap(0);
129
                        flowLayout.setHgap(0);
130
                        pControl = new JPanel();
131
                        pControl.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
132
                        pControl.setLayout(flowLayout);
133
                        pControl.setSize(width - INTERNAL_MARGIN, HEIGHT_CONTROL);
134
                        pControl.setPreferredSize(new java.awt.Dimension(width - INTERNAL_MARGIN, HEIGHT_CONTROL));
135
                        pControl.add(getPTableControl(), null);
136
                }
137
                return pControl;
138
        }
139

    
140
        /**
141
         * This method initializes jPanel        
142
         *         
143
         * @return javax.swing.JPanel        
144
         */
145
        public TableControlerPanel getPTableControl() {
146
                if (pTableControl == null) {
147
                        pTableControl = new TableControlerPanel(tableListener);
148
                }
149
                return pTableControl;
150
        }
151

    
152
        /**
153
         * This method initializes jPanel        
154
         *         
155
         * @return javax.swing.JPanel        
156
         */
157
        private JPanel getPGeneral() {
158
                if (pGeneral == null) {
159
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
160
                        gridBagConstraints.gridx = 0;
161
                        gridBagConstraints.gridy = 0;
162
                gridBagConstraints.insets = new java.awt.Insets(0,0,(INTERNAL_MARGIN / 2),0);
163
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
164
                        gridBagConstraints1.gridx = 0;
165
                        gridBagConstraints1.gridy = 1;
166
                gridBagConstraints1.insets = new java.awt.Insets(0,0,0,0);
167
                        pGeneral = new JPanel();
168
                        pGeneral.setPreferredSize(new java.awt.Dimension(width, height));
169
                        pGeneral.setLayout(new GridBagLayout());
170
                        pGeneral.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
171
                        pGeneral.add(getPTable(), gridBagConstraints);
172
                        pGeneral.add(getPControl(), gridBagConstraints1);
173
                }
174
                return pGeneral;
175
        }
176
        
177
        //********************M?todos de Tabla*****************************//
178
        
179
        /**
180
         * A?ade una fila a la tabla.
181
         * @param list Lista de cadenas
182
         */
183
        public void addRow(Object[] list)throws NotInitializeException{
184
                if(!initTable)
185
                        throw new NotInitializeException();
186
                
187
                TableListener.comboEventEnable = false;
188
                ((Table)pTable).addRow(list);
189
                pTableControl.addPointToTable(((Table)pTable).getTable().getRowCount());
190
                setSelectedIndex(getRowCount() - 1);
191
                TableListener.comboEventEnable = true;
192
        }
193
        
194
        /**
195
         * Elimina una fila de la tabla.
196
         * @param i Fila a eliminar
197
         */
198
        public void delRow(int i)throws NotInitializeException{
199
                if(!initTable)
200
                        throw new NotInitializeException();
201
                
202
                TableListener.comboEventEnable = false;
203
                try{
204
                        setSelectedIndex(getSelectedIndex() - 1);
205
                }catch(ArrayIndexOutOfBoundsException ex){
206
                        try{
207
                                setSelectedIndex(getSelectedIndex() + 1);
208
                        }catch(ArrayIndexOutOfBoundsException exc){}
209
                }
210
                ((Table)pTable).delRow(i);
211
                pTableControl.setNItems(getRowCount());
212
                TableListener.comboEventEnable = true;
213
        }
214

    
215
        /**
216
         * Elimina todas las filas de la tabla.
217
         */
218
        public void removeAllRows()throws NotInitializeException{
219
                if(!initTable)
220
                        throw new NotInitializeException();
221
                
222
                TableListener.comboEventEnable = false;
223
                ((Table)pTable).removeAllRows();
224
                pTableControl.setNItems(0);
225
                TableListener.comboEventEnable = true;
226
        }
227
        
228
        /**
229
         * Obtiene el n?mero de filas en la tabla
230
         * @return N?mero de filas de la tabla
231
         */
232
        public int getRowCount()throws NotInitializeException{
233
                if(!initTable)
234
                        throw new NotInitializeException();
235
                
236
                return ((Table)pTable).getTable().getRowCount();
237
        }
238
        
239
        /**
240
         * Selecciona un punto de la lista
241
         * @param i punto a seleccionar
242
         */
243
        public void setSelectedIndex(int i)throws NotInitializeException{
244
                if(!initTable)
245
                        throw new NotInitializeException();
246
                
247
                TableListener.comboEventEnable = false;
248
                try{
249
                        pTableControl.setSelectedIndex(i);
250
                        ((Table)pTable).getTable().setRowSelectionInterval(i, i);
251
                }catch(IllegalArgumentException ex){
252
                        
253
                }
254
                TableListener.comboEventEnable = true;
255
        }
256
        
257
        /**
258
         * Obtiene el punto seleccionado de la lista
259
         * @return Posici?n del punto seleccionado de la tabla
260
         */
261
        public int getSelectedIndex()throws NotInitializeException{
262
                if(!initTable)
263
                        throw new NotInitializeException();
264
                
265
                return ((Table)pTable).getTable().getSelectedRow();
266
        }
267

    
268
        /**
269
         * Asigna un valor a una posici?n de la tabla
270
         * @param value Valor
271
         * @param row Fila
272
         * @param col Columna
273
         */
274
        public void setValueAt(Object value, int row, int col)throws NotInitializeException{
275
                if(!initTable)
276
                        throw new NotInitializeException();
277
                
278
                ((Table)pTable).getTable().setValueAt(value, row, col);
279
        }
280
        
281
        /**
282
         * Dice si una tabla es editable o no. 
283
         * Este flag hay que asignarlo antes de la inicializaci?n de tabla.
284
         * @param editable
285
         */
286
        public void setEditable(boolean editable)throws NotInitializeException{
287
                if(!initTable)
288
                        throw new NotInitializeException();
289
                
290
                if(!editable)
291
                        ((Table)pTable).getTable().setBackground(getPGeneral().getBackground());
292
                else
293
                        ((Table)pTable).getTable().setBackground(Color.white);
294
                
295
                ((Table)pTable).getTable().setEnabled(editable);
296
        }
297
        
298
        /**
299
         * Asigna el modelo de la tabla
300
         * @param model cadena con el nombre del modelo
301
         */
302
        public void setModel(String model){
303
                tableModelClass = model;
304
        }
305
        
306
        /**
307
         * Obtiene el model de la tabla
308
         * @return
309
         */
310
        public DefaultTableModel getModel(){
311
                return (DefaultTableModel)((Table)pTable).getTable().getModel();
312
        }
313

    
314
        /**
315
         * Asigna al panel de control de tabla la propiedad de visible/invisible a true o false
316
         * @param visible
317
         */
318
        public void setControlVisible(boolean visible){
319
                getPTableControl().setVisible(visible);
320
                if(!visible)
321
                        HEIGHT_CONTROL = 0;
322
                else
323
                        HEIGHT_CONTROL = 24;
324
        }
325
        
326
        /**
327
         * Obtiene el control de tabla
328
         * @return TableControlerPanel
329
         */
330
        public TableControlerPanel getControl(){
331
                return this.pTableControl;
332
        }
333
        
334
        /**
335
         * Desactiva o activa el evento de nueva linea. Si se desactiva tendr? que ser
336
         * gestionado por el cliente
337
         * @param enabled true para activar y false para desactivar
338
         */
339
        public void setEnableControlsListener(boolean enabled){
340
                tableListener.enableNewLineListener = enabled;
341
        }
342
        
343
        /**
344
         * Activar o desactivar los componentes del panel
345
         */
346
        public void setEnabled(boolean enabled){
347
                getPTable().getTable().setEnabled(enabled);
348
                if(!enabled)
349
                        getPTableControl().disableAllControls();
350
                else
351
                        getPTableControl().restoreControlsValue();
352
        }
353
 }