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