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 @ 46447
History | View | Annotate | Download (17.3 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 |
@Override
|
225 |
public List<String> getColumnNames() { |
226 |
return Collections.unmodifiableList(this.columnNames); |
227 |
} |
228 |
|
229 |
private int getColumnHeaderWidth(JTable table, TableColumn tableColumn) { |
230 |
|
231 |
String value = Objects.toString(tableColumn.getHeaderValue());
|
232 |
TableCellRenderer renderer = tableColumn.getHeaderRenderer();
|
233 |
if (renderer == null) { |
234 |
renderer = table.getTableHeader().getDefaultRenderer(); |
235 |
} |
236 |
JComponent c = (JComponent) renderer.getTableCellRendererComponent(table, value, false, false, -1, tableColumn.getModelIndex()); |
237 |
Font font = c.getFont();
|
238 |
FontMetrics fm = c.getFontMetrics(font);
|
239 |
int w = fm.stringWidth(value);
|
240 |
Insets insets = c.getInsets(null); |
241 |
int dx = insets.left + insets.right;
|
242 |
return w+dx;
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public void setCellRenderers(JTable table) { |
247 |
setCellRenderers(table, Collections.EMPTY_MAP);
|
248 |
} |
249 |
|
250 |
public void setCellRenderers(JTable table, Map<String, TableCellRenderer> renderers) { |
251 |
if ( table.getModel() != this ) { |
252 |
return;
|
253 |
} |
254 |
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
255 |
|
256 |
// JScrollPane js=new JScrollPane(table);
|
257 |
// js.setVisible(true);
|
258 |
// table.add(js);
|
259 |
|
260 |
DefaultTableColumnModel columnsModel = new DefaultTableColumnModel(); |
261 |
int n = 0; |
262 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
263 |
FeatureAttributeDescriptor descriptor = null;
|
264 |
for (String columnName : this.columnNames) { |
265 |
try {
|
266 |
descriptor = this.featureType.getAttributeDescriptor(columnName);
|
267 |
if (descriptor == null) { |
268 |
descriptor = this.featureType.getExtraColumns().get(columnName);
|
269 |
} |
270 |
TableColumn columnModel = new TableColumn(); |
271 |
columnModel.setModelIndex(n); |
272 |
columnModel.setHeaderValue(i18n.getTranslation(descriptor.getLabel())); |
273 |
TableCellRenderer renderer = renderers.get(columnName);
|
274 |
if (renderer == null) { |
275 |
renderer = new FeatureAttributeCellRenderer(descriptor);
|
276 |
} |
277 |
columnModel.setCellRenderer(renderer); |
278 |
if (descriptor.getDisplaySize()>0) { |
279 |
int displaySize = ToolsSwingUtils.cols2px(descriptor.getDisplaySize());
|
280 |
columnModel.setPreferredWidth(displaySize); |
281 |
} |
282 |
columnsModel.addColumn(columnModel); |
283 |
n++; |
284 |
} catch (Exception ex) { |
285 |
throw new RuntimeException("Not able to get type of descriptor for column", ex); |
286 |
} |
287 |
} |
288 |
if (descriptor!=null && descriptor.getType()==DataTypes.STRING) { |
289 |
table.getTableHeader().setResizingColumn(columnsModel.getColumn(n-1));
|
290 |
} |
291 |
table.setColumnModel(columnsModel); |
292 |
n = 0;
|
293 |
for (String columnName : this.columnNames) { |
294 |
try {
|
295 |
descriptor = this.featureType.getAttributeDescriptor(columnName);
|
296 |
if (descriptor == null) { |
297 |
descriptor = this.featureType.getExtraColumns().get(columnName);
|
298 |
} |
299 |
TableColumn columnModel = columnsModel.getColumn(n);
|
300 |
if (descriptor.getDisplaySize()<=0) { |
301 |
columnModel.setPreferredWidth(getColumnHeaderWidth(table, columnModel)); |
302 |
} |
303 |
n++; |
304 |
} catch (Exception ex) { |
305 |
throw new RuntimeException("Not able to get type of descriptor for column", ex); |
306 |
} |
307 |
} |
308 |
table.setColumnModel(columnsModel); |
309 |
} |
310 |
|
311 |
@Override
|
312 |
public List<Feature> getFeatures() { |
313 |
return this.features; |
314 |
} |
315 |
|
316 |
@Override
|
317 |
public int getRowCount() { |
318 |
if (this.features == null) { |
319 |
return 0; |
320 |
} |
321 |
try {
|
322 |
return this.features.size(); |
323 |
} catch (Throwable ex) { |
324 |
this.errors = true; |
325 |
LOGGER.warn("Can't calculate row count.", ex);
|
326 |
return 0; |
327 |
} |
328 |
} |
329 |
|
330 |
@Override
|
331 |
public boolean hasErrors() { |
332 |
return this.errors; |
333 |
} |
334 |
|
335 |
@Override
|
336 |
public int getColumnCount() { |
337 |
return this.columnNames.size(); |
338 |
} |
339 |
|
340 |
@Override
|
341 |
public String getColumnName(int columnIndex) { |
342 |
String attrName = this.columnNames.get(columnIndex); |
343 |
if (this.featureType == null) { |
344 |
return attrName;
|
345 |
} |
346 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
347 |
if (attrdesc == null) { |
348 |
EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumns().get(attrName);
|
349 |
if (extraCol != null) { |
350 |
return extraCol.getLocalizedShortLabel();
|
351 |
} |
352 |
if (attrName == null) { |
353 |
return "Column" + columnIndex; |
354 |
} |
355 |
return attrName;
|
356 |
} |
357 |
return attrdesc.getLocalizedShortLabel();
|
358 |
} |
359 |
|
360 |
@Override
|
361 |
public Class<?> getColumnClass(int columnIndex) { |
362 |
if (this.featureType == null) { |
363 |
return String.class; |
364 |
} |
365 |
try {
|
366 |
String attrName = this.columnNames.get(columnIndex); |
367 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
368 |
if (attrdesc == null) { |
369 |
int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
|
370 |
if (extraIndex != -1) { |
371 |
attrdesc = featureType.getExtraColumns().get(extraIndex); |
372 |
} |
373 |
} |
374 |
if (attrdesc == null) { |
375 |
return String.class; |
376 |
} |
377 |
if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
|
378 |
return String.class; |
379 |
} |
380 |
Class theClass = attrdesc.getDataType().getDefaultClass();
|
381 |
if (theClass == null) { |
382 |
return String.class; |
383 |
} |
384 |
return theClass;
|
385 |
} catch (Exception ex) { |
386 |
return String.class; |
387 |
} |
388 |
} |
389 |
|
390 |
@Override
|
391 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
392 |
return false; |
393 |
} |
394 |
|
395 |
public FeatureAttributeDescriptor getFeatureDescriptor(int columnIndex) { |
396 |
String attrName = this.columnNames.get(columnIndex); |
397 |
if (this.featureType == null) { |
398 |
return null; |
399 |
} |
400 |
for (FeatureAttributeDescriptor attr : this.featureType.getAllAttributeDescriptors()) { |
401 |
if (StringUtils.equals(attrName,attr.getName())) {
|
402 |
return attr;
|
403 |
} |
404 |
} |
405 |
return null; |
406 |
} |
407 |
|
408 |
@Override
|
409 |
public Feature get(int position) { |
410 |
if (this.features == null) { |
411 |
return null; |
412 |
} |
413 |
Feature feature = this.features.get(position);
|
414 |
return feature;
|
415 |
} |
416 |
|
417 |
@Override
|
418 |
public Feature getFeatureAt(int rowIndex) { |
419 |
if (this.features == null || rowIndex<0 ) { |
420 |
return null; |
421 |
} |
422 |
try {
|
423 |
Feature feature = this.features.get(rowIndex);
|
424 |
return feature;
|
425 |
} catch (Throwable th) { |
426 |
this.errors = true; |
427 |
logger.warn("Can't get feature at row " + rowIndex + ".", th); |
428 |
return null; |
429 |
} |
430 |
} |
431 |
|
432 |
@Override
|
433 |
public Object getValueAt(int rowIndex, int columnIndex) { |
434 |
if (this.features == null) { |
435 |
return null; |
436 |
} |
437 |
try {
|
438 |
Feature feature = this.features.get(rowIndex);
|
439 |
String attrName = this.columnNames.get(columnIndex); |
440 |
Object value = null; |
441 |
value = feature.get(attrName); |
442 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
443 |
if(attrdesc == null && this.featureType.getExtraColumns().get(attrName)!=null) { |
444 |
attrdesc = this.featureType.getExtraColumns().get(attrName);
|
445 |
} |
446 |
if (attrdesc != null) { |
447 |
if (value == null) { |
448 |
return null; |
449 |
} |
450 |
if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
|
451 |
value = attrdesc.getForeingKey().getLabelForValue(value); |
452 |
} |
453 |
} |
454 |
return value;
|
455 |
} catch (Throwable th) { |
456 |
this.errors = true; |
457 |
logger.warn("Can't get cell value at " + rowIndex + ", " + columnIndex + ".", th); |
458 |
return null; |
459 |
} |
460 |
} |
461 |
|
462 |
@Override
|
463 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
464 |
|
465 |
} |
466 |
|
467 |
@Override
|
468 |
public List<Feature> toList() { |
469 |
return this.features; |
470 |
} |
471 |
|
472 |
@Override
|
473 |
public boolean isEmpty() { |
474 |
return this.features.isEmpty(); |
475 |
} |
476 |
|
477 |
@Override
|
478 |
public int size() { |
479 |
return this.features.size(); |
480 |
} |
481 |
|
482 |
@Override
|
483 |
public Iterator<Feature> iterator() { |
484 |
return this.features.iterator(); |
485 |
} |
486 |
|
487 |
@Override
|
488 |
public void dispose() { |
489 |
if (this.features != null) { |
490 |
DisposeUtils.disposeQuietly(((FacadeOfAFeaturePagingHelper) features).getFeaturePagingHelper()); |
491 |
this.features = null; |
492 |
} |
493 |
this.features = null; |
494 |
} |
495 |
|
496 |
} |