Revision 27464 branches/v2_0_0_prep/extensions/extGeocoding/src/org/gvsig/geocoding/gui/TableResultsModel.java

View differences:

TableResultsModel.java
27 27

  
28 28
package org.gvsig.geocoding.gui;
29 29

  
30
import java.util.Iterator;
30 31
import java.util.List;
32
import java.util.Set;
31 33

  
32 34
import javax.swing.table.AbstractTableModel;
33 35

  
34 36
import org.cresques.cts.IProjection;
37
import org.gvsig.fmap.geom.primitive.Point2D;
35 38
import org.gvsig.fmap.mapcontext.MapContext;
36 39
import org.gvsig.fmap.mapcontrol.MapControl;
40
import org.gvsig.geocoding.Address;
41
import org.gvsig.geocoding.AddressComponent;
42
import org.gvsig.geocoding.ComposeAddress;
43
import org.gvsig.geocoding.Literal;
44
import org.gvsig.geocoding.NumberAddress;
45
import org.gvsig.geocoding.extension.GeocoController;
37 46
import org.gvsig.geocoding.result.GeocodingResult;
38 47
import org.gvsig.geocoding.utils.GeocoUtils;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
39 50

  
40 51
import com.iver.andami.PluginServices;
41 52
import com.iver.cit.gvsig.project.documents.view.gui.View;
......
49 60

  
50 61
public class TableResultsModel extends AbstractTableModel {
51 62

  
63
	private static final Logger log = LoggerFactory
64
			.getLogger(TableResultsModel.class);
65

  
52 66
	private static final long serialVersionUID = 1L;
53 67
	private PluginServices ps = PluginServices.getPluginServices(this);
54
	private String[] columnNames = new String[5];;
55
	private Object[][] data = new Object[0][5];
68
	private String[] columnNames;
69
	private Object[][] data;
70
	private GeocoController control;
71
	private int columnsNumber;
56 72

  
57
	public TableResultsModel() {
58
		columnNames[0] = ps.getText("fsel");
59
		columnNames[1] = ps.getText("fquality");
60
		columnNames[2] = ps.getText("faccuracy");
61
		View view = GeocoUtils.getCurrentView();
62
		if (view != null) {
63
			MapContext context = view.getModel().getMapContext();
64
			MapControl mControl = new MapControl();
65
			mControl.setMapContext(context);
66
			IProjection proj = mControl.getProjection();
67
			if (proj.isProjected()) {
68
				columnNames[3] = ps.getText("fx");
69
				columnNames[4] = ps.getText("fy");
70
			} else {
71
				columnNames[3] = ps.getText("flong");
72
				columnNames[4] = ps.getText("flat");
73
	/**
74
	 * 
75
	 */
76
	public TableResultsModel(GeocoController control) {
77
		this.control = control;
78
		columnNames = new String[5];
79
		columnsNumber = 5;
80
		this.getMainColumnNames();
81
		data = new Object[0][5];
82
	}
83

  
84
	/**
85
	 * 
86
	 * @param address
87
	 */
88
	public TableResultsModel(GeocoController control, Address address) {
89
		this.control = control;
90
		// Number address
91

  
92
		if (address instanceof NumberAddress) {
93
			NumberAddress adr = (NumberAddress) address;
94
			Literal mainLit = adr.getMainLiteral();
95
			int litsize = mainLit.size();
96
			// main columns+literal+numbers
97
			columnsNumber = 5 + litsize + 2;
98
			columnNames = new String[columnsNumber];
99
			getMainColumnNames();
100
			for (int i = 0; i < litsize; i++) {
101
				columnNames[5 + i] = ((AddressComponent) mainLit.get(i))
102
						.getKeyElement();
73 103
			}
74
		} else {
75
			columnNames[3] = ps.getText("fx");
76
			columnNames[4] = ps.getText("fy");
104
			columnNames[5 + litsize] = ps.getText("fnumber");
77 105
		}
106

  
107
		// Composed address
108
		else if (address instanceof ComposeAddress) {
109
			ComposeAddress adr = (ComposeAddress) address;
110
			int intersize = adr.getIntersectionLiteral().size();
111
			Literal mainLit = adr.getMainLiteral();
112
			int litsize = mainLit.size();
113
			// cross
114
			if (intersize == 1) {
115
				columnsNumber = 5 + litsize + litsize;
116
				columnNames = new String[columnsNumber];
117
			}
118
			// between
119
			else {
120
				columnsNumber = 5 + litsize + litsize + litsize;
121
				columnNames = new String[columnsNumber];
122
			}
123
			getMainColumnNames();
124
			for (int i = 0; i < litsize; i++) {
125
				columnNames[5 + i] = ((AddressComponent) mainLit.get(i))
126
						.getKeyElement()+"_1";
127
			}
128
			Literal secLit = adr.getIntersectionLiteral().get(0);
129
			for (int i = 0; i < litsize; i++) {
130
				columnNames[5 + litsize + i] = ((AddressComponent) secLit
131
						.get(i)).getKeyElement()
132
						+ "_2";
133
			}
134
			if (intersize == 2) {
135
				Literal thiLit = adr.getIntersectionLiteral().get(1);
136
				for (int i = 0; i < litsize; i++) {
137
					columnNames[5 + litsize + litsize + i] = ((AddressComponent) thiLit
138
							.get(i)).getKeyElement()
139
							+ "_3";
140
				}
141
			}
142
		}
143
		// address
144
		else {
145
			Literal mainLit = address.getMainLiteral();
146
			int litsize = mainLit.size();
147
			columnsNumber = 5 + litsize;
148
			columnNames = new String[columnsNumber];
149
			getMainColumnNames();
150
			for (int i = 0; i < litsize; i++) {
151
				columnNames[5 + i] = ((AddressComponent) mainLit.get(i))
152
						.getKeyElement();
153
			}
154
		}
78 155
	}
79 156

  
157
	/**
158
	 * 
159
	 */
80 160
	public int getColumnCount() {
81 161
		return columnNames.length;
82 162
	}
83 163

  
164
	/**
165
	 * 
166
	 */
84 167
	public int getRowCount() {
85 168
		return data.length;
86 169
	}
87 170

  
171
	/**
172
	 * 
173
	 */
88 174
	public String getColumnName(int col) {
89 175
		return columnNames[col];
90 176
	}
91 177

  
178
	/**
179
	 * 
180
	 */
92 181
	public Object getValueAt(int row, int col) {
93 182
		return data[row][col];
94 183
	}
......
108 197
	public boolean isCellEditable(int row, int col) {
109 198
		// Note that the data/cell address is constant,
110 199
		// no matter where the cell appears onscreen.
111
		if (col == 0) {
200
		if (col == 1) {
112 201
			return true;
113 202
		} else {
114 203
			return false;
......
127 216
	 * 
128 217
	 * @param loc
129 218
	 */
130
	public void setGeocoLocationSet(List<GeocodingResult> result, int maxResults) {
131
		// int num = loc.size();
132
		// if (loc.size() > maxResults) {
133
		// num = maxResults;
134
		// }
135
		// data = new Object[num][5];
136
		// for (int i = 0; i < num; i++) {
137
		// OpGeocoLocation optional = loc.getOpGeocoLocation(i);
138
		// Boolean sel = new Boolean(optional.isSelected());
139
		// Integer qua = new Integer(optional.getQuality());
140
		// Integer accu = new Integer(optional.getAccuracy());
141
		// Double x = new Double(optional.getX());
142
		// Double y = new Double(optional.getY());
143
		// data[i][0] = sel;
144
		// data[i][1] = qua;
145
		// data[i][2] = accu;
146
		// data[i][3] = x;
147
		// data[i][4] = y;
148
		// }
149
		// fireTableDataChanged();
219
	public void setResultSet(Set<GeocodingResult> result, int maxResults,
220
			double score) {
221

  
222
		data = new Object[0][columnsNumber];
223
		fireTableDataChanged();
224

  
225
		int num = result.size();
226
		if (result.size() > maxResults) {
227
			num = maxResults;
228
			// count score ok
229
			int numscore = 0;
230
			for (GeocodingResult res : result) {
231
				double sco = res.getScore();
232
				if (sco >= score) {
233
					numscore++;
234
				}
235
			}
236
			if (num > numscore) {
237
				num = numscore;
238
			}
239
		} else {
240
			// count score ok
241
			int numscore = 0;
242
			for (GeocodingResult res : result) {
243
				double sco = res.getScore();
244
				if (sco >= score) {
245
					numscore++;
246
				}
247
			}
248
			if (num > numscore) {
249
				num = numscore;
250
			}
251
		}
252

  
253
		data = new Object[num][columnsNumber];
254
		int i = 0;
255
		log.debug("Results: " + result.size());
256
		Iterator<GeocodingResult> it = result.iterator();
257
		while (it.hasNext()) {
258
			if (i < num) {
259
				GeocodingResult res = it.next();
260

  
261
				int id = i + 1;
262
				Boolean sel = id == 1 ? true : false;
263
				Double score1 = new Double(res.getScore());
264
				Double x = new Double(((Point2D) res.getGeometry()).getX());
265
				Double y = new Double(((Point2D) res.getGeometry()).getY());
266
				data[i][0] = id;
267
				data[i][1] = sel;
268
				data[i][2] = score1.intValue();
269
				data[i][3] = x;
270
				data[i][4] = y;
271

  
272
				// range
273
				if (res.getAddress() instanceof NumberAddress) {
274
					// get number address
275
					NumberAddress address = (NumberAddress) res.getAddress();
276
					Literal mainLit = address.getMainLiteral();
277
					int litsize = mainLit.size();
278
					// fill literal address values
279
					for (int j = 0; j < litsize; j++) {
280
						int cont = 5 + j;
281
						AddressComponent comp = (AddressComponent) mainLit
282
								.get(j);
283
						data[i][cont] = comp.getValue();
284
					}
285
					// fill address number
286
					data[i][5 + litsize] = address.getNumber();
287
				}
288

  
289
				// Composed address
290
				else if (res.getAddress() instanceof ComposeAddress) {
291
					// get compose address
292
					ComposeAddress address = (ComposeAddress) res.getAddress();
293
					// main literal
294
					Literal mainLit = address.getMainLiteral();
295
					int litsize = mainLit.size();
296
					// fill main literal address values
297
					for (int j = 0; j < litsize; j++) {
298
						int cont = 5 + j;
299
						AddressComponent comp = (AddressComponent) mainLit
300
								.get(j);
301
						data[i][cont] = comp.getValue();
302
					}
303
					// fill second literal address values
304
					List<Literal> inter = address.getIntersectionLiteral();
305
					Literal secLit = inter.get(0);
306
					for (int j = 0; j < litsize; j++) {
307
						int cont = 5 + litsize + j;
308
						AddressComponent comp = (AddressComponent) secLit
309
								.get(j);
310
						data[i][cont] = comp.getValue();
311
					}
312
					// third literal (between)
313
					int intersize = address.getIntersectionLiteral().size();
314
					if (intersize == 2) {
315
						Literal thiLit = inter.get(1);
316
						for (int j = 0; j < litsize; j++) {
317
							int cont = 5 + litsize + litsize + j;
318
							AddressComponent comp = (AddressComponent) thiLit
319
									.get(j);
320
							data[i][cont] = comp.getValue();
321
						}
322
					}
323
				}
324
				// address
325
				else {
326
					Literal mainLit = res.getAddress().getMainLiteral();
327
					int litsize = mainLit.size();
328
					for (int j = 0; j < litsize; j++) {
329
						int cont = 5 + j;
330
						AddressComponent comp = (AddressComponent) mainLit
331
								.get(j);
332
						data[i][cont] = comp.getValue();
333
					}
334
				}
335

  
336
			} else {
337
				break;
338
			}
339
			i++;
340
		}
341
		fireTableDataChanged();
342

  
150 343
	}
151 344

  
152 345
	/**
153
	 * This method sort
154 346
	 * 
155
	 * @param column
156 347
	 */
157
	public void sort(int column) {
348
	private void getMainColumnNames() {
158 349

  
350
		columnNames[0] = ps.getText("fid");
351
		columnNames[1] = ps.getText("fsel");
352
		columnNames[2] = ps.getText("fscore");
353
		View view = GeocoUtils.getCurrentView();
354
		if (view != null) {
355
			MapContext context = view.getModel().getMapContext();
356
			MapControl mControl = new MapControl();
357
			mControl.setMapContext(context);
358
			IProjection proj = mControl.getProjection();
359
			if (proj.isProjected()) {
360
				columnNames[3] = ps.getText("fx");
361
				columnNames[4] = ps.getText("fy");
362
			} else {
363
				columnNames[3] = ps.getText("flong");
364
				columnNames[4] = ps.getText("flat");
365
			}
366
		} else {
367
			columnNames[3] = ps.getText("fx");
368
			columnNames[4] = ps.getText("fy");
369
		}
159 370
	}
160 371

  
372
	/**
373
	 * get the geometry
374
	 * 
375
	 * @param row
376
	 * @return
377
	 */
378
	public Point2D getGeometry(int row) {
379
		Double x = (Double) getValueAt(row, 3);
380
		Double y = (Double) getValueAt(row, 4);
381
		return new Point2D(x, y);
382
	}
383

  
161 384
}

Also available in: Unified diff