svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extGeocoding / src / org / gvsig / geocoding / gui / TableResultsModel.java @ 31946
History | View | Annotate | Download (11.7 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 |
* 2009 Prodevelop S.L vsanjaime programador
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.geocoding.gui; |
29 |
|
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
import java.util.Set; |
33 |
|
34 |
import javax.swing.table.AbstractTableModel; |
35 |
|
36 |
import org.cresques.cts.IProjection; |
37 |
import org.gvsig.andami.PluginServices; |
38 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
39 |
import org.gvsig.app.project.documents.view.gui.IView; |
40 |
import org.gvsig.fmap.dal.exception.DataException; |
41 |
import org.gvsig.fmap.dal.feature.Feature; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
43 |
import org.gvsig.fmap.dal.feature.FeatureType; |
44 |
import org.gvsig.fmap.geom.Geometry; |
45 |
import org.gvsig.fmap.geom.GeometryLocator; |
46 |
import org.gvsig.fmap.geom.primitive.Point; |
47 |
import org.gvsig.fmap.mapcontext.MapContext; |
48 |
import org.gvsig.fmap.mapcontrol.MapControl; |
49 |
import org.gvsig.geocoding.address.Address; |
50 |
import org.gvsig.geocoding.address.ComposedAddress; |
51 |
import org.gvsig.geocoding.address.Literal; |
52 |
import org.gvsig.geocoding.address.NumberAddress; |
53 |
import org.gvsig.geocoding.extension.GeocodingController; |
54 |
import org.gvsig.geocoding.result.GeocodingResult; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
/**
|
59 |
* table model of the results tables
|
60 |
*
|
61 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
62 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
63 |
*/
|
64 |
|
65 |
public class TableResultsModel extends AbstractTableModel { |
66 |
|
67 |
private static final Logger log = LoggerFactory |
68 |
.getLogger(TableResultsModel.class); |
69 |
|
70 |
private static final long serialVersionUID = 1L; |
71 |
private PluginServices ps = PluginServices.getPluginServices(this); |
72 |
private String[] columnNames; |
73 |
private Object[][] data; |
74 |
private int columnsNumber; |
75 |
|
76 |
private GeocodingController control = null; |
77 |
|
78 |
/**
|
79 |
* Constructor with control
|
80 |
*
|
81 |
* @param control
|
82 |
*/
|
83 |
public TableResultsModel(GeocodingController control) {
|
84 |
|
85 |
this.control = control;
|
86 |
columnNames = new String[4]; |
87 |
columnsNumber = 4;
|
88 |
this.getMainColumnNames();
|
89 |
data = new Object[0][4]; |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Constructor with control and address
|
94 |
*
|
95 |
* @param control
|
96 |
* @param address
|
97 |
*/
|
98 |
public TableResultsModel(GeocodingController control, Address address) {
|
99 |
|
100 |
this.control = control;
|
101 |
|
102 |
// Number address
|
103 |
if (address instanceof NumberAddress) { |
104 |
NumberAddress adr = (NumberAddress) address; |
105 |
Literal mainLit = adr.getMainLiteral(); |
106 |
int litsize = mainLit.size();
|
107 |
columnsNumber = 4 + litsize + 1; |
108 |
columnNames = new String[columnsNumber]; |
109 |
getMainColumnNames(); |
110 |
int i = 0; |
111 |
for (String key : mainLit.keySet()) { |
112 |
columnNames[4 + i] = key;
|
113 |
i++; |
114 |
} |
115 |
columnNames[4 + litsize] = ps.getText("fnumber"); |
116 |
} |
117 |
|
118 |
// Composed address
|
119 |
else if (address instanceof ComposedAddress) { |
120 |
ComposedAddress adr = (ComposedAddress) address; |
121 |
int intersize = adr.getIntersectionLiterals().size();
|
122 |
Literal mainLit = adr.getMainLiteral(); |
123 |
int litsize = mainLit.size();
|
124 |
// cross
|
125 |
if (intersize == 1) { |
126 |
columnsNumber = 4 + litsize + litsize;
|
127 |
columnNames = new String[columnsNumber]; |
128 |
} |
129 |
// between
|
130 |
else {
|
131 |
columnsNumber = 4 + litsize + litsize + litsize;
|
132 |
columnNames = new String[columnsNumber]; |
133 |
} |
134 |
getMainColumnNames(); |
135 |
int i = 0; |
136 |
for (String key : mainLit.keySet()) { |
137 |
columnNames[4 + i] = key + "_1"; |
138 |
i++; |
139 |
} |
140 |
Literal secLit = adr.getIntersectionLiterals().get(0);
|
141 |
i = 0;
|
142 |
for (String key : secLit.keySet()) { |
143 |
columnNames[4 + litsize + i] = key + "_2"; |
144 |
i++; |
145 |
} |
146 |
if (intersize == 2) { |
147 |
Literal thiLit = adr.getIntersectionLiterals().get(1);
|
148 |
i = 0;
|
149 |
for (String key : thiLit.keySet()) { |
150 |
columnNames[4 + litsize + litsize + i] = key + "_3"; |
151 |
i++; |
152 |
} |
153 |
} |
154 |
} |
155 |
// address
|
156 |
else {
|
157 |
Literal mainLit = address.getMainLiteral(); |
158 |
int litsize = mainLit.size();
|
159 |
columnsNumber = 4 + litsize;
|
160 |
columnNames = new String[columnsNumber]; |
161 |
getMainColumnNames(); |
162 |
int i = 0; |
163 |
for (String key : mainLit.keySet()) { |
164 |
columnNames[4 + i] = key;
|
165 |
i++; |
166 |
} |
167 |
} |
168 |
} |
169 |
|
170 |
/**
|
171 |
*
|
172 |
* @param type
|
173 |
* @param set
|
174 |
*/
|
175 |
public TableResultsModel(FeatureType type, FeatureSet set) {
|
176 |
|
177 |
int siz = 0; |
178 |
try {
|
179 |
siz = (int) set.getSize();
|
180 |
|
181 |
columnsNumber = type.size(); |
182 |
columnNames = new String[columnsNumber]; |
183 |
for (int i = 0; i < columnsNumber; i++) { |
184 |
columnNames[i] = type.getAttributeDescriptor(i).getName(); |
185 |
} |
186 |
data = new Object[siz][columnsNumber]; |
187 |
Iterator<Feature> it = set.iterator();
|
188 |
int i = 0; |
189 |
while (it.hasNext()) {
|
190 |
Feature feat = it.next(); |
191 |
for (int j = 0; j < columnsNumber; j++) { |
192 |
String fieldname = type.getAttributeDescriptor(j).getName();
|
193 |
data[i][j] = feat.get(fieldname); |
194 |
} |
195 |
i++; |
196 |
} |
197 |
|
198 |
} catch (DataException e) {
|
199 |
log.error("",e);
|
200 |
} |
201 |
|
202 |
} |
203 |
|
204 |
/**
|
205 |
* Get number of column in the table model
|
206 |
*
|
207 |
* @return
|
208 |
*/
|
209 |
public int getColumnCount() { |
210 |
return columnNames.length;
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* Get rows number
|
215 |
*
|
216 |
* @return
|
217 |
*/
|
218 |
public int getRowCount() { |
219 |
return data.length;
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Get selected column name
|
224 |
*
|
225 |
* @param col
|
226 |
* @return
|
227 |
*/
|
228 |
public String getColumnName(int col) { |
229 |
return columnNames[col];
|
230 |
} |
231 |
|
232 |
/**
|
233 |
* Get registry value
|
234 |
*
|
235 |
* @param row
|
236 |
* @param col
|
237 |
* @return
|
238 |
*/
|
239 |
public Object getValueAt(int row, int col) { |
240 |
if (row < 0 || col < 0) { |
241 |
log.debug("Fila. " + row);
|
242 |
log.debug("Columna. " + col);
|
243 |
return null; |
244 |
} |
245 |
Object obj = data[row][col];
|
246 |
return obj;
|
247 |
} |
248 |
|
249 |
/**
|
250 |
* JTable uses this method to determine the default renderer/ editor for
|
251 |
* each cell. If we didn't implement this method, then the last column would
|
252 |
* contain text ("true"/"false"), rather than a check box.
|
253 |
*
|
254 |
* @param c
|
255 |
* @return
|
256 |
*/
|
257 |
public Class getColumnClass(int c) { |
258 |
Object obj = getValueAt(0, c); |
259 |
if (obj == null) { |
260 |
return String.class; |
261 |
} |
262 |
return obj.getClass();
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* Are cells editables
|
267 |
*
|
268 |
* @param row
|
269 |
* @param col
|
270 |
* @return
|
271 |
*/
|
272 |
public boolean isCellEditable(int row, int col) { |
273 |
return false; |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Set value in selected cell
|
278 |
*
|
279 |
* @param row
|
280 |
* @param col
|
281 |
* @return
|
282 |
*/
|
283 |
public void setValueAt(Object value, int row, int col) { |
284 |
data[row][col] = value; |
285 |
fireTableCellUpdated(row, col); |
286 |
} |
287 |
|
288 |
/**
|
289 |
* fill the model with results set
|
290 |
*
|
291 |
* @param result
|
292 |
* @param maxResults
|
293 |
* @param score
|
294 |
*/
|
295 |
public void setResultSet(Set<GeocodingResult> result, int maxResults, |
296 |
double score) {
|
297 |
|
298 |
data = new Object[0][columnsNumber]; |
299 |
fireTableDataChanged(); |
300 |
|
301 |
int num = result.size();
|
302 |
if (result.size() > maxResults) {
|
303 |
num = maxResults; |
304 |
// count score ok
|
305 |
int numscore = 0; |
306 |
for (GeocodingResult res : result) {
|
307 |
double sco = res.getScore();
|
308 |
if (sco >= score) {
|
309 |
numscore++; |
310 |
} |
311 |
} |
312 |
if (num > numscore) {
|
313 |
num = numscore; |
314 |
} |
315 |
} else {
|
316 |
// count score ok
|
317 |
int numscore = 0; |
318 |
for (GeocodingResult res : result) {
|
319 |
double sco = res.getScore();
|
320 |
if (sco >= score) {
|
321 |
numscore++; |
322 |
} |
323 |
} |
324 |
if (num > numscore) {
|
325 |
num = numscore; |
326 |
} |
327 |
} |
328 |
|
329 |
data = new Object[num][columnsNumber]; |
330 |
int i = 0; |
331 |
log.debug("Results: " + result.size());
|
332 |
Iterator<GeocodingResult> it = result.iterator();
|
333 |
while (it.hasNext()) {
|
334 |
if (i < num) {
|
335 |
GeocodingResult res = it.next(); |
336 |
|
337 |
int id = i + 1; |
338 |
Double score1 = new Double(res.getScore()); |
339 |
|
340 |
data[i][0] = id;
|
341 |
data[i][1] = score1.intValue();
|
342 |
if (res.getGeometry() != null) { |
343 |
Double x = new Double(((Point) res.getGeometry()).getX()); |
344 |
Double y = new Double(((Point) res.getGeometry()).getY()); |
345 |
data[i][2] = x;
|
346 |
data[i][3] = y;
|
347 |
} else {
|
348 |
data[i][2] = 0.0; |
349 |
data[i][3] = 0.0; |
350 |
} |
351 |
|
352 |
// range
|
353 |
if (res.getAddress() instanceof NumberAddress) { |
354 |
// get number address
|
355 |
NumberAddress address = (NumberAddress) res.getAddress(); |
356 |
Literal mainLit = address.getMainLiteral(); |
357 |
int litsize = mainLit.size();
|
358 |
// fill literal address values
|
359 |
int j = 4; |
360 |
for (String value : mainLit.values()) { |
361 |
data[i][j] = value; |
362 |
} |
363 |
// fill address number
|
364 |
data[i][4 + litsize] = address.getNumber();
|
365 |
} |
366 |
|
367 |
// Composed address
|
368 |
else if (res.getAddress() instanceof ComposedAddress) { |
369 |
// get compose address
|
370 |
ComposedAddress address = (ComposedAddress) res |
371 |
.getAddress(); |
372 |
// main literal
|
373 |
Literal mainLit = address.getMainLiteral(); |
374 |
int litsize = mainLit.size();
|
375 |
// fill main literal address values
|
376 |
int j = 4; |
377 |
for (String value : mainLit.values()) { |
378 |
data[i][j] = value; |
379 |
j++; |
380 |
} |
381 |
// fill second literal address values
|
382 |
List<Literal> inter = address.getIntersectionLiterals();
|
383 |
Literal secLit = inter.get(0);
|
384 |
j = 4 + litsize;
|
385 |
for (String value : secLit.values()) { |
386 |
data[i][j] = value; |
387 |
j++; |
388 |
} |
389 |
// third literal (between)
|
390 |
int intersize = address.getIntersectionLiterals().size();
|
391 |
if (intersize == 2) { |
392 |
Literal thiLit = inter.get(1);
|
393 |
j = 4 + litsize + litsize;
|
394 |
for (String value : thiLit.values()) { |
395 |
data[i][j] = value; |
396 |
j++; |
397 |
} |
398 |
} |
399 |
} |
400 |
// address
|
401 |
else {
|
402 |
Literal mainLit = res.getAddress().getMainLiteral(); |
403 |
int litsize = mainLit.size();
|
404 |
int j = 4; |
405 |
for (String value : mainLit.values()) { |
406 |
data[i][j] = value; |
407 |
j++; |
408 |
} |
409 |
} |
410 |
|
411 |
} else {
|
412 |
break;
|
413 |
} |
414 |
i++; |
415 |
} |
416 |
fireTableDataChanged(); |
417 |
} |
418 |
|
419 |
/**
|
420 |
* Get the main columns names
|
421 |
*/
|
422 |
private void getMainColumnNames() { |
423 |
|
424 |
if (ps != null) { |
425 |
columnNames[0] = ps.getText("fid"); |
426 |
columnNames[1] = ps.getText("fscore"); |
427 |
IView view = control.getCurrentView(); |
428 |
if (view != null) { |
429 |
MapContext context = ((DefaultViewPanel)view).getModel().getMapContext(); |
430 |
MapControl mControl = new MapControl();
|
431 |
mControl.setMapContext(context); |
432 |
IProjection proj = mControl.getProjection(); |
433 |
if (proj.isProjected()) {
|
434 |
columnNames[2] = ps.getText("fx"); |
435 |
columnNames[3] = ps.getText("fy"); |
436 |
} else {
|
437 |
columnNames[2] = ps.getText("flong"); |
438 |
columnNames[3] = ps.getText("flat"); |
439 |
} |
440 |
} else {
|
441 |
columnNames[2] = ps.getText("fx"); |
442 |
columnNames[3] = ps.getText("fy"); |
443 |
} |
444 |
} |
445 |
} |
446 |
|
447 |
/**
|
448 |
* get the geometry (Point2D)
|
449 |
*
|
450 |
* @param row
|
451 |
* @return
|
452 |
*/
|
453 |
public Point getGeometry(int row, int xColumn, int yColumn) { |
454 |
Double x = (Double) getValueAt(row, xColumn); |
455 |
Double y = (Double) getValueAt(row, yColumn); |
456 |
Point pto = null; |
457 |
try {
|
458 |
pto = GeometryLocator.getGeometryManager().createPoint(x, y, |
459 |
Geometry.SUBTYPES.GEOM2D); |
460 |
} catch (Exception e) { |
461 |
log.error("Error building a geometry", e);
|
462 |
} |
463 |
return pto;
|
464 |
} |
465 |
|
466 |
} |