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 / FeatureTable.java @ 45968
History | View | Annotate | Download (9.38 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 42775 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40435 | jjdelcerro | *
|
11 | 42775 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40435 | jjdelcerro | *
|
16 | 42775 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40435 | jjdelcerro | *
|
20 | 42775 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | 42775 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.featuretable; |
24 | 40435 | jjdelcerro | |
25 | import java.awt.Color; |
||
26 | import java.awt.Component; |
||
27 | 42775 | jjdelcerro | import java.awt.event.ActionEvent; |
28 | import java.awt.event.ActionListener; |
||
29 | 40435 | jjdelcerro | import java.sql.Timestamp; |
30 | 41699 | jjdelcerro | import java.util.Date; |
31 | 42775 | jjdelcerro | import java.util.HashSet; |
32 | import java.util.Set; |
||
33 | 40435 | jjdelcerro | |
34 | import javax.swing.JTable; |
||
35 | import javax.swing.event.ChangeEvent; |
||
36 | import javax.swing.event.TableModelEvent; |
||
37 | import javax.swing.table.TableCellRenderer; |
||
38 | import javax.swing.table.TableColumn; |
||
39 | import javax.swing.table.TableColumnModel; |
||
40 | import javax.swing.table.TableModel; |
||
41 | 42806 | fdiaz | |
42 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
43 | 42807 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSelection; |
44 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
45 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
46 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.swing.FeatureTableModel; |
47 | import org.gvsig.fmap.dal.swing.impl.featuretable.table.DefaultFeatureTableModel; |
||
48 | 42806 | fdiaz | import org.gvsig.fmap.dal.swing.impl.featuretable.table.editors.FormattedCellEditor; |
49 | import org.gvsig.fmap.dal.swing.impl.featuretable.table.editors.GeometryWKTCellEditor; |
||
50 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.swing.impl.featuretable.table.renders.FeatureCellRenderer; |
51 | import org.gvsig.fmap.dal.swing.impl.featuretable.table.renders.FormattedCellRenderer; |
||
52 | 42806 | fdiaz | import org.gvsig.fmap.dal.swing.impl.featuretable.table.renders.GeometryWKTCellRenderer; |
53 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.swing.impl.featuretable.table.renders.JToggleButtonHeaderCellRenderer; |
54 | 42806 | fdiaz | import org.gvsig.fmap.geom.Geometry; |
55 | 40435 | jjdelcerro | import org.gvsig.tools.observer.Observable; |
56 | import org.gvsig.tools.observer.Observer; |
||
57 | |||
58 | public class FeatureTable extends JTable implements Observer, Observable { |
||
59 | |||
60 | private static final long serialVersionUID = -6139395189283163964L; |
||
61 | 41821 | jjdelcerro | |
62 | 40435 | jjdelcerro | private final FeatureTableModel featureTableModel; |
63 | private JToggleButtonHeaderCellRenderer headerCellRenderer;
|
||
64 | 42775 | jjdelcerro | private final Set<ActionListener> columnSelectionListeners; |
65 | private static final int COLUMN_HEADER_MARGIN = 8; |
||
66 | 40435 | jjdelcerro | |
67 | 42775 | jjdelcerro | private static final int COLUMN_HEADER_MIN_WIDTH = 50; |
68 | 40435 | jjdelcerro | |
69 | 42775 | jjdelcerro | public FeatureTable(FeatureTableModel featureTableModel) {
|
70 | 40435 | jjdelcerro | super(featureTableModel);
|
71 | this.featureTableModel = featureTableModel;
|
||
72 | 42775 | jjdelcerro | this.columnSelectionListeners = new HashSet<>(); |
73 | 40435 | jjdelcerro | init(); |
74 | } |
||
75 | |||
76 | 42775 | jjdelcerro | public void addColumnSelectionListener(ActionListener listener) { |
77 | this.columnSelectionListeners.add(listener);
|
||
78 | 40435 | jjdelcerro | } |
79 | 42806 | fdiaz | |
80 | 42775 | jjdelcerro | public void fireColumnSelection(ActionEvent e) { |
81 | for( ActionListener listener : this.columnSelectionListeners ) { |
||
82 | try {
|
||
83 | listener.actionPerformed(e); |
||
84 | } catch(Exception ex) { |
||
85 | 42806 | fdiaz | |
86 | 42775 | jjdelcerro | } |
87 | } |
||
88 | } |
||
89 | 40435 | jjdelcerro | |
90 | 42775 | jjdelcerro | @Override
|
91 | 40435 | jjdelcerro | public void update(Observable observable, Object notification) { |
92 | 42807 | jjdelcerro | if( observable instanceof FeatureSelection ) { |
93 | // Ha cambiado la seleccion, simplemente repintamos.
|
||
94 | repaint(); |
||
95 | return;
|
||
96 | } |
||
97 | if( observable instanceof FeatureStore ) { |
||
98 | 40435 | jjdelcerro | FeatureStoreNotification fsNotification = (FeatureStoreNotification) notification; |
99 | String type = fsNotification.getType();
|
||
100 | /*
|
||
101 | * This is necessary to let Swing know
|
||
102 | * that editing (in terms of Swing, not gvsig editing)
|
||
103 | * must be cancelled because the deleted row
|
||
104 | * is perhaps the row that was being edited
|
||
105 | */
|
||
106 | if (FeatureStoreNotification.BEFORE_DELETE.equals(type)) {
|
||
107 | if (this.isEditing()) { |
||
108 | ChangeEvent che = new ChangeEvent(this); |
||
109 | this.editingCanceled(che);
|
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | 42775 | jjdelcerro | @Override
|
116 | 40435 | jjdelcerro | public void addObserver(Observer observer) { |
117 | headerCellRenderer.addObserver(observer); |
||
118 | } |
||
119 | |||
120 | 42775 | jjdelcerro | @Override
|
121 | 40435 | jjdelcerro | public void deleteObserver(Observer observer) { |
122 | headerCellRenderer.deleteObserver(observer); |
||
123 | } |
||
124 | |||
125 | 42775 | jjdelcerro | @Override
|
126 | 40435 | jjdelcerro | public void deleteObservers() { |
127 | headerCellRenderer.deleteObservers(); |
||
128 | } |
||
129 | |||
130 | 42775 | jjdelcerro | // public void setSelectionUp(boolean selectionUp) {
|
131 | // ((FeatureTableModel) getModel()).setSelectionUp(selectionUp);
|
||
132 | // scrollRectToVisible(getCellRect(0, 0, true));
|
||
133 | // }
|
||
134 | 40435 | jjdelcerro | @Override
|
135 | protected void initializeLocalVars() { |
||
136 | super.initializeLocalVars();
|
||
137 | // Add a cell renderer for Geometries and Features
|
||
138 | setDefaultRenderer(Geometry.class, new GeometryWKTCellRenderer());
|
||
139 | setDefaultEditor(Geometry.class, new GeometryWKTCellEditor());
|
||
140 | setDefaultRenderer(Feature.class, new FeatureCellRenderer());
|
||
141 | |||
142 | 42775 | jjdelcerro | if (this.getModel() instanceof DefaultFeatureTableModel) { |
143 | DefaultFeatureTableModel model = (DefaultFeatureTableModel) this.getModel();
|
||
144 | 41699 | jjdelcerro | setDefaultRenderer(Double.class, new FormattedCellRenderer(model)); |
145 | setDefaultRenderer(Float.class, new FormattedCellRenderer(model)); |
||
146 | setDefaultRenderer(Integer.class, new FormattedCellRenderer(model)); |
||
147 | setDefaultRenderer(Long.class, new FormattedCellRenderer(model)); |
||
148 | setDefaultRenderer(Date.class, new FormattedCellRenderer(model)); |
||
149 | 41704 | fdiaz | setDefaultEditor(Double.class, new FormattedCellEditor(model)); |
150 | setDefaultEditor(Float.class, new FormattedCellEditor(model)); |
||
151 | setDefaultEditor(Integer.class, new FormattedCellEditor(model)); |
||
152 | setDefaultEditor(Long.class, new FormattedCellEditor(model)); |
||
153 | setDefaultEditor(Date.class, new FormattedCellEditor(model)); |
||
154 | 41699 | jjdelcerro | } |
155 | |||
156 | 40435 | jjdelcerro | // Set the selected row colors
|
157 | setSelectionForeground(Color.blue);
|
||
158 | setSelectionBackground(Color.yellow);
|
||
159 | } |
||
160 | |||
161 | /**
|
||
162 | * Initializes the table GUI.
|
||
163 | */
|
||
164 | 42775 | jjdelcerro | private void init() { |
165 | setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
||
166 | 40435 | jjdelcerro | |
167 | featureTableModel.getFeatureStore().addObserver(this);
|
||
168 | 42807 | jjdelcerro | this.featureTableModel.getFeatureSelection().addObserver(this); |
169 | this.featureTableModel.addChangeListener(new ActionListener() { |
||
170 | |||
171 | @Override
|
||
172 | public void actionPerformed(ActionEvent e) { |
||
173 | if( FeatureTableModel.CHANGE_SELECTION.equals(e.getActionCommand()) ) {
|
||
174 | featureSelectionChanged(); |
||
175 | } |
||
176 | } |
||
177 | }); |
||
178 | 40435 | jjdelcerro | // Change the selection model to link with the FeatureStore selection
|
179 | // through the FeatureTableModel
|
||
180 | setRowSelectionAllowed(true);
|
||
181 | setColumnSelectionAllowed(false);
|
||
182 | 42775 | jjdelcerro | setSelectionModel(new FeatureSelectionModel(featureTableModel));
|
183 | 40435 | jjdelcerro | |
184 | 42775 | jjdelcerro | headerCellRenderer = new JToggleButtonHeaderCellRenderer(this); |
185 | getTableHeader().setDefaultRenderer(headerCellRenderer); |
||
186 | 40435 | jjdelcerro | |
187 | 42775 | jjdelcerro | TableColumnModel tcmodel = getColumnModel();
|
188 | for (int i = 0; i < tcmodel.getColumnCount(); i++) { |
||
189 | TableColumn col = tcmodel.getColumn(i);
|
||
190 | // Get width of column header
|
||
191 | TableCellRenderer renderer = col.getHeaderRenderer();
|
||
192 | if (renderer == null) { |
||
193 | renderer = getTableHeader().getDefaultRenderer(); |
||
194 | } |
||
195 | Component comp
|
||
196 | = renderer.getTableCellRendererComponent(this,
|
||
197 | col.getHeaderValue(), false, false, 0, i); |
||
198 | int width = comp.getPreferredSize().width;
|
||
199 | width |
||
200 | = width < COLUMN_HEADER_MIN_WIDTH ? COLUMN_HEADER_MIN_WIDTH |
||
201 | : width; |
||
202 | col.setPreferredWidth(width + 2 * COLUMN_HEADER_MARGIN);
|
||
203 | } |
||
204 | this.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); |
||
205 | 40435 | jjdelcerro | } |
206 | 42806 | fdiaz | |
207 | 42807 | jjdelcerro | private void featureSelectionChanged() { |
208 | this.featureTableModel.getFeatureSelection().addObserver(this); |
||
209 | } |
||
210 | |||
211 | 42775 | jjdelcerro | @Override
|
212 | public void setModel(TableModel dataModel) { |
||
213 | super.setModel(dataModel);
|
||
214 | } |
||
215 | 42806 | fdiaz | |
216 | 42775 | jjdelcerro | @Override
|
217 | 40435 | jjdelcerro | public int getSelectedColumnCount() { |
218 | return headerCellRenderer.getSelectedColumns().length;
|
||
219 | } |
||
220 | 41704 | fdiaz | |
221 | 42775 | jjdelcerro | @Override
|
222 | public int[] getSelectedColumns() { |
||
223 | return headerCellRenderer.getSelectedColumns();
|
||
224 | } |
||
225 | 42806 | fdiaz | |
226 | 42775 | jjdelcerro | @Override
|
227 | 40435 | jjdelcerro | public void tableChanged(TableModelEvent e) { |
228 | // Clear the header selection
|
||
229 | if (e != null && e.getFirstRow() == TableModelEvent.HEADER_ROW |
||
230 | 42775 | jjdelcerro | && headerCellRenderer != null) {
|
231 | 40435 | jjdelcerro | headerCellRenderer.deselectAll(); |
232 | } |
||
233 | 41704 | fdiaz | |
234 | 40435 | jjdelcerro | super.tableChanged(e);
|
235 | } |
||
236 | 41704 | fdiaz | |
237 | 42775 | jjdelcerro | @Override
|
238 | 40435 | jjdelcerro | public Class<?> getColumnClass(int column) { |
239 | Class resp = super.getColumnClass(column); |
||
240 | if (Timestamp.class.isAssignableFrom(resp)) { |
||
241 | return Object.class; |
||
242 | } else {
|
||
243 | return resp;
|
||
244 | } |
||
245 | } |
||
246 | 41704 | fdiaz | |
247 | 41821 | jjdelcerro | @Override
|
248 | public int getSelectedRowCount() { |
||
249 | 42775 | jjdelcerro | return this.featureTableModel.getSelectionCount(); |
250 | 41821 | jjdelcerro | } |
251 | 42775 | jjdelcerro | } |