Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / data / feature / swing / FeatureTable.java @ 25838

History | View | Annotate | Download (5.37 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {Create a Table component for Features}
26
 */
27
package org.gvsig.fmap.data.feature.swing;
28

    
29
import java.awt.Color;
30

    
31
import javax.swing.JTable;
32
import javax.swing.table.TableColumnModel;
33
import javax.swing.table.TableModel;
34

    
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
39
import org.gvsig.fmap.data.feature.swing.table.*;
40
import org.gvsig.fmap.data.feature.swing.table.notification.ColumnHeaderSelectionChangeNotification;
41
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.tools.observer.Observable;
43
import org.gvsig.tools.observer.Observer;
44

    
45
/**
46
 * Table extension to show Feature values.
47
 * 
48
 * It's based on the usage of a FeatureTableModel, and adds renderers for
49
 * Geometry and Feature cell values.
50
 * 
51
 * Observers are notified about column header selection changes, with a
52
 * {@link ColumnHeaderSelectionChangeNotification}.
53
 * 
54
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
55
 */
56
public class FeatureTable extends JTable implements Observer, Observable {
57

    
58
    /**
59
     * Generated Serial UID
60
     */
61
    private static final long serialVersionUID = -6139395189283163964L;
62
    private final FeatureTableModel featureTableModel;
63
    private JToggleButtonHeaderCellRenderer headerCellRenderer;
64

    
65
    /**
66
     * @throws DataException
67
     * @see JTable#JTable(TableModel)
68
     */
69
    public FeatureTable(FeatureTableModel featureTableModel)
70
            throws DataException {
71
        super(featureTableModel);
72
        this.featureTableModel = featureTableModel;
73
        init();
74
    }
75

    
76
    /**
77
     * @throws DataException
78
     * @see JTable#JTable(TableModel, TableColumnModel)
79
     */
80
    public FeatureTable(FeatureTableModel featureTableModel, TableColumnModel cm)
81
            throws DataException {
82
        super(featureTableModel, cm);
83
        this.featureTableModel = featureTableModel;
84
        init();
85
    }
86

    
87
    public void update(Observable observable, Object notification) {
88
        if (notification instanceof FeatureStoreNotification) {
89
            FeatureStoreNotification fsNotification = (FeatureStoreNotification) notification;
90
            String type = fsNotification.getType();
91
            // If the selection has changed, repaint the table to show the new
92
            // selected rows
93
            if (FeatureStoreNotification.SELECTION_CHANGE.equals(type)) {
94
                repaint();
95
            }
96
        }
97
    }
98

    
99
    /**
100
     * Returns the FeatureAttributeDescriptor related to the selected columns.
101
     * 
102
     * @return an array of FeatureAttributeDescriptor
103
     * 
104
     * @see org.gvsig.fmap.data.feature.swing.table.JToggleButtonHeaderCellRenderer#getSelectedColumns()
105
     */
106
    public FeatureAttributeDescriptor[] getSelectedColumnsAttributeDescriptor() {
107
        int[] columns = headerCellRenderer.getSelectedColumns();
108
        FeatureAttributeDescriptor[] descriptors = new FeatureAttributeDescriptor[columns.length];
109

    
110
        for (int i = 0; i < descriptors.length; i++) {
111
            descriptors[i] = featureTableModel
112
                    .getDescriptorForColumn(columns[i]);
113
        }
114
        
115
        return descriptors;
116
    }
117

    
118
    @Override
119
    protected void initializeLocalVars() {
120
        super.initializeLocalVars();
121
        // Add a cell renderer for Geometries and Features
122
        setDefaultRenderer(Geometry.class, new GeometryCellRenderer());
123
        setDefaultRenderer(Feature.class, new FeatureCellRenderer());
124

    
125
        // Set the selected row colors
126
        setSelectionForeground(Color.blue);
127
        setSelectionBackground(Color.yellow);
128
    }
129

    
130
    private void init() throws DataException {
131
        featureTableModel.getFeatureStore().addObserver(this);
132
        // Change the selection model to link with the FeatureStore selection
133
        // through the FeatureTableModel
134
        setRowSelectionAllowed(true);
135
        setColumnSelectionAllowed(false);
136
        setSelectionModel(new FeatureSelectionModel(featureTableModel));
137

    
138
        headerCellRenderer = new JToggleButtonHeaderCellRenderer(this);
139
        getTableHeader().setDefaultRenderer(headerCellRenderer);
140
    }
141

    
142
    public void addObserver(Observer observer) {
143
        headerCellRenderer.addObserver(observer);
144
    }
145

    
146
    public void deleteObserver(Observer observer) {
147
        headerCellRenderer.deleteObserver(observer);
148
    }
149

    
150
    public void deleteObservers() {
151
        headerCellRenderer.deleteObservers();
152
    }
153
}