Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / FeatureTable.java @ 33378

History | View | Annotate | Download (8.09 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (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.mapcontrol.dal.feature.swing;
28

    
29
import java.awt.Color;
30
import java.awt.Component;
31

    
32
import javax.swing.JTable;
33
import javax.swing.table.TableCellRenderer;
34
import javax.swing.table.TableColumn;
35
import javax.swing.table.TableColumnModel;
36
import javax.swing.table.TableModel;
37

    
38
import org.gvsig.fmap.dal.exception.DataException;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureCellRenderer;
44
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTableModel;
45
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.GeometryWKTCellEditor;
46
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.GeometryWKTCellRenderer;
47
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.JToggleButtonHeaderCellRenderer;
48
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.notification.ColumnHeaderSelectionChangeNotification;
49
import org.gvsig.tools.observer.Observable;
50
import org.gvsig.tools.observer.Observer;
51

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

    
65
    /**
66
     * Generated Serial UID
67
     */
68
    private static final long serialVersionUID = -6139395189283163964L;
69
    private final FeatureTableModel featureTableModel;
70
    private JToggleButtonHeaderCellRenderer headerCellRenderer;
71

    
72
        private static final int COLUMN_HEADER_MARGIN = 8;
73

    
74
        private static final int COLUMN_HEADER_MIN_WIDTH = 50;
75

    
76
    /**
77
     * Creates a new FeatureTable with a {@link FeatureTableModel}.
78
     *
79
     * @param featureTableModel
80
     *            the table model to get data to be shown on the table
81
     * @throws DataException
82
     *             if there is an error while loading the Features to show
83
     * @see JTable#JTable(TableModel)
84
     */
85
    public FeatureTable(FeatureTableModel featureTableModel)
86
            throws DataException {
87
        super(featureTableModel);
88
        this.featureTableModel = featureTableModel;
89
        init();
90
    }
91

    
92
    /**
93
     * Creates a new FeatureTable with a {@link FeatureTableModel}.
94
     *
95
     * @param featureTableModel
96
     *            the table model to get data to be shown on the table
97
     * @param cm
98
     *            the table column model to use
99
     * @throws DataException
100
     *             if there is an error while loading the Features to show
101
     * @see JTable#JTable(TableModel, TableColumnModel)
102
     */
103
    public FeatureTable(FeatureTableModel featureTableModel, TableColumnModel cm)
104
            throws DataException {
105
        super(featureTableModel, cm);
106
        this.featureTableModel = featureTableModel;
107
        init();
108
    }
109

    
110
    public void update(Observable observable, Object notification) {
111
        if (notification instanceof FeatureStoreNotification) {
112
            FeatureStoreNotification fsNotification = (FeatureStoreNotification) notification;
113
            String type = fsNotification.getType();
114
            // If the selection has changed, repaint the table to show the new
115
            // selected rows
116
            if (FeatureStoreNotification.SELECTION_CHANGE.equals(type)) {
117
                repaint();
118
            }
119
        }
120
    }
121

    
122
    /**
123
     * Returns the FeatureAttributeDescriptor related to the selected columns.
124
     *
125
     * @return an array of FeatureAttributeDescriptor
126
     *
127
     * @see org.gvsig.fmap.mapcontrol.dal.feature.swing.table.JToggleButtonHeaderCellRenderer#getSelectedColumns()
128
     */
129
    public FeatureAttributeDescriptor[] getSelectedColumnsAttributeDescriptor() {
130
        int[] columns = headerCellRenderer.getSelectedColumns();
131
        FeatureAttributeDescriptor[] descriptors = new FeatureAttributeDescriptor[columns.length];
132

    
133
        for (int i = 0; i < descriptors.length; i++) {
134
            descriptors[i] = featureTableModel
135
                    .getDescriptorForColumn(columns[i]);
136
        }
137

    
138
        return descriptors;
139
    }
140

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

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

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

    
153
        /**
154
         * Sets that the selected Features to be viewed first.
155
         */
156
        public void setSelectionUp(boolean selectionUp) {
157
                ((FeatureTableModel) getModel()).setSelectionUp(selectionUp);
158
                scrollRectToVisible(getCellRect(0, 0, true));
159
        }
160

    
161
    // @Override
162
    // public void tableChanged(TableModelEvent e) {
163
    // super.tableChanged(e);
164
    // if (headerCellRenderer != null) {
165
    // headerCellRenderer.deselectAll();
166
    // }
167
    // }
168

    
169
    @Override
170
    protected void initializeLocalVars() {
171
        super.initializeLocalVars();
172
        // Add a cell renderer for Geometries and Features
173
        setDefaultRenderer(Geometry.class, new GeometryWKTCellRenderer());
174
        setDefaultEditor(Geometry.class, new GeometryWKTCellEditor());
175
        setDefaultRenderer(Feature.class, new FeatureCellRenderer());
176

    
177
        // Set the selected row colors
178
        setSelectionForeground(Color.blue);
179
        setSelectionBackground(Color.yellow);
180
    }
181

    
182
    /**
183
     * Initializes the table GUI.
184
     */
185
    private void init() throws DataException {
186
                setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
187

    
188
        featureTableModel.getFeatureStore().addObserver(this);
189
        // Change the selection model to link with the FeatureStore selection
190
        // through the FeatureTableModel
191
        setRowSelectionAllowed(true);
192
        setColumnSelectionAllowed(false);
193
                //setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
194
                setSelectionModel(new FeatureSelectionModelNew(featureTableModel));
195
                // setSelectionModel(new FeatureSelectionModel(featureTableModel));
196

    
197
                headerCellRenderer = new JToggleButtonHeaderCellRenderer(this);
198
                getTableHeader().setDefaultRenderer(headerCellRenderer);
199

    
200
                TableColumnModel tcmodel = getColumnModel();
201
                for (int i = 0; i < tcmodel.getColumnCount(); i++) {
202
                        TableColumn col = tcmodel.getColumn(i);
203
                        // Get width of column header
204
                        TableCellRenderer renderer = col.getHeaderRenderer();
205
                        if (renderer == null) {
206
                                renderer = getTableHeader().getDefaultRenderer();
207
                        }
208
                        Component comp =
209
                                        renderer.getTableCellRendererComponent(this,
210
                                                        col.getHeaderValue(), false, false, 0, i);
211
                        int width = comp.getPreferredSize().width;
212
                        width =
213
                                        width < COLUMN_HEADER_MIN_WIDTH ? COLUMN_HEADER_MIN_WIDTH
214
                                                        : width;
215
                        col.setPreferredWidth(width + 2 * COLUMN_HEADER_MARGIN);
216
                }
217
    }
218

    
219
    /**
220
     * Returns the number of selected columns.
221
     *
222
     * @return the number of selected columns, 0 if no columns are selected
223
     */
224
    public int getSelectedColumnCount() {
225
        return headerCellRenderer.getSelectedColumns().length;
226
    }
227
}