svn-gvsig-desktop / tags / v2_0_0_Build_2038 / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / FeatureTable.java @ 36906
History | View | Annotate | Download (8.29 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.event.TableModelEvent; |
34 |
import javax.swing.table.TableCellRenderer; |
35 |
import javax.swing.table.TableColumn; |
36 |
import javax.swing.table.TableColumnModel; |
37 |
import javax.swing.table.TableModel; |
38 |
|
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.feature.Feature; |
41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
43 |
import org.gvsig.fmap.geom.Geometry; |
44 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureCellRenderer; |
45 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTableModel; |
46 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.GeometryWKTCellEditor; |
47 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.GeometryWKTCellRenderer; |
48 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.JToggleButtonHeaderCellRenderer; |
49 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.notification.ColumnHeaderSelectionChangeNotification; |
50 |
import org.gvsig.tools.observer.Observable; |
51 |
import org.gvsig.tools.observer.Observer; |
52 |
|
53 |
/**
|
54 |
* Table extension to show Feature values.
|
55 |
*
|
56 |
* It's based on the usage of a FeatureTableModel, and adds renderers for
|
57 |
* Geometry and Feature cell values.
|
58 |
*
|
59 |
* Observers are notified about column header selection changes, with a
|
60 |
* {@link ColumnHeaderSelectionChangeNotification}.
|
61 |
*
|
62 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
63 |
*/
|
64 |
public class FeatureTable extends JTable implements Observer, Observable { |
65 |
|
66 |
/**
|
67 |
* Generated Serial UID
|
68 |
*/
|
69 |
private static final long serialVersionUID = -6139395189283163964L; |
70 |
private final FeatureTableModel featureTableModel; |
71 |
private JToggleButtonHeaderCellRenderer headerCellRenderer;
|
72 |
|
73 |
private static final int COLUMN_HEADER_MARGIN = 8; |
74 |
|
75 |
private static final int COLUMN_HEADER_MIN_WIDTH = 50; |
76 |
|
77 |
/**
|
78 |
* Creates a new FeatureTable with a {@link FeatureTableModel}.
|
79 |
*
|
80 |
* @param featureTableModel
|
81 |
* the table model to get data to be shown on the table
|
82 |
* @throws DataException
|
83 |
* if there is an error while loading the Features to show
|
84 |
* @see JTable#JTable(TableModel)
|
85 |
*/
|
86 |
public FeatureTable(FeatureTableModel featureTableModel)
|
87 |
throws DataException {
|
88 |
super(featureTableModel);
|
89 |
this.featureTableModel = featureTableModel;
|
90 |
init(); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* Creates a new FeatureTable with a {@link FeatureTableModel}.
|
95 |
*
|
96 |
* @param featureTableModel
|
97 |
* the table model to get data to be shown on the table
|
98 |
* @param cm
|
99 |
* the table column model to use
|
100 |
* @throws DataException
|
101 |
* if there is an error while loading the Features to show
|
102 |
* @see JTable#JTable(TableModel, TableColumnModel)
|
103 |
*/
|
104 |
public FeatureTable(FeatureTableModel featureTableModel, TableColumnModel cm) |
105 |
throws DataException {
|
106 |
super(featureTableModel, cm);
|
107 |
this.featureTableModel = featureTableModel;
|
108 |
init(); |
109 |
} |
110 |
|
111 |
public void update(Observable observable, Object notification) { |
112 |
if (notification instanceof FeatureStoreNotification) { |
113 |
FeatureStoreNotification fsNotification = (FeatureStoreNotification) notification; |
114 |
String type = fsNotification.getType();
|
115 |
// If the selection has changed, repaint the table to show the new
|
116 |
// selected rows
|
117 |
if (FeatureStoreNotification.SELECTION_CHANGE.equals(type)) {
|
118 |
repaint(); |
119 |
} |
120 |
} |
121 |
} |
122 |
|
123 |
/**
|
124 |
* Returns the FeatureAttributeDescriptor related to the selected columns.
|
125 |
*
|
126 |
* @return an array of FeatureAttributeDescriptor
|
127 |
*
|
128 |
* @see org.gvsig.fmap.mapcontrol.dal.feature.swing.table.JToggleButtonHeaderCellRenderer#getSelectedColumns()
|
129 |
*/
|
130 |
public FeatureAttributeDescriptor[] getSelectedColumnsAttributeDescriptor() { |
131 |
int[] columns = headerCellRenderer.getSelectedColumns(); |
132 |
FeatureAttributeDescriptor[] descriptors = new FeatureAttributeDescriptor[columns.length]; |
133 |
|
134 |
for (int i = 0; i < descriptors.length; i++) { |
135 |
descriptors[i] = featureTableModel |
136 |
.getDescriptorForColumn(columns[i]); |
137 |
} |
138 |
|
139 |
return descriptors;
|
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 |
|
154 |
/**
|
155 |
* Sets that the selected Features to be viewed first.
|
156 |
*/
|
157 |
public void setSelectionUp(boolean selectionUp) { |
158 |
((FeatureTableModel) getModel()).setSelectionUp(selectionUp); |
159 |
scrollRectToVisible(getCellRect(0, 0, true)); |
160 |
} |
161 |
|
162 |
// @Override
|
163 |
// public void tableChanged(TableModelEvent e) {
|
164 |
// super.tableChanged(e);
|
165 |
// if (headerCellRenderer != null) {
|
166 |
// headerCellRenderer.deselectAll();
|
167 |
// }
|
168 |
// }
|
169 |
|
170 |
@Override
|
171 |
protected void initializeLocalVars() { |
172 |
super.initializeLocalVars();
|
173 |
// Add a cell renderer for Geometries and Features
|
174 |
setDefaultRenderer(Geometry.class, new GeometryWKTCellRenderer());
|
175 |
setDefaultEditor(Geometry.class, new GeometryWKTCellEditor());
|
176 |
setDefaultRenderer(Feature.class, new FeatureCellRenderer());
|
177 |
|
178 |
// Set the selected row colors
|
179 |
setSelectionForeground(Color.blue);
|
180 |
setSelectionBackground(Color.yellow);
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Initializes the table GUI.
|
185 |
*/
|
186 |
private void init() throws DataException { |
187 |
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
188 |
|
189 |
featureTableModel.getFeatureStore().addObserver(this);
|
190 |
// Change the selection model to link with the FeatureStore selection
|
191 |
// through the FeatureTableModel
|
192 |
setRowSelectionAllowed(true);
|
193 |
setColumnSelectionAllowed(false);
|
194 |
setSelectionModel(new FeatureSelectionModel(featureTableModel));
|
195 |
|
196 |
headerCellRenderer = new JToggleButtonHeaderCellRenderer(this); |
197 |
getTableHeader().setDefaultRenderer(headerCellRenderer); |
198 |
|
199 |
TableColumnModel tcmodel = getColumnModel();
|
200 |
for (int i = 0; i < tcmodel.getColumnCount(); i++) { |
201 |
TableColumn col = tcmodel.getColumn(i);
|
202 |
// Get width of column header
|
203 |
TableCellRenderer renderer = col.getHeaderRenderer();
|
204 |
if (renderer == null) { |
205 |
renderer = getTableHeader().getDefaultRenderer(); |
206 |
} |
207 |
Component comp =
|
208 |
renderer.getTableCellRendererComponent(this,
|
209 |
col.getHeaderValue(), false, false, 0, i); |
210 |
int width = comp.getPreferredSize().width;
|
211 |
width = |
212 |
width < COLUMN_HEADER_MIN_WIDTH ? COLUMN_HEADER_MIN_WIDTH |
213 |
: width; |
214 |
col.setPreferredWidth(width + 2 * COLUMN_HEADER_MARGIN);
|
215 |
} |
216 |
} |
217 |
|
218 |
/**
|
219 |
* Returns the number of selected columns.
|
220 |
*
|
221 |
* @return the number of selected columns, 0 if no columns are selected
|
222 |
*/
|
223 |
public int getSelectedColumnCount() { |
224 |
return headerCellRenderer.getSelectedColumns().length;
|
225 |
} |
226 |
|
227 |
public void tableChanged(TableModelEvent e) { |
228 |
// Clear the header selection
|
229 |
if (e != null && e.getFirstRow() == TableModelEvent.HEADER_ROW |
230 |
&& headerCellRenderer != null) {
|
231 |
headerCellRenderer.deselectAll(); |
232 |
} |
233 |
super.tableChanged(e);
|
234 |
} |
235 |
} |