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