Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Table.java @ 2165

History | View | Annotate | Download (10.5 KB)

1 1103 fjp
/* 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 312 fernando
package com.iver.cit.gvsig.gui;
42
43 1047 fernando
import java.beans.PropertyChangeEvent;
44
import java.beans.PropertyChangeListener;
45 312 fernando
import java.text.DateFormat;
46
import java.text.NumberFormat;
47 457 fernando
import java.util.BitSet;
48
49
import javax.swing.DefaultListSelectionModel;
50 312 fernando
import javax.swing.JLabel;
51
import javax.swing.JPanel;
52 457 fernando
import javax.swing.event.ListSelectionEvent;
53
import javax.swing.event.ListSelectionListener;
54 312 fernando
import javax.swing.table.AbstractTableModel;
55
56 464 fernando
import org.apache.log4j.Logger;
57 312 fernando
58 1830 fernando
import com.hardcode.gdbms.engine.data.driver.DriverException;
59 464 fernando
import com.hardcode.gdbms.engine.values.ValueFactory;
60 596 fernando
import com.iver.andami.PluginServices;
61 732 fernando
import com.iver.andami.messages.NotificationManager;
62 596 fernando
import com.iver.andami.ui.mdiManager.SingletonView;
63
import com.iver.andami.ui.mdiManager.ViewInfo;
64 1350 fernando
import com.iver.andami.ui.mdiManager.ViewListener;
65 464 fernando
import com.iver.cit.gvsig.fmap.FMap;
66 694 fernando
import com.iver.cit.gvsig.fmap.layers.FLayer;
67 464 fernando
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
68
import com.iver.cit.gvsig.fmap.layers.SelectionEvent;
69
import com.iver.cit.gvsig.fmap.layers.SelectionListener;
70 1221 fernando
import com.iver.cit.gvsig.project.ProjectTable;
71 464 fernando
72
73 428 fernando
/**
74
 * DOCUMENT ME!
75
 *
76
 * @author Fernando Gonz?lez Cort?s
77
 */
78 1830 fernando
public class Table extends JPanel implements SingletonView {
79 428 fernando
    private static Logger logger = Logger.getLogger(Table.class.getName());
80
    private javax.swing.JScrollPane jScrollPane = null;
81
    private javax.swing.JTable table = null;
82 457 fernando
    private ProjectTable model = null;
83 428 fernando
    private DateFormat df = DateFormat.getDateInstance();
84
    private NumberFormat nf = NumberFormat.getInstance();
85
    private JLabel jLabelStatus = null;
86
    private ViewInfo m_viewInfo = null;
87 457 fernando
    private FMap fmap;
88
89
    private boolean updating = false;
90
91
    private TableSelectionListener selectionListener = new TableSelectionListener();
92
93 428 fernando
    /**
94
     * This is the default constructor
95
     */
96
    public Table() {
97
        super();
98
        initialize();
99
    }
100 312 fernando
101 428 fernando
    /**
102
     * DOCUMENT ME!
103
     *
104
     * @return DOCUMENT ME!
105
     */
106 457 fernando
    public ProjectTable getModel() {
107
        return model;
108 428 fernando
    }
109 312 fernando
110 1830 fernando
    private void refreshControls(){
111
            try{
112
                        PluginServices.getMDIManager().getViewInfo(Table.this).setTitle(PluginServices.getText(this, "Tabla")+": " + model.getName());
113
114
115
116
                if (model.getAssociatedTable() != null){
117
                        this.fmap = ((FLayer)model.getAssociatedTable()).getFMap();
118
                }else{
119
                        this.fmap = null;
120
                }
121
122
                SelectableDataSource dataSource = model.getModelo();
123
                logger.debug("dataSource.start()");
124
                dataSource.start();
125
126
                AbstractTableModel dataModel = new DataSourceDataModel(dataSource);
127
128
                        TableSorter sorter = new TableSorter(dataModel);
129
                        getTable().setModel(sorter);
130
                        sorter.setTableHeader(getTable().getTableHeader());
131
132
                model.getModelo().addSelectionListener(selectionListener);
133
134
                updateSelection();
135
136
                jLabelStatus.setText(" " + dataSource.getRowCount() + " " +
137
                    PluginServices.getText(this, "registros") + ".");
138
            }catch (DriverException e){
139
                    NotificationManager.addError("No se pudo leer la informaci?n", e);
140
            }
141
142
    }
143
144 428 fernando
    /**
145
     * DOCUMENT ME!
146
     *
147 457 fernando
     * @param table DOCUMENT ME!
148 428 fernando
     */
149 694 fernando
    public void setModel(ProjectTable table) {
150 1830 fernando
        SelectableDataSource dataSource;
151
        model = table;
152 457 fernando
153 1830 fernando
        //Gesti?n del nombre de la ventana
154
        model.addPropertyChangeListener(new PropertyChangeListener() {
155
                        public void propertyChange(PropertyChangeEvent evt) {
156
                                if (evt.getPropertyName().equals("name")){
157
                                        PluginServices.getMDIManager().getViewInfo(Table.this).setTitle(PluginServices.getText(this, "Tabla")+": " + (String)evt.getNewValue());
158
                                }else if (evt.getPropertyName().equals("model")){
159
                                        refreshControls();
160
                                }
161
                        }
162
                });
163
164
        refreshControls();
165 428 fernando
    }
166
167
    /**
168 457 fernando
     *
169
     */
170
    private void updateSelection() {
171
            updating = true;
172
173
        DefaultListSelectionModel sm = (DefaultListSelectionModel) getTable()
174
                                                                       .getSelectionModel();
175
        sm.clearSelection();
176 464 fernando
        BitSet bs = model.getModelo().getSelection();
177 457 fernando
        sm.setValueIsAdjusting(true);
178
179
        for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
180
            sm.addSelectionInterval(i, i);
181
        }
182
183
        sm.setValueIsAdjusting(false);
184
185
        updating = false;
186
    }
