Statistics
| Revision:

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 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 45393 jjdelcerro
import org.gvsig.fmap.dal.swing.featuretable.SimpleFeaturesTableModel;
9 44340 jjdelcerro
import java.util.ArrayList;
10 46051 omartinez
import java.util.Collections;
11 44340 jjdelcerro
import java.util.Iterator;
12
import java.util.List;
13 46051 omartinez
import java.util.Map;
14 45995 omartinez
import java.util.Objects;
15
import javax.swing.JCheckBox;
16
import javax.swing.JComponent;
17
import javax.swing.JLabel;
18 45185 jjdelcerro
import javax.swing.JTable;
19 45995 omartinez
import javax.swing.SwingConstants;
20
import javax.swing.UIManager;
21 44340 jjdelcerro
import javax.swing.table.AbstractTableModel;
22 45185 jjdelcerro
import javax.swing.table.DefaultTableCellRenderer;
23 45995 omartinez
import javax.swing.table.DefaultTableColumnModel;
24
import javax.swing.table.TableCellRenderer;
25
import javax.swing.table.TableColumn;
26 45695 omartinez
import org.apache.commons.lang3.StringUtils;
27 46472 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
28 44340 jjdelcerro
import org.gvsig.fmap.dal.complements.Search;
29
import org.gvsig.fmap.dal.exception.DataException;
30 44794 omartinez
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
31 44340 jjdelcerro
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 45100 jjdelcerro
import org.gvsig.fmap.dal.feature.paging.FacadeOfAFeaturePagingHelper;
36 46485 fdiaz
import org.gvsig.fmap.dal.swing.DALSwingLocator;
37 44340 jjdelcerro
import org.gvsig.tools.ToolsLocator;
38 46051 omartinez
import org.gvsig.tools.dataTypes.DataTypeUtils;
39 45185 jjdelcerro
import org.gvsig.tools.dataTypes.DataTypes;
40 45100 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
41 45995 omartinez
import org.gvsig.tools.i18n.I18nManager;
42 44400 jjdelcerro
import org.gvsig.tools.logger.FilteredLogger;
43 45995 omartinez
import org.gvsig.tools.swing.api.ToolsSwingManager;
44
import org.gvsig.tools.swing.api.ToolsSwingUtils;
45 44400 jjdelcerro
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47 44340 jjdelcerro
48
/**
49
 *
50
 * @author jjdelcerro
51
 */
52 45393 jjdelcerro
public class SimpleFeaturesTableModelImpl
53 44340 jjdelcerro
        extends AbstractTableModel
54 45393 jjdelcerro
        implements SimpleFeaturesTableModel {
55 44340 jjdelcerro
56 45393 jjdelcerro
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFeaturesTableModelImpl.class);
57 46051 omartinez
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 44340 jjdelcerro
85 46051 omartinez
        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 45185 jjdelcerro
    private static class FeatureAttributeCellRenderer extends DefaultTableCellRenderer {
131
132
        private final FeatureAttributeDescriptor descriptor;
133 45995 omartinez
        private final JCheckBox check;
134 45185 jjdelcerro
135
        public FeatureAttributeCellRenderer(FeatureAttributeDescriptor descriptor) {
136
            this.descriptor = descriptor;
137 45995 omartinez
            this.check = new JCheckBox();
138
            this.check.setHorizontalAlignment(SwingConstants.CENTER);
139
            this.check.setBackground(UIManager.getColor(ToolsSwingManager.COLOR_TABLE_BACKGROUND));
140 44340 jjdelcerro
        }
141 45995 omartinez
//        @Override
142
//        protected void setValue(Object value) {
143
//            if (value == null) {
144
//                setText("");
145
//            } else {
146
//                setText((String) descriptor.format(value));
147
//            }
148
//        }
149 45201 omartinez
150 45185 jjdelcerro
        @Override
151 45995 omartinez
        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 45185 jjdelcerro
            } else {
169 45995 omartinez
                label.setHorizontalAlignment(SwingConstants.LEFT);
170 44340 jjdelcerro
            }
171 45995 omartinez
            return label;
172 44340 jjdelcerro
        }
173 45995 omartinez
174 46051 omartinez
        public FeatureAttributeDescriptor getDescriptor() {
175
            return this.descriptor;
176
        }
