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 / table / editors / GeometryWKTCellEditor.java @ 42775
History | View | Annotate | Download (7.01 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40559 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40559 | jjdelcerro | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | /*
|
||
25 | * AUTHORS (In addition to CIT):
|
||
26 | * 2008 {DiSiD Technologies} {{Task}}
|
||
27 | */
|
||
28 | 42775 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.featuretable.table.editors; |
29 | 40435 | jjdelcerro | |
30 | import java.awt.Component; |
||
31 | import java.awt.event.ActionEvent; |
||
32 | import java.awt.event.ItemEvent; |
||
33 | import java.util.EventObject; |
||
34 | |||
35 | import javax.swing.DefaultCellEditor; |
||
36 | import javax.swing.JOptionPane; |
||
37 | import javax.swing.JTable; |
||
38 | 42692 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
39 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.swing.impl.featuretable.table.GeometryToWKTException; |
40 | 40435 | jjdelcerro | |
41 | import org.gvsig.fmap.geom.Geometry; |
||
42 | import org.gvsig.fmap.geom.GeometryLocator; |
||
43 | import org.gvsig.fmap.geom.GeometryManager; |
||
44 | import org.gvsig.i18n.Messages; |
||
45 | import org.slf4j.Logger; |
||
46 | import org.slf4j.LoggerFactory; |
||
47 | |||
48 | /**
|
||
49 | * Editor for cells of type Geometry in WKT format.
|
||
50 | * <p>
|
||
51 | * If the WKT to represent a Geometry is too big, editing is not allowed, as the
|
||
52 | * rendering of that big text is too slow.
|
||
53 | * </p>
|
||
54 | *
|
||
55 | * @author <a href="mailto:cordin@disid.com">C�sar Ordi�ana</a>
|
||
56 | */
|
||
57 | public class GeometryWKTCellEditor extends TextAreaCellEditor { |
||
58 | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
59 | private static final Logger logger = LoggerFactory.getLogger(GeometryWKTCellEditor.class); |
||
60 | public static final int DEFAULT_MAX_WKT_LENGTH = 10000; |
||
61 | |||
62 | private static final long serialVersionUID = -2296004227902843851L; |
||
63 | |||
64 | private int maxRowHeight; |
||
65 | |||
66 | /**
|
||
67 | * Creates a new editor for Geometries in WKT format.
|
||
68 | */
|
||
69 | public GeometryWKTCellEditor() {
|
||
70 | this(DEFAULT_MAX_WKT_LENGTH, 160); |
||
71 | } |
||
72 | |||
73 | /**
|
||
74 | * Creates a new editor for Geometries in WKT format.
|
||
75 | *
|
||
76 | * @param maxWKTLength
|
||
77 | * the maximum WTK length allowed to be edited.
|
||
78 | * @param maxRowHeight
|
||
79 | * the maximum row height for the rows with cells rendered with
|
||
80 | * this component
|
||
81 | */
|
||
82 | public GeometryWKTCellEditor(int maxWKTLength, int maxRowHeight) { |
||
83 | super();
|
||
84 | delegate = new GeometryToWKTDelegate(delegate, maxWKTLength);
|
||
85 | this.maxRowHeight = maxRowHeight;
|
||
86 | } |
||
87 | |||
88 | @Override
|
||
89 | public Object getCellEditorValue() { |
||
90 | String wkt = (String) super.getCellEditorValue(); |
||
91 | try {
|
||
92 | 42692 | jjdelcerro | if( wkt==null || StringUtils.isEmpty(wkt.trim()) ) { |
93 | return null; |
||
94 | } |
||
95 | return geomManager.createFrom(wkt);
|
||
96 | 40435 | jjdelcerro | } catch (Exception ex) { |
97 | throw new WKTToGeometryException(wkt, ex); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | @Override
|
||
102 | public Component getTableCellEditorComponent(JTable table, Object value, |
||
103 | boolean isSelected, int row, int column) { |
||
104 | delegate.setValue(value); |
||
105 | if (((GeometryToWKTDelegate) delegate).isWtkTextTooLong()) {
|
||
106 | JOptionPane
|
||
107 | .showMessageDialog( |
||
108 | table.getParent(), |
||
109 | Messages |
||
110 | .getText("Geometria_no_editable_WKT"), Messages
|
||
111 | .getText("Error_editar_geometria"),
|
||
112 | JOptionPane.ERROR_MESSAGE);
|
||
113 | |||
114 | return null; |
||
115 | } else {
|
||
116 | int height_wanted = (int) getTextArea().getPreferredSize() |
||
117 | .getHeight(); |
||
118 | |||
119 | height_wanted = height_wanted > maxRowHeight ? maxRowHeight |
||
120 | : height_wanted; |
||
121 | |||
122 | if (height_wanted > table.getRowHeight(row)) {
|
||
123 | 41621 | jjdelcerro | int previousRowHeight = table.getRowHeight();
|
124 | 40435 | jjdelcerro | table.setRowHeight(row, height_wanted); |
125 | 41621 | jjdelcerro | table.firePropertyChange("rowheight", previousRowHeight, height_wanted);
|
126 | 40435 | jjdelcerro | } |
127 | |||
128 | return editorComponent;
|
||
129 | } |
||
130 | } |
||
131 | |||
132 | @SuppressWarnings("serial") |
||
133 | private class GeometryToWKTDelegate extends |
||
134 | DefaultCellEditor.EditorDelegate {
|
||
135 | private DefaultCellEditor.EditorDelegate delegate; |
||
136 | |||
137 | private boolean wtkTextTooLong = false; |
||
138 | |||
139 | private int maxWKTLength; |
||
140 | |||
141 | public GeometryToWKTDelegate(DefaultCellEditor.EditorDelegate delegate, |
||
142 | int maxWKTLength) {
|
||
143 | this.delegate = delegate;
|
||
144 | this.maxWKTLength = maxWKTLength;
|
||
145 | } |
||
146 | |||
147 | /**
|
||
148 | * @return the wtkTextTooLong
|
||
149 | */
|
||
150 | public boolean isWtkTextTooLong() { |
||
151 | return wtkTextTooLong;
|
||
152 | } |
||
153 | |||
154 | public void setValue(Object value) { |
||
155 | wtkTextTooLong = false;
|
||
156 | String strValue = ""; |
||
157 | |||
158 | if (value != null) { |
||
159 | try {
|
||
160 | Geometry geometry = (Geometry) value; |
||
161 | strValue = geometry.convertToWKT(); |
||
162 | |||
163 | if (strValue.length() > maxWKTLength) {
|
||
164 | wtkTextTooLong = true;
|
||
165 | delegate.setValue(null);
|
||
166 | } else {
|
||
167 | delegate.setValue(strValue); |
||
168 | } |
||
169 | } catch (Exception ex) { |
||
170 | throw new GeometryToWKTException(ex); |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | |||
175 | public Object getCellEditorValue() { |
||
176 | if (wtkTextTooLong) {
|
||
177 | return null; |
||
178 | } else {
|
||
179 | return delegate.getCellEditorValue();
|
||
180 | } |
||
181 | } |
||
182 | |||
183 | public void actionPerformed(ActionEvent e) { |
||
184 | delegate.actionPerformed(e); |
||
185 | } |
||
186 | |||
187 | public void cancelCellEditing() { |
||
188 | delegate.cancelCellEditing(); |
||
189 | } |
||
190 | |||
191 | public boolean equals(Object obj) { |
||
192 | return delegate.equals(obj);
|
||
193 | } |
||
194 | |||
195 | public int hashCode() { |
||
196 | return delegate.hashCode();
|
||
197 | } |
||
198 | |||
199 | public boolean isCellEditable(EventObject anEvent) { |
||
200 | return delegate.isCellEditable(anEvent);
|
||
201 | } |
||
202 | |||
203 | public void itemStateChanged(ItemEvent e) { |
||
204 | delegate.itemStateChanged(e); |
||
205 | } |
||
206 | |||
207 | public boolean shouldSelectCell(EventObject anEvent) { |
||
208 | return delegate.shouldSelectCell(anEvent);
|
||
209 | } |
||
210 | |||
211 | public boolean startCellEditing(EventObject anEvent) { |
||
212 | return delegate.startCellEditing(anEvent);
|
||
213 | } |
||
214 | |||
215 | public boolean stopCellEditing() { |
||
216 | return delegate.stopCellEditing();
|
||
217 | } |
||
218 | |||
219 | public String toString() { |
||
220 | return delegate.toString();
|
||
221 | } |
||
222 | } |
||
223 | } |