root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / GeometryWKTCellEditor.java @ 38564
History | View | Annotate | Download (7.28 KB)
1 | 25894 | cordinyana | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | 31544 | cordinyana | * of the Valencian Government (CIT)
|
5 | 25894 | cordinyana | *
|
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} {{Task}}
|
||
26 | */
|
||
27 | 31616 | cordinyana | package org.gvsig.fmap.mapcontrol.dal.feature.swing.table; |
28 | 25894 | cordinyana | |
29 | 25909 | cordinyana | import java.awt.Component; |
30 | 25894 | cordinyana | import java.awt.event.ActionEvent; |
31 | import java.awt.event.ItemEvent; |
||
32 | import java.util.EventObject; |
||
33 | |||
34 | import javax.swing.DefaultCellEditor; |
||
35 | 25909 | cordinyana | import javax.swing.JOptionPane; |
36 | import javax.swing.JTable; |
||
37 | 25894 | cordinyana | |
38 | import org.gvsig.fmap.geom.Geometry; |
||
39 | 38564 | jjdelcerro | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
40 | 27411 | jpiera | import org.gvsig.fmap.geom.GeometryLocator; |
41 | import org.gvsig.fmap.geom.GeometryManager; |
||
42 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
43 | 25894 | cordinyana | import org.gvsig.fmap.geom.operation.fromwkt.FromWKT; |
44 | import org.gvsig.fmap.geom.operation.fromwkt.FromWKTGeometryOperationContext; |
||
45 | import org.gvsig.fmap.geom.operation.towkt.ToWKT; |
||
46 | 26030 | cordinyana | import org.gvsig.i18n.Messages; |
47 | 27411 | jpiera | import org.slf4j.Logger; |
48 | import org.slf4j.LoggerFactory; |
||
49 | 25894 | cordinyana | |
50 | /**
|
||
51 | 25909 | cordinyana | * Editor for cells of type Geometry in WKT format.
|
52 | * <p>
|
||
53 | * If the WKT to represent a Geometry is too big, editing is not allowed, as the
|
||
54 | * rendering of that big text is too slow.
|
||
55 | * </p>
|
||
56 | 25894 | cordinyana | *
|
57 | * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
||
58 | */
|
||
59 | public class GeometryWKTCellEditor extends TextAreaCellEditor { |
||
60 | 27411 | jpiera | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
61 | private static final Logger logger = LoggerFactory.getLogger(GeometryWKTCellEditor.class); |
||
62 | 25909 | cordinyana | public static final int DEFAULT_MAX_WKT_LENGTH = 10000; |
63 | 25894 | cordinyana | |
64 | private static final long serialVersionUID = -2296004227902843851L; |
||
65 | 27411 | jpiera | private Geometry nullGeometry = null; |
66 | 27032 | jpiera | |
67 | 25916 | cordinyana | private int maxRowHeight; |
68 | |||
69 | 25909 | cordinyana | /**
|
70 | * Creates a new editor for Geometries in WKT format.
|
||
71 | */
|
||
72 | 25894 | cordinyana | public GeometryWKTCellEditor() {
|
73 | 25916 | cordinyana | this(DEFAULT_MAX_WKT_LENGTH, 160); |
74 | 25909 | cordinyana | } |
75 | |||
76 | /**
|
||
77 | * Creates a new editor for Geometries in WKT format.
|
||
78 | *
|
||
79 | * @param maxWKTLength
|
||
80 | * the maximum WTK length allowed to be edited.
|
||
81 | 25916 | cordinyana | * @param maxRowHeight
|
82 | * the maximum row height for the rows with cells rendered with
|
||
83 | * this component
|
||
84 | 25909 | cordinyana | */
|
85 | 25916 | cordinyana | public GeometryWKTCellEditor(int maxWKTLength, int maxRowHeight) { |
86 | 25894 | cordinyana | super();
|
87 | 25909 | cordinyana | delegate = new GeometryToWKTDelegate(delegate, maxWKTLength);
|
88 | 25916 | cordinyana | this.maxRowHeight = maxRowHeight;
|
89 | 27411 | jpiera | try {
|
90 | nullGeometry = geomManager.createNullGeometry(SUBTYPES.GEOM2D); |
||
91 | } catch (CreateGeometryException e) {
|
||
92 | logger.error("Error creating a null geometry", e);
|
||
93 | } |
||
94 | 25894 | cordinyana | } |
95 | |||
96 | @Override
|
||
97 | public Object getCellEditorValue() { |
||
98 | String wkt = (String) super.getCellEditorValue(); |
||
99 | FromWKTGeometryOperationContext context = new FromWKTGeometryOperationContext(
|
||
100 | wkt, null);
|
||
101 | |||
102 | try {
|
||
103 | return nullGeometry.invokeOperation(FromWKT.CODE, context);
|
||
104 | } catch (Exception ex) { |
||
105 | throw new WKTToGeometryException(wkt, ex); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | 25909 | cordinyana | @Override
|
110 | public Component getTableCellEditorComponent(JTable table, Object value, |
||
111 | boolean isSelected, int row, int column) { |
||
112 | delegate.setValue(value); |
||
113 | if (((GeometryToWKTDelegate) delegate).isWtkTextTooLong()) {
|
||
114 | JOptionPane
|
||
115 | .showMessageDialog( |
||
116 | table.getParent(), |
||
117 | 26030 | cordinyana | Messages |
118 | .getText("Geometria_no_editable_WKT"), Messages
|
||
119 | .getText("Error_editar_geometria"),
|
||
120 | 25909 | cordinyana | JOptionPane.ERROR_MESSAGE);
|
121 | |||
122 | return null; |
||
123 | } else {
|
||
124 | 25916 | cordinyana | int height_wanted = (int) getTextArea().getPreferredSize() |
125 | .getHeight(); |
||
126 | |||
127 | height_wanted = height_wanted > maxRowHeight ? maxRowHeight |
||
128 | : height_wanted; |
||
129 | |||
130 | if (height_wanted > table.getRowHeight(row)) {
|
||
131 | table.setRowHeight(row, height_wanted); |
||
132 | } |
||
133 | |||
134 | 25909 | cordinyana | return editorComponent;
|
135 | } |
||
136 | } |
||
137 | |||
138 | 25894 | cordinyana | @SuppressWarnings("serial") |
139 | private class GeometryToWKTDelegate extends |
||
140 | DefaultCellEditor.EditorDelegate {
|
||
141 | private DefaultCellEditor.EditorDelegate delegate; |
||
142 | |||
143 | 25909 | cordinyana | private boolean wtkTextTooLong = false; |
144 | 25916 | cordinyana | |
145 | 25909 | cordinyana | private int maxWKTLength; |
146 | |||
147 | public GeometryToWKTDelegate(DefaultCellEditor.EditorDelegate delegate, |
||
148 | int maxWKTLength) {
|
||
149 | 25894 | cordinyana | this.delegate = delegate;
|
150 | 25909 | cordinyana | this.maxWKTLength = maxWKTLength;
|
151 | 25894 | cordinyana | } |
152 | |||
153 | 25909 | cordinyana | /**
|
154 | * @return the wtkTextTooLong
|
||
155 | */
|
||
156 | public boolean isWtkTextTooLong() { |
||
157 | return wtkTextTooLong;
|
||
158 | } |
||
159 | |||
160 | 25894 | cordinyana | public void setValue(Object value) { |
161 | 25909 | cordinyana | wtkTextTooLong = false;
|
162 | 25894 | cordinyana | String strValue = ""; |
163 | |||
164 | if (value != null) { |
||
165 | try {
|
||
166 | Geometry geometry = (Geometry) value; |
||
167 | strValue = (String) geometry.invokeOperation(ToWKT.CODE,
|
||
168 | null);
|
||
169 | 25909 | cordinyana | |
170 | if (strValue.length() > maxWKTLength) {
|
||
171 | wtkTextTooLong = true;
|
||
172 | delegate.setValue(null);
|
||
173 | } else {
|
||
174 | delegate.setValue(strValue); |
||
175 | } |
||
176 | 25894 | cordinyana | } catch (Exception ex) { |
177 | throw new GeometryToWKTException(ex); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | 25909 | cordinyana | public Object getCellEditorValue() { |
183 | if (wtkTextTooLong) {
|
||
184 | return null; |
||
185 | } else {
|
||
186 | return delegate.getCellEditorValue();
|
||
187 | } |
||
188 | } |
||
189 | |||
190 | 25894 | cordinyana | public void actionPerformed(ActionEvent e) { |
191 | delegate.actionPerformed(e); |
||
192 | } |
||
193 | |||
194 | public void cancelCellEditing() { |
||
195 | delegate.cancelCellEditing(); |
||
196 | } |
||
197 | |||
198 | public boolean equals(Object obj) { |
||
199 | return delegate.equals(obj);
|
||
200 | } |
||
201 | |||
202 | public int hashCode() { |
||
203 | return delegate.hashCode();
|
||
204 | } |
||
205 | |||
206 | public boolean isCellEditable(EventObject anEvent) { |
||
207 | return delegate.isCellEditable(anEvent);
|
||
208 | } |
||
209 | |||
210 | public void itemStateChanged(ItemEvent e) { |
||
211 | delegate.itemStateChanged(e); |
||
212 | } |
||
213 | |||
214 | public boolean shouldSelectCell(EventObject anEvent) { |
||
215 | return delegate.shouldSelectCell(anEvent);
|
||
216 | } |
||
217 | |||
218 | public boolean startCellEditing(EventObject anEvent) { |
||
219 | return delegate.startCellEditing(anEvent);
|
||
220 | } |
||
221 | |||
222 | public boolean stopCellEditing() { |
||
223 | return delegate.stopCellEditing();
|
||
224 | } |
||
225 | |||
226 | public String toString() { |
||
227 | return delegate.toString();
|
||
228 | } |
||
229 | } |
||
230 | } |