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