177
178 44340 jjdelcerro
179 45185 jjdelcerro
    }
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 45393 jjdelcerro
    public SimpleFeaturesTableModelImpl(FeatureStore store) throws DataException {
188
        this(store.getDefaultFeatureType(), null, store.getFeatures());
189
    }
190
191 46472 jjdelcerro
    public SimpleFeaturesTableModelImpl(FeatureStore store, Expression filter) throws DataException {
192
        this(store.getDefaultFeatureType(), null, store.getFeatures(filter));
193
    }
194
195 45393 jjdelcerro
    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 45995 omartinez
223 46447 jjdelcerro
    @Override
224
    public List<String> getColumnNames() {
225
        return Collections.unmodifiableList(this.columnNames);
226
    }
227
228 45995 omartinez
    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 45393 jjdelcerro
244 46051 omartinez
    @Override
245 45393 jjdelcerro
    public void setCellRenderers(JTable table) {
246 46051 omartinez
        setCellRenderers(table, Collections.EMPTY_MAP);
247
    }
248
249
    public void setCellRenderers(JTable table, Map<String, TableCellRenderer> renderers) {
250 45393 jjdelcerro
        if ( table.getModel() != this ) {
251 45185 jjdelcerro
            return;
252 44340 jjdelcerro
        }
253 45995 omartinez
        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 45393 jjdelcerro
        for (String columnName : this.columnNames) {
264 45201 omartinez
            try {
265 45995 omartinez
                descriptor = this.featureType.getAttributeDescriptor(columnName);
266 45201 omartinez
                if (descriptor == null) {
267 45393 jjdelcerro
                    descriptor = this.featureType.getExtraColumns().get(columnName);
268 45201 omartinez
                }
269 45995 omartinez
                TableColumn columnModel = new TableColumn();
270
                columnModel.setModelIndex(n);
271 46485 fdiaz
                columnModel.setHeaderValue(DALSwingLocator.getDataSwingManager().getAttributeDescriptorLabel(descriptor));
272 46051 omartinez
                TableCellRenderer renderer = renderers.get(columnName);
273
                if (renderer == null) {
274
                    renderer = new FeatureAttributeCellRenderer(descriptor);
275
                }
276
                columnModel.setCellRenderer(renderer);
277 45995 omartinez
                if (descriptor.getDisplaySize()>0) {
278
                    int displaySize = ToolsSwingUtils.cols2px(descriptor.getDisplaySize());
279
                    columnModel.setPreferredWidth(displaySize);
280 45201 omartinez
                }
281 45995 omartinez
                columnsModel.addColumn(columnModel);
282
                n++;
283 45201 omartinez
            } catch (Exception ex) {
284
                throw new RuntimeException("Not able to get type of descriptor for column", ex);
285 45185 jjdelcerro
            }
286
        }
287 45995 omartinez
        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 45185 jjdelcerro
    }
309 44340 jjdelcerro
310 45393 jjdelcerro
    @Override
311 45185 jjdelcerro
    public List<Feature> getFeatures() {
312
        return this.features;
313
    }
314
315
    @Override
316 46472 jjdelcerro
    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 45185 jjdelcerro
    public int getRowCount() {
330 46472 jjdelcerro
        if (this.getFeatures() == null) {
331 45185 jjdelcerro
            return 0;
332 44836 jjdelcerro
        }
333 45185 jjdelcerro
        try {
334 46472 jjdelcerro
            return this.getFeatures().size();
335 45185 jjdelcerro
        } catch (Throwable ex) {
336
            this.errors = true;
337
            LOGGER.warn("Can't calculate row count.", ex);
338
            return 0;
339 44340 jjdelcerro
        }
340 45185 jjdelcerro
    }
341 44340 jjdelcerro
342 45393 jjdelcerro
    @Override
343 45185 jjdelcerro
    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 44340 jjdelcerro
            }
364 45185 jjdelcerro
            if (attrName == null) {
365
                return "Column" + columnIndex;
366 44340 jjdelcerro
            }
367 45185 jjdelcerro
            return attrName;
368 44340 jjdelcerro
        }
369 45185 jjdelcerro
        return attrdesc.getLocalizedShortLabel();
370
    }
371 44340 jjdelcerro
372 45185 jjdelcerro
    @Override