187
188
    /**
189 428 fernando
     * This method initializes this
190
     */
191
    private void initialize() {
192
        jLabelStatus = new JLabel();
193
        setLayout(new java.awt.BorderLayout());
194
        jLabelStatus.setText("");
195
        jLabelStatus.setName("");
196
        jLabelStatus.setPreferredSize(new java.awt.Dimension(100, 18));
197
        add(getJScrollPane(), java.awt.BorderLayout.CENTER);
198
        this.add(jLabelStatus, java.awt.BorderLayout.SOUTH);
199
    }
200
201
    /**
202
     * This method initializes table
203
     *
204
     * @return javax.swing.JTable
205
     */
206
    private javax.swing.JTable getTable() {
207
        if (table == null) {
208
            table = new javax.swing.JTable();
209
            table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
210 457 fernando
            table.setSelectionModel(new DefaultListSelectionModel());
211
            table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
212
                    public void valueChanged(ListSelectionEvent e) {
213
                            if (updating) return;
214 428 fernando
215 464 fernando
                            SelectableDataSource dataModel = Table.this.model.getModelo();
216
217
                            DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel();
218
                        BitSet selection = dataModel.getSelection();
219 428 fernando
220 464 fernando
                        for (int i = e.getFirstIndex();
221
                                i <= e.getLastIndex(); i++) {
222
                            selection.set(i, model.isSelectedIndex(i));
223 457 fernando
                        }
224
225
                        if (e.getValueIsAdjusting() == false) {
226
                                if (fmap != null){
227 681 fernando
                                        fmap.endAtomicEvent();
228 457 fernando
                                }
229 464 fernando
                            dataModel.fireSelectionEvents();
230 681 fernando
                        }else{
231 694 fernando
                                if (fmap != null)
232 681 fernando
                                fmap.beginAtomicEvent();
233 457 fernando
                        }
234 428 fernando
                    }
235
                });
236
        }
237
238
        return table;
239
    }
240
241
    /**
242
     * This method initializes jScrollPane
243
     *
244
     * @return javax.swing.JScrollPane
245
     */
246
    private javax.swing.JScrollPane getJScrollPane() {
247
        if (jScrollPane == null) {
248
            jScrollPane = new javax.swing.JScrollPane();
249
            jScrollPane.setViewportView(getTable());
250
        }
251
252
        return jScrollPane;
253
    }
254
255
    /**
256
     * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getViewModel()
257
     */
258
    public Object getViewModel() {
259 457 fernando
        return model;
260 428 fernando
    }
261
262
    /**
263
     * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
264
     */
265
    public ViewInfo getViewInfo() {
266 596 fernando
            ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE | ViewInfo.MAXIMIZABLE |
267 428 fernando
                ViewInfo.RESIZABLE);
268 596 fernando
            m_viewInfo.setTitle(PluginServices.getText(this, "Tabla"));
269 428 fernando
            m_viewInfo.setWidth(300);
270
            m_viewInfo.setHeight(200);
271
272
        return m_viewInfo;
273
    }
274
275
    /**
276 457 fernando
     * DOCUMENT ME!
277
     *
278
     * @author Fernando Gonz?lez Cort?s
279
     */
280
    public class DataSourceDataModel extends AbstractTableModel {
281 464 fernando
        private SelectableDataSource dataSource;
282 457 fernando
283
        /**
284
         * Crea un nuevo DataSourceDataModel.
285
         *
286
         * @param ds DOCUMENT ME!
287
         */
288 464 fernando
        public DataSourceDataModel(SelectableDataSource ds) {
289 457 fernando
            dataSource = ds;
290
        }
291
292
        /**
293
         * DOCUMENT ME!
294
         *
295
         * @param col DOCUMENT ME!
296
         *
297
         * @return DOCUMENT ME!
298
         */
299
        public String getColumnName(int col) {
300
            try {
301
                return dataSource.getFieldName(col).trim();
302
            } catch (DriverException e) {
303
                return e.getMessage();
304
            }
305
        }
306
307
        /**
308
         * DOCUMENT ME!
309
         *
310
         * @return DOCUMENT ME!
311
         */
312
        public int getColumnCount() {
313
            try {
314
                return dataSource.getFieldCount();
315
            } catch (DriverException e) {
316
                return 0;
317
            }
318
        }
319
320
        /**
321
         * DOCUMENT ME!
322
         *
323
         * @return DOCUMENT ME!
324
         */
325
        public int getRowCount() {
326
            try {
327
                return (int) dataSource.getRowCount();
328
            } catch (DriverException e) {
329
                return 0;
330
            }
331
        }
332
333
        /**
334
         * DOCUMENT ME!
335
         *
336
         * @param row DOCUMENT ME!
337
         * @param col DOCUMENT ME!
338
         *
339
         * @return DOCUMENT ME!
340
         */
341
        public Object getValueAt(int row, int col) {
342
            try {
343
                return dataSource.getFieldValue(row, col);
344
            } catch (DriverException e) {
345
                return ValueFactory.createValue("").toString();
346
            }
347
        }
348
    }
349
350 464 fernando
    public class TableSelectionListener implements SelectionListener{
351 457 fernando
352
                /**
353 564 fernando
                 * @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
354 457 fernando
                 */
355 464 fernando
                public void selectionChanged(SelectionEvent e) {
356 457 fernando
                        updateSelection();
357
                        Table.this.repaint();
358
                }
359
360
    }
361 312 fernando
}