svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretable / SimpleFeaturesTableModelImpl.java @ 46485
History | View | Annotate | Download (17.5 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featuretable; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Component; |
5 |
import java.awt.Font; |
6 |
import java.awt.FontMetrics; |
7 |
import java.awt.Insets; |
8 |
import org.gvsig.fmap.dal.swing.featuretable.SimpleFeaturesTableModel; |
9 |
import java.util.ArrayList; |
10 |
import java.util.Collections; |
11 |
import java.util.Iterator; |
12 |
import java.util.List; |
13 |
import java.util.Map; |
14 |
import java.util.Objects; |
15 |
import javax.swing.JCheckBox; |
16 |
import javax.swing.JComponent; |
17 |
import javax.swing.JLabel; |
18 |
import javax.swing.JTable; |
19 |
import javax.swing.SwingConstants; |
20 |
import javax.swing.UIManager; |
21 |
import javax.swing.table.AbstractTableModel; |
22 |
import javax.swing.table.DefaultTableCellRenderer; |
23 |
import javax.swing.table.DefaultTableColumnModel; |
24 |
import javax.swing.table.TableCellRenderer; |
25 |
import javax.swing.table.TableColumn; |
26 |
import org.apache.commons.lang3.StringUtils; |
27 |
import org.gvsig.expressionevaluator.Expression; |
28 |
import org.gvsig.fmap.dal.complements.Search; |
29 |
import org.gvsig.fmap.dal.exception.DataException; |
30 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
31 |
import org.gvsig.fmap.dal.feature.Feature; |
32 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.dal.feature.FeatureType; |
35 |
import org.gvsig.fmap.dal.feature.paging.FacadeOfAFeaturePagingHelper; |
36 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
37 |
import org.gvsig.tools.ToolsLocator; |
38 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
39 |
import org.gvsig.tools.dataTypes.DataTypes; |
40 |
import org.gvsig.tools.dispose.DisposeUtils; |
41 |
import org.gvsig.tools.i18n.I18nManager; |
42 |
import org.gvsig.tools.logger.FilteredLogger; |
43 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
44 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
/**
|
49 |
*
|
50 |
* @author jjdelcerro
|
51 |
*/
|
52 |
public class SimpleFeaturesTableModelImpl |
53 |
extends AbstractTableModel |
54 |
implements SimpleFeaturesTableModel {
|
55 |
|
56 |
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFeaturesTableModelImpl.class); |
57 |
|
58 |
|
59 |
// public static class LegendOne implements TableCellRenderer {
|
60 |
// public LegendOne() {
|
61 |
//
|
62 |
// }
|
63 |
//
|
64 |
// @Override
|
65 |
// public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
|
66 |
// TableCellRenderer renderer = this.getRenderByValue(o);
|
67 |
// Component xxx = renderer.getTableCellRendererComponent(jtable, o, bln, bln1, i, i1);
|
68 |
// return xxx;
|
69 |
// }
|
70 |
//
|
71 |
// public Object getSymbolByValue(Object value) {
|
72 |
//
|
73 |
// return null;
|
74 |
// }
|
75 |
//
|
76 |
// public TableCellRenderer getRenderByValue(Object value) {
|
77 |
// Object symbol = getSymbolByValue(value);
|
78 |
// return null;symbol.createRenderer();
|
79 |
// }
|
80 |
// }
|
81 |
|
82 |
|
83 |
public static class ColorRangeRenderer extends FeatureAttributeCellRenderer { |
84 |
|
85 |
private final double max; |
86 |
private final double min; |
87 |
|
88 |
public ColorRangeRenderer(FeatureAttributeDescriptor descriptor,double min, double max) { |
89 |
super(descriptor);
|
90 |
this.min = min;
|
91 |
this.max = max;
|
92 |
} |
93 |
|
94 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
95 |
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
96 |
|
97 |
if (this.getDescriptor().getDataType().isNumeric()) { |
98 |
double myvalue = 0; |
99 |
if (value==null) { |
100 |
myvalue = 0;
|
101 |
} else {
|
102 |
myvalue = DataTypeUtils.toDouble(value); |
103 |
} |
104 |
Color color = this.getColor(myvalue); |
105 |
label.setBackground(color); |
106 |
} |
107 |
return label;
|
108 |
} |
109 |
|
110 |
public Color getColor(double n) { |
111 |
if (n<=min) {
|
112 |
n = 0;
|
113 |
} else if(n>=max) { |
114 |
n = max-min; |
115 |
} |
116 |
int per = DataTypeUtils.toInteger((100*(n-min))/(max-min)); |
117 |
int R = (255 * per) / 100; |
118 |
int G = (255 * (100 - per)) / 100; |
119 |
int B = 0; |
120 |
Color color;
|
121 |
try {
|
122 |
color = new Color(R, G, B, 100); |
123 |
} catch(Exception ex) { |
124 |
color = Color.GRAY;
|
125 |
} |
126 |
return color;
|
127 |
} |
128 |
} |
129 |
|
130 |
private static class FeatureAttributeCellRenderer extends DefaultTableCellRenderer { |
131 |
|
132 |
private final FeatureAttributeDescriptor descriptor; |
133 |
private final JCheckBox check; |
134 |
|
135 |
public FeatureAttributeCellRenderer(FeatureAttributeDescriptor descriptor) {
|
136 |
this.descriptor = descriptor;
|
137 |
this.check = new JCheckBox(); |
138 |
this.check.setHorizontalAlignment(SwingConstants.CENTER); |
139 |
this.check.setBackground(UIManager.getColor(ToolsSwingManager.COLOR_TABLE_BACKGROUND)); |
140 |
} |
141 |
// @Override
|
142 |
// protected void setValue(Object value) {
|
143 |
// if (value == null) {
|
144 |
// setText("");
|
145 |
// } else {
|
146 |
// setText((String) descriptor.format(value));
|
147 |
// }
|
148 |
// }
|
149 |
|
150 |
@Override
|
151 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
152 |
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
153 |
int type = descriptor.getType();
|
154 |
if (value instanceof Boolean && type==DataTypes.BOOLEAN) { |
155 |
check.setSelected((boolean) value);
|
156 |
check.setBackground(label.getBackground()); |
157 |
return check;
|
158 |
} |
159 |
label.setText((String) descriptor.format(value));
|
160 |
if (descriptor.getAvailableValues()==null && (descriptor.getDataType().isNumeric() || |
161 |
type == DataTypes.TIMESTAMP || |
162 |
type == DataTypes.TIME || |
163 |
type == DataTypes.DATE)) { |
164 |
label.setHorizontalAlignment(SwingConstants.RIGHT);
|
165 |
} else if (type==DataTypes.BOOLEAN) { |
166 |
label.setHorizontalAlignment(SwingConstants.CENTER);
|
167 |
label.setText("null");
|
168 |
} else {
|
169 |
label.setHorizontalAlignment(SwingConstants.LEFT);
|
170 |
} |
171 |
return label;
|
172 |
} |
173 |
|
174 |
public FeatureAttributeDescriptor getDescriptor() {
|
175 |
return this.descriptor; |
176 |
} |
177 |
|
178 |
|
179 |
} |
180 |
|
181 |
private List<Feature> features; |
182 |
private final List<String> columnNames; |
183 |
private final FeatureType featureType; |
184 |
private FilteredLogger logger;
|
185 |
private boolean errors; |
186 |
|
187 |
public SimpleFeaturesTableModelImpl(FeatureStore store) throws DataException { |
188 |
this(store.getDefaultFeatureType(), null, store.getFeatures()); |
189 |
} |
190 |
|
191 |
public SimpleFeaturesTableModelImpl(FeatureStore store, Expression filter) throws DataException { |
192 |
this(store.getDefaultFeatureType(), null, store.getFeatures(filter)); |
193 |
} |
194 |
|
195 |
public SimpleFeaturesTableModelImpl(FeatureType featureType) {
|
196 |
this(featureType, null, null); |
197 |
this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
198 |
} |
199 |
|
200 |
public SimpleFeaturesTableModelImpl(FeatureType featureType, List<String> columnNames, List<Feature> features) { |
201 |
this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
202 |
this.features = features;
|
203 |
this.featureType = featureType;
|
204 |
this.errors = false; |
205 |
if (columnNames == null || columnNames.isEmpty()) { |
206 |
this.columnNames = new ArrayList<>(); |
207 |
Search search = (Search) ToolsLocator.getComplementsManager().get( |
208 |
Search.COMPLEMENT_MANE, featureType |
209 |
); |
210 |
List<Search.OrderedAttribute> attributos = search.getOrderedAttributes(
|
211 |
Search.BASIC_TYPES_FILTER, |
212 |
Search.STR_INT_LONG_LABEL_ORDER, |
213 |
12
|
214 |
); |
215 |
for (Search.OrderedAttribute attrdesc : attributos) {
|
216 |
this.columnNames.add(attrdesc.getDescriptor().getName());
|
217 |
} |
218 |
} else {
|
219 |
this.columnNames = columnNames;
|
220 |
} |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public List<String> getColumnNames() { |
225 |
return Collections.unmodifiableList(this.columnNames); |
226 |
} |
227 |
|
228 |
private int getColumnHeaderWidth(JTable table, TableColumn tableColumn) { |
229 |
|
230 |
String value = Objects.toString(tableColumn.getHeaderValue());
|
231 |
TableCellRenderer renderer = tableColumn.getHeaderRenderer();
|
232 |
if (renderer == null) { |
233 |
renderer = table.getTableHeader().getDefaultRenderer(); |
234 |
} |
235 |
JComponent c = (JComponent) renderer.getTableCellRendererComponent(table, value, false, false, -1, tableColumn.getModelIndex()); |
236 |
Font font = c.getFont();
|
237 |
FontMetrics fm = c.getFontMetrics(font);
|
238 |
int w = fm.stringWidth(value);
|
239 |
Insets insets = c.getInsets(null); |
240 |
int dx = insets.left + insets.right;
|
241 |
return w+dx;
|
242 |
} |
243 |
|
244 |
@Override
|
245 |
public void setCellRenderers(JTable table) { |
246 |
setCellRenderers(table, Collections.EMPTY_MAP);
|
247 |
} |
248 |
|
249 |
public void setCellRenderers(JTable table, Map<String, TableCellRenderer> renderers) { |
250 |
if ( table.getModel() != this ) { |
251 |
return;
|
252 |
} |
253 |
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
254 |
|
255 |
// JScrollPane js=new JScrollPane(table);
|
256 |
// js.setVisible(true);
|
257 |
// table.add(js);
|
258 |
|
259 |
DefaultTableColumnModel columnsModel = new DefaultTableColumnModel(); |
260 |
int n = 0; |
261 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
262 |
FeatureAttributeDescriptor descriptor = null;
|
263 |
for (String columnName : this.columnNames) { |
264 |
try {
|
265 |
descriptor = this.featureType.getAttributeDescriptor(columnName);
|
266 |
if (descriptor == null) { |
267 |
descriptor = this.featureType.getExtraColumns().get(columnName);
|
268 |
} |
269 |
TableColumn columnModel = new TableColumn(); |
270 |
columnModel.setModelIndex(n); |
271 |
columnModel.setHeaderValue(DALSwingLocator.getDataSwingManager().getAttributeDescriptorLabel(descriptor)); |
272 |
TableCellRenderer renderer = renderers.get(columnName);
|
273 |
if (renderer == null) { |
274 |
renderer = new FeatureAttributeCellRenderer(descriptor);
|
275 |
} |
276 |
columnModel.setCellRenderer(renderer); |
277 |
if (descriptor.getDisplaySize()>0) { |
278 |
int displaySize = ToolsSwingUtils.cols2px(descriptor.getDisplaySize());
|
279 |
columnModel.setPreferredWidth(displaySize); |
280 |
} |
281 |
columnsModel.addColumn(columnModel); |
282 |
n++; |
283 |
} catch (Exception ex) { |
284 |
throw new RuntimeException("Not able to get type of descriptor for column", ex); |
285 |
} |
286 |
} |
287 |
if (descriptor!=null && descriptor.getType()==DataTypes.STRING) { |
288 |
table.getTableHeader().setResizingColumn(columnsModel.getColumn(n-1));
|
289 |
} |
290 |
table.setColumnModel(columnsModel); |
291 |
n = 0;
|
292 |
for (String columnName : this.columnNames) { |
293 |
try {
|
294 |
descriptor = this.featureType.getAttributeDescriptor(columnName);
|
295 |
if (descriptor == null) { |
296 |
descriptor = this.featureType.getExtraColumns().get(columnName);
|
297 |
} |
298 |
TableColumn columnModel = columnsModel.getColumn(n);
|
299 |
if (descriptor.getDisplaySize()<=0) { |
300 |
columnModel.setPreferredWidth(getColumnHeaderWidth(table, columnModel)); |
301 |
} |
302 |
n++; |
303 |
} catch (Exception ex) { |
304 |
throw new RuntimeException("Not able to get type of descriptor for column", ex); |
305 |
} |
306 |
} |
307 |
table.setColumnModel(columnsModel); |
308 |
} |
309 |
|
310 |
@Override
|
311 |
public List<Feature> getFeatures() { |
312 |
return this.features; |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public void dispose() { |
317 |
if (this.features != null) { |
318 |
if( this.features instanceof FacadeOfAFeaturePagingHelper) { |
319 |
DisposeUtils.disposeQuietly(((FacadeOfAFeaturePagingHelper) features).getFeaturePagingHelper()); |
320 |
} else {
|
321 |
DisposeUtils.disposeQuietly(features); |
322 |
} |
323 |
this.features = null; |
324 |
} |
325 |
this.features = null; |
326 |
} |
327 |
|
328 |
@Override
|
329 |
public int getRowCount() { |
330 |
if (this.getFeatures() == null) { |
331 |
return 0; |
332 |
} |
333 |
try {
|
334 |
return this.getFeatures().size(); |
335 |
} catch (Throwable ex) { |
336 |
this.errors = true; |
337 |
LOGGER.warn("Can't calculate row count.", ex);
|
338 |
return 0; |
339 |
} |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public boolean hasErrors() { |
344 |
return this.errors; |
345 |
} |
346 |
|
347 |
@Override
|
348 |
public int getColumnCount() { |
349 |
return this.columnNames.size(); |
350 |
} |
351 |
|
352 |
@Override
|
353 |
public String getColumnName(int columnIndex) { |
354 |
String attrName = this.columnNames.get(columnIndex); |
355 |
if (this.featureType == null) { |
356 |
return attrName;
|
357 |
} |
358 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
359 |
if (attrdesc == null) { |
360 |
EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumns().get(attrName);
|
361 |
if (extraCol != null) { |
362 |
return extraCol.getLocalizedShortLabel();
|
363 |
} |
364 |
if (attrName == null) { |
365 |
return "Column" + columnIndex; |
366 |
} |
367 |
return attrName;
|
368 |
} |
369 |
return attrdesc.getLocalizedShortLabel();
|
370 |
} |
371 |
|
372 |
@Override
|
373 |
public Class<?> getColumnClass(int columnIndex) { |
374 |
if (this.featureType == null) { |
375 |
return String.class; |
376 |
} |
377 |
try {
|
378 |
String attrName = this.columnNames.get(columnIndex); |
379 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
380 |
if (attrdesc == null) { |
381 |
int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
|
382 |
if (extraIndex != -1) { |
383 |
attrdesc = featureType.getExtraColumns().get(extraIndex); |
384 |
} |
385 |
} |
386 |
if (attrdesc == null) { |
387 |
return String.class; |
388 |
} |
389 |
if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
|
390 |
return String.class; |
391 |
} |
392 |
Class theClass = attrdesc.getDataType().getDefaultClass();
|
393 |
if (theClass == null) { |
394 |
return String.class; |
395 |
} |
396 |
return theClass;
|
397 |
} catch (Exception ex) { |
398 |
return String.class; |
399 |
} |
400 |
} |
401 |
|
402 |
@Override
|
403 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
404 |
return false; |
405 |
} |
406 |
|
407 |
public FeatureAttributeDescriptor getFeatureDescriptor(int columnIndex) { |
408 |
String attrName = this.columnNames.get(columnIndex); |
409 |
if (this.featureType == null) { |
410 |
return null; |
411 |
} |
412 |
for (FeatureAttributeDescriptor attr : this.featureType.getAllAttributeDescriptors()) { |
413 |
if (StringUtils.equals(attrName,attr.getName())) {
|
414 |
return attr;
|
415 |
} |
416 |
} |
417 |
return null; |
418 |
} |
419 |
|
420 |
@Override
|
421 |
public Feature get(int position) { |
422 |
if (this.getFeatures() == null) { |
423 |
return null; |
424 |
} |
425 |
Feature feature = this.getFeatures().get(position);
|
426 |
return feature;
|
427 |
} |
428 |
|
429 |
@Override
|
430 |
public Feature getFeatureAt(int rowIndex) { |
431 |
if (this.getFeatures() == null || rowIndex<0 ) { |
432 |
return null; |
433 |
} |
434 |
try {
|
435 |
Feature feature = this.getFeatures().get(rowIndex);
|
436 |
return feature;
|
437 |
} catch (Throwable th) { |
438 |
this.errors = true; |
439 |
logger.warn("Can't get feature at row " + rowIndex + ".", th); |
440 |
return null; |
441 |
} |
442 |
} |
443 |
|
444 |
@Override
|
445 |
public Object getValueAt(int rowIndex, int columnIndex) { |
446 |
if (this.getFeatures() == null) { |
447 |
return null; |
448 |
} |
449 |
try {
|
450 |
Feature feature = this.getFeatures().get(rowIndex);
|
451 |
String attrName = this.columnNames.get(columnIndex); |
452 |
Object value = null; |
453 |
value = feature.get(attrName); |
454 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
455 |
if(attrdesc == null && this.featureType.getExtraColumns().get(attrName)!=null) { |
456 |
attrdesc = this.featureType.getExtraColumns().get(attrName);
|
457 |
} |
458 |
if (attrdesc != null) { |
459 |
if (value == null) { |
460 |
return null; |
461 |
} |
462 |
if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
|
463 |
value = attrdesc.getForeingKey().getLabelForValue(value); |
464 |
} |
465 |
} |
466 |
return value;
|
467 |
} catch (Throwable th) { |
468 |
this.errors = true; |
469 |
logger.warn("Can't get cell value at " + rowIndex + ", " + columnIndex + ".", th); |
470 |
return null; |
471 |
} |
472 |
} |
473 |
|
474 |
@Override
|
475 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
476 |
|
477 |
} |
478 |
|
479 |
@Override
|
480 |
public List<Feature> toList() { |
481 |
return this.getFeatures(); |
482 |
} |
483 |
|
484 |
@Override
|
485 |
public boolean isEmpty() { |
486 |
return this.getFeatures().isEmpty(); |
487 |
} |
488 |
|
489 |
@Override
|
490 |
public int size() { |
491 |
return this.getFeatures().size(); |
492 |
} |
493 |
|
494 |
@Override
|
495 |
public Iterator<Feature> iterator() { |
496 |
return this.getFeatures().iterator(); |
497 |
} |
498 |
|
499 |
} |