373
    public Class<?> getColumnClass(int columnIndex) {
374
        if (this.featureType == null) {
375
            return String.class;
376
        }
377
        try {
378 44340 jjdelcerro
            String attrName = this.columnNames.get(columnIndex);
379
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
380
            if (attrdesc == null) {
381 44829 omartinez
                int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
382 44794 omartinez
                if (extraIndex != -1) {
383 44829 omartinez
                    attrdesc = featureType.getExtraColumns().get(extraIndex);
384 44794 omartinez
                }
385
            }
386
            if (attrdesc == null) {
387 44340 jjdelcerro
                return String.class;
388
            }
389 45185 jjdelcerro
            if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
390 44340 jjdelcerro
                return String.class;
391
            }
392
            Class theClass = attrdesc.getDataType().getDefaultClass();
393 45185 jjdelcerro
            if (theClass == null) {
394 44340 jjdelcerro
                return String.class;
395
            }
396
            return theClass;
397 45185 jjdelcerro
        } catch (Exception ex) {
398
            return String.class;
399 44340 jjdelcerro
        }
400 45185 jjdelcerro
    }
401 44340 jjdelcerro
402 45185 jjdelcerro
    @Override
403
    public boolean isCellEditable(int rowIndex, int columnIndex) {
404
        return false;
405
    }
406 45695 omartinez
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 45185 jjdelcerro
420
    @Override
421
    public Feature get(int position) {
422 46472 jjdelcerro
        if (this.getFeatures() == null) {
423 45185 jjdelcerro
            return null;
424 44340 jjdelcerro
        }
425 46472 jjdelcerro
        Feature feature = this.getFeatures().get(position);
426 45185 jjdelcerro
        return feature;
427
    }
428 44340 jjdelcerro
429 45185 jjdelcerro
    @Override
430 45794 jjdelcerro
    public Feature getFeatureAt(int rowIndex) {
431 46472 jjdelcerro
        if (this.getFeatures() == null || rowIndex<0 ) {
432 45794 jjdelcerro
            return null;
433
        }
434
        try {
435 46472 jjdelcerro
            Feature feature = this.getFeatures().get(rowIndex);
436 45794 jjdelcerro
            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 45185 jjdelcerro
    public Object getValueAt(int rowIndex, int columnIndex) {
446 46472 jjdelcerro
        if (this.getFeatures() == null) {
447 45185 jjdelcerro
            return null;
448 44340 jjdelcerro
        }
449 45185 jjdelcerro
        try {
450 46472 jjdelcerro
            Feature feature = this.getFeatures().get(rowIndex);
451 45185 jjdelcerro
            String attrName = this.columnNames.get(columnIndex);
452
            Object value = null;
453
            value = feature.get(attrName);
454
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
455 46060 omartinez
                        if(attrdesc == null && this.featureType.getExtraColumns().get(attrName)!=null) {
456
                                attrdesc = this.featureType.getExtraColumns().get(attrName);
457
                        }
458 45185 jjdelcerro
            if (attrdesc != null) {
459 45249 omartinez
                if (value == null) {
460
                    return null;
461
                }
462 45185 jjdelcerro
                if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
463
                    value = attrdesc.getForeingKey().getLabelForValue(value);
464 46060 omartinez
                                        }
465 44340 jjdelcerro
                }
466 45185 jjdelcerro
            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 44340 jjdelcerro
        }
472 45185 jjdelcerro
    }
473 44340 jjdelcerro
474 45185 jjdelcerro
    @Override
475
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
476 44340 jjdelcerro
477 45185 jjdelcerro
    }
478 44340 jjdelcerro
479
    @Override
480
    public List<Feature> toList() {
481 46472 jjdelcerro
        return this.getFeatures();
482 44340 jjdelcerro
    }
483
484
    @Override
485
    public boolean isEmpty() {
486 46472 jjdelcerro
        return this.getFeatures().isEmpty();
487 44340 jjdelcerro
    }
488
489
    @Override
490
    public int size() {
491 46472 jjdelcerro
        return this.getFeatures().size();
492 44340 jjdelcerro
    }
493
494
    @Override
495
    public Iterator<Feature> iterator() {
496 46472 jjdelcerro
        return this.getFeatures().iterator();
497 44340 jjdelcerro
    }
498 45100 jjdelcerro
499 44340 jjdelcerro
}