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 |
/* 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; |
42 |
|
43 |
import java.beans.PropertyChangeEvent; |
44 |
import java.beans.PropertyChangeListener; |
45 |
import java.text.DateFormat; |
46 |
import java.text.NumberFormat; |
47 |
import java.util.BitSet; |
48 |
|
49 |
import javax.swing.DefaultListSelectionModel; |
50 |
import javax.swing.JLabel; |
51 |
import javax.swing.JPanel; |
52 |
import javax.swing.event.ListSelectionEvent; |
53 |
import javax.swing.event.ListSelectionListener; |
54 |
import javax.swing.table.AbstractTableModel; |
55 |
|
56 |
import org.apache.log4j.Logger; |
57 |
|
58 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
59 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
60 |
import com.iver.andami.PluginServices; |
61 |
import com.iver.andami.messages.NotificationManager; |
62 |
import com.iver.andami.ui.mdiManager.SingletonView; |
63 |
import com.iver.andami.ui.mdiManager.ViewInfo; |
64 |
import com.iver.andami.ui.mdiManager.ViewListener; |
65 |
import com.iver.cit.gvsig.fmap.FMap; |
66 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
67 |
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 |
import com.iver.cit.gvsig.project.ProjectTable; |
71 |
|
72 |
|
73 |
/**
|
74 |
* DOCUMENT ME!
|
75 |
*
|
76 |
* @author Fernando Gonz?lez Cort?s
|
77 |
*/
|
78 |
public class Table extends JPanel implements SingletonView { |
79 |
private static Logger logger = Logger.getLogger(Table.class.getName()); |
80 |
private javax.swing.JScrollPane jScrollPane = null; |
81 |
private javax.swing.JTable table = null; |
82 |
private ProjectTable model = null; |
83 |
private DateFormat df = DateFormat.getDateInstance(); |
84 |
private NumberFormat nf = NumberFormat.getInstance(); |
85 |
private JLabel jLabelStatus = null; |
86 |
private ViewInfo m_viewInfo = null; |
87 |
private FMap fmap;
|
88 |
|
89 |
private boolean updating = false; |
90 |
|
91 |
private TableSelectionListener selectionListener = new TableSelectionListener(); |
92 |
|
93 |
/**
|
94 |
* This is the default constructor
|
95 |
*/
|
96 |
public Table() {
|
97 |
super();
|
98 |
initialize(); |
99 |
} |
100 |
|
101 |
/**
|
102 |
* DOCUMENT ME!
|
103 |
*
|
104 |
* @return DOCUMENT ME!
|
105 |
*/
|
106 |
public ProjectTable getModel() {
|
107 |
return model;
|
108 |
} |
109 |
|
110 |
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 |
/**
|
145 |
* DOCUMENT ME!
|
146 |
*
|
147 |
* @param table DOCUMENT ME!
|
148 |
*/
|
149 |
public void setModel(ProjectTable table) { |
150 |
SelectableDataSource dataSource; |
151 |
model = table; |
152 |
|
153 |
//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 |
} |
166 |
|
167 |
/**
|
168 |
*
|
169 |
*/
|
170 |
private void updateSelection() { |
171 |
updating = true;
|
172 |
|
173 |
DefaultListSelectionModel sm = (DefaultListSelectionModel) getTable() |
174 |
.getSelectionModel(); |
175 |
sm.clearSelection(); |
176 |
BitSet bs = model.getModelo().getSelection();
|
177 |
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 |
* 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 |
table.setSelectionModel(new DefaultListSelectionModel()); |
211 |
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
212 |
public void valueChanged(ListSelectionEvent e) { |
213 |
if (updating) return; |
214 |
|
215 |
SelectableDataSource dataModel = Table.this.model.getModelo(); |
216 |
|
217 |
DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel(); |
218 |
BitSet selection = dataModel.getSelection();
|
219 |
|
220 |
for (int i = e.getFirstIndex(); |
221 |
i <= e.getLastIndex(); i++) { |
222 |
selection.set(i, model.isSelectedIndex(i)); |
223 |
} |
224 |
|
225 |
if (e.getValueIsAdjusting() == false) { |
226 |
if (fmap != null){ |
227 |
fmap.endAtomicEvent(); |
228 |
} |
229 |
dataModel.fireSelectionEvents(); |
230 |
}else{
|
231 |
if (fmap != null) |
232 |
fmap.beginAtomicEvent(); |
233 |
} |
234 |
} |
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 |
return model;
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
|
264 |
*/
|
265 |
public ViewInfo getViewInfo() {
|
266 |
ViewInfo m_viewInfo = new ViewInfo(ViewInfo.ICONIFIABLE | ViewInfo.MAXIMIZABLE |
|
267 |
ViewInfo.RESIZABLE); |
268 |
m_viewInfo.setTitle(PluginServices.getText(this, "Tabla")); |
269 |
m_viewInfo.setWidth(300);
|
270 |
m_viewInfo.setHeight(200);
|
271 |
|
272 |
return m_viewInfo;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* DOCUMENT ME!
|
277 |
*
|
278 |
* @author Fernando Gonz?lez Cort?s
|
279 |
*/
|
280 |
public class DataSourceDataModel extends AbstractTableModel { |
281 |
private SelectableDataSource dataSource;
|
282 |
|
283 |
/**
|
284 |
* Crea un nuevo DataSourceDataModel.
|
285 |
*
|
286 |
* @param ds DOCUMENT ME!
|
287 |
*/
|
288 |
public DataSourceDataModel(SelectableDataSource ds) {
|
289 |
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 |
public class TableSelectionListener implements SelectionListener{ |
351 |
|
352 |
/**
|
353 |
* @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
354 |
*/
|
355 |
public void selectionChanged(SelectionEvent e) { |
356 |
updateSelection(); |
357 |
Table.this.repaint(); |
358 |
} |
359 |
|
360 |
} |
361 |
} |