gvsig-lrs / org.gvsig.lrs / trunk / org.gvsig.lrs / org.gvsig.lrs.swing / org.gvsig.lrs.swing.impl / src / main / java / org / gvsig / lrs / swing / impl / JLrsEditRouteCalibrationController.java @ 44
History | View | Annotate | Download (27.6 KB)
1 |
/*
|
---|---|
2 |
* Copyright 2015 DiSiD Technologies S.L.L. All rights reserved.
|
3 |
*
|
4 |
* Project : DiSiD org.gvsig.lrs.swing.impl
|
5 |
* SVN Id : $Id$
|
6 |
*/
|
7 |
package org.gvsig.lrs.swing.impl; |
8 |
|
9 |
import java.awt.Color; |
10 |
import java.awt.event.ActionEvent; |
11 |
import java.awt.event.ActionListener; |
12 |
import java.awt.event.ItemEvent; |
13 |
import java.awt.event.ItemListener; |
14 |
import java.text.MessageFormat; |
15 |
import java.util.ArrayList; |
16 |
import java.util.List; |
17 |
import java.util.Locale; |
18 |
|
19 |
import javax.swing.ComboBoxModel; |
20 |
import javax.swing.DefaultComboBoxModel; |
21 |
import javax.swing.JComponent; |
22 |
import javax.swing.event.DocumentEvent; |
23 |
import javax.swing.event.DocumentListener; |
24 |
import javax.swing.event.ListSelectionEvent; |
25 |
import javax.swing.event.ListSelectionListener; |
26 |
import javax.swing.event.TableModelEvent; |
27 |
import javax.swing.event.TableModelListener; |
28 |
|
29 |
import org.cresques.cts.ICoordTrans; |
30 |
import org.slf4j.Logger; |
31 |
import org.slf4j.LoggerFactory; |
32 |
|
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.exception.ReadException; |
35 |
import org.gvsig.fmap.dal.feature.Feature; |
36 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
37 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
38 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
39 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.dal.feature.FeatureType; |
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.fmap.geom.aggregate.MultiLine; |
45 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
46 |
import org.gvsig.fmap.geom.primitive.Line; |
47 |
import org.gvsig.fmap.geom.primitive.Point; |
48 |
import org.gvsig.fmap.mapcontext.MapContext; |
49 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
50 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
51 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
52 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
53 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
54 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
55 |
import org.gvsig.lrs.lib.api.LrsAlgorithmParams; |
56 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator; |
57 |
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager; |
58 |
import org.gvsig.lrs.lib.api.LrsEditRouteCalibrationAlgorithmParams; |
59 |
import org.gvsig.lrs.lib.api.exceptions.LrsGettingParametersException; |
60 |
import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException; |
61 |
import org.gvsig.lrs.swing.api.JLrsAlgorithmParams; |
62 |
import org.gvsig.tools.ToolsLocator; |
63 |
import org.gvsig.tools.dispose.DisposableIterator; |
64 |
import org.gvsig.tools.exception.BaseException; |
65 |
import org.gvsig.tools.i18n.I18nManager; |
66 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
67 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
68 |
import org.gvsig.tools.visitor.VisitCanceledException; |
69 |
import org.gvsig.tools.visitor.Visitor; |
70 |
|
71 |
public class JLrsEditRouteCalibrationController extends |
72 |
JLrsEditRouteCalibrationParamsView implements JLrsAlgorithmParams {
|
73 |
|
74 |
/**
|
75 |
*
|
76 |
*/
|
77 |
private static final long serialVersionUID = 7762321351616631999L; |
78 |
|
79 |
private static final Logger logger = LoggerFactory |
80 |
.getLogger(JLrsEditRouteCalibrationController.class); |
81 |
|
82 |
private boolean canceled; |
83 |
private boolean hasWarnings; |
84 |
private List<String> warnings; |
85 |
private ActionListenerSupport listeners =
|
86 |
ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport(); |
87 |
|
88 |
private FLayer layer;
|
89 |
private FeatureAttributeDescriptor fieldIdRoute;
|
90 |
private FeatureSet selectedFeatures;
|
91 |
private Feature editedFeature;
|
92 |
private Double editedFeatureMax; |
93 |
private Double editedFeatureMin; |
94 |
private MapContext mapContext;
|
95 |
GraphicLayer graphics; |
96 |
private int idMarkerSymbol; |
97 |
private int idLineSymbol; |
98 |
|
99 |
public static final String EDIT_ROUTECALIBRATION_GRAPHICS_ID = "EditRouteCalibration"; |
100 |
|
101 |
/**
|
102 |
* @param layer
|
103 |
* @throws LrsNeededParameterException
|
104 |
* @throws LrsGettingParametersException
|
105 |
*
|
106 |
*/
|
107 |
public JLrsEditRouteCalibrationController(FLayer layer, MapContext mapContext, FeatureAttributeDescriptor fieldIdRoute,
|
108 |
LrsAlgorithmParams params) throws LrsNeededParameterException, LrsGettingParametersException {
|
109 |
super();
|
110 |
this.layer = layer;
|
111 |
this.fieldIdRoute=fieldIdRoute;
|
112 |
this.mapContext = mapContext;
|
113 |
initializeGraphics(mapContext); |
114 |
initComponents(); |
115 |
translate(); |
116 |
setParams(params); |
117 |
|
118 |
} |
119 |
|
120 |
/**
|
121 |
* @param mapContext
|
122 |
*/
|
123 |
private void initializeGraphics(MapContext mapContext) { |
124 |
this.graphics = mapContext.getGraphicsLayer();
|
125 |
SymbolManager symbolManager = MapContextLocator.getSymbolManager(); |
126 |
|
127 |
ISymbol markerSymbol = symbolManager.createSymbol(Geometry.TYPES.POINT, Color.RED);
|
128 |
ISymbol lineSymbol = symbolManager.createSymbol(Geometry.TYPES.CURVE, Color.YELLOW);
|
129 |
this.idLineSymbol = this.graphics.addSymbol(lineSymbol); |
130 |
this.idMarkerSymbol = this.graphics.addSymbol(markerSymbol); |
131 |
} |
132 |
|
133 |
public void addActionListener(ActionListener listener) { |
134 |
listeners.addActionListener(listener); |
135 |
} |
136 |
|
137 |
public void removeActionListener(ActionListener listener) { |
138 |
listeners.removeActionListener(listener); |
139 |
} |
140 |
|
141 |
private void translate() { |
142 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
143 |
lblIdRouteField |
144 |
.setText(i18nManager.getTranslation(lblIdRouteField.getText())); |
145 |
lblMaximum.setText(i18nManager.getTranslation(lblMaximum.getText())); |
146 |
lblMinimum.setText(i18nManager.getTranslation(lblMinimum.getText())); |
147 |
btnInvert.setText(i18nManager.getTranslation(btnInvert.getText())); |
148 |
btnApply.setText(i18nManager.getTranslation(btnApply.getText())); |
149 |
btnAccept.setText(i18nManager.getTranslation(btnAccept.getText())); |
150 |
btnCancel.setText(i18nManager.getTranslation(btnCancel.getText())); |
151 |
} |
152 |
|
153 |
/**
|
154 |
*
|
155 |
*/
|
156 |
public void setLocate(Locale locale) { |
157 |
Locale l = super.getLocale(); |
158 |
if (!l.equals(locale)) {
|
159 |
translate(); |
160 |
} |
161 |
super.setLocale(locale);
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* Validates it is an editable curve layer with at least 1 non-geometric
|
166 |
* field
|
167 |
* if it is not correct throws an exception
|
168 |
*
|
169 |
* @throws LrsNeededParameterException
|
170 |
* @throws ReadException
|
171 |
*/
|
172 |
public static void validateLayer(FLayer layer) |
173 |
throws LrsNeededParameterException {
|
174 |
logger.debug("Validating layer for Edit Route");
|
175 |
if (layer == null) { |
176 |
logger.error("A layer is needed");
|
177 |
throw new LrsNeededParameterException("a_layer_is_needed", null); |
178 |
} |
179 |
|
180 |
if (!layer.isEditing()) {
|
181 |
logger.error("An editing layer is needed");
|
182 |
throw new LrsNeededParameterException("an_editing_layer_is_needed", |
183 |
null);
|
184 |
} |
185 |
|
186 |
if (!JLrsUtils.isMlayerWithNonGeomField(layer)) {
|
187 |
logger.error("Impossible to find the layers needed");
|
188 |
throw new LrsNeededParameterException( |
189 |
"impossible_to_find_needed_layer", null); |
190 |
} |
191 |
} |
192 |
|
193 |
/**
|
194 |
* Checks if the selectedIdRoute is contained in the layer
|
195 |
*
|
196 |
* @throws LrsNeededParameterException
|
197 |
* @throws ReadException
|
198 |
*/
|
199 |
public static void validateSelectedIdRoute(FLayer layer, FeatureAttributeDescriptor selectedIdRoute) |
200 |
throws LrsNeededParameterException {
|
201 |
logger.debug("Validating selected id route for Edit Route");
|
202 |
if (selectedIdRoute == null) { |
203 |
logger.error("A selectedIdRoute is needed");
|
204 |
throw new LrsNeededParameterException("a_selectedIdRoute_is_needed", null); |
205 |
} |
206 |
|
207 |
if (!isCorrectIdRoute(layer,selectedIdRoute)) {
|
208 |
logger.error("Selected id route is not contained in editing layer");
|
209 |
throw new LrsNeededParameterException( |
210 |
"route_not_contained", null); |
211 |
} |
212 |
} |
213 |
|
214 |
public void validateSelectedFeatures() throws LrsGettingParametersException{ |
215 |
//TODO added to Test
|
216 |
if (selectedFeatures!=null) return; |
217 |
|
218 |
final List<String> routeNames=new ArrayList<String>(); |
219 |
try {
|
220 |
selectedFeatures.accept(new Visitor() {
|
221 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
222 |
if(obj instanceof Feature){ |
223 |
Feature feature=(Feature)obj; |
224 |
if (feature.get(fieldIdRoute.getName()) instanceof String){ |
225 |
String routeName=(String)feature.get(fieldIdRoute.getName()); |
226 |
if (routeNames.contains(routeName)){
|
227 |
logger.error("Duplicate route name found");
|
228 |
throw new LrsGettingParametersException("Duplicate route name found",null); |
229 |
}else{
|
230 |
routeNames.add(routeName); |
231 |
} |
232 |
} |
233 |
} |
234 |
} |
235 |
}); |
236 |
} catch (BaseException e1) {
|
237 |
logger.error("Impossible to validate selected features",e1);
|
238 |
throw new LrsGettingParametersException("Impossible to validate selected features", e1); |
239 |
} |
240 |
} |
241 |
|
242 |
/**
|
243 |
* @throws LrsNeededParameterException
|
244 |
* @throws LrsGettingParametersException
|
245 |
*/
|
246 |
private void initComponents() throws LrsNeededParameterException, LrsGettingParametersException { |
247 |
|
248 |
validateLayer(layer); |
249 |
validateSelectedIdRoute(layer, fieldIdRoute); |
250 |
|
251 |
selectedFeatures=getSelectedFeatures(layer); |
252 |
validateSelectedFeatures(); |
253 |
|
254 |
String selectedRouteName=initCmbRoute();
|
255 |
|
256 |
routeChanged(selectedRouteName); |
257 |
|
258 |
this.btnApply.addActionListener(new ActionListener() { |
259 |
public void actionPerformed(ActionEvent e) { |
260 |
canceled = false;
|
261 |
doApply(); |
262 |
} |
263 |
}); |
264 |
|
265 |
this.btnInvert.addActionListener(new ActionListener() { |
266 |
public void actionPerformed(ActionEvent e) { |
267 |
canceled = false;
|
268 |
doInvert(); |
269 |
} |
270 |
}); |
271 |
|
272 |
this.btnAccept.addActionListener(new ActionListener() { |
273 |
|
274 |
public void actionPerformed(ActionEvent e) { |
275 |
canceled = false;
|
276 |
validateData(); |
277 |
doClose(); |
278 |
listeners.fireActionEvent(new ActionEvent( |
279 |
JLrsEditRouteCalibrationController.this, 0,
|
280 |
"accept"));
|
281 |
} |
282 |
}); |
283 |
|
284 |
this.btnCancel.addActionListener(new ActionListener() { |
285 |
|
286 |
public void actionPerformed(ActionEvent e) { |
287 |
canceled = true;
|
288 |
doClose(); |
289 |
listeners.fireActionEvent(new ActionEvent( |
290 |
JLrsEditRouteCalibrationController.this, 0,
|
291 |
"cancel"));
|
292 |
} |
293 |
}); |
294 |
|
295 |
this.tblLayerInfo.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
296 |
|
297 |
public void valueChanged(ListSelectionEvent e) { |
298 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
299 |
|
300 |
graphics.removeGraphics(EDIT_ROUTECALIBRATION_GRAPHICS_ID); |
301 |
showEditingRoute(); |
302 |
int[] selectedRows = tblLayerInfo.getSelectedRows(); |
303 |
for (int i = 0; i < selectedRows.length; i++) { |
304 |
Point point = null; |
305 |
try {
|
306 |
point = (Point) geomManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
|
307 |
point.setX((Double)tblLayerInfo.getValueAt(selectedRows[i], 2)); |
308 |
point.setY((Double)tblLayerInfo.getValueAt(selectedRows[i], 3)); |
309 |
|
310 |
ICoordTrans ct = layer.getCoordTrans(); |
311 |
point.reProject(ct); |
312 |
|
313 |
} catch (CreateGeometryException e1) {
|
314 |
logger.warn("Can't create a point", e);
|
315 |
} |
316 |
if(point != null){ |
317 |
graphics.addGraphic( |
318 |
EDIT_ROUTECALIBRATION_GRAPHICS_ID, |
319 |
point, |
320 |
idMarkerSymbol); |
321 |
} |
322 |
} |
323 |
mapContext.invalidate(); |
324 |
} |
325 |
}); |
326 |
|
327 |
setVisibleAceptCancel(false);
|
328 |
canceled = false;
|
329 |
} |
330 |
|
331 |
private void reloadMaxMin(EditRouteCalibrationTableModel tableModel){ |
332 |
this.txtMaximum.getDocument().addDocumentListener(new DocumentListener() { |
333 |
public void changedUpdate(DocumentEvent e) { |
334 |
enableApply(e); |
335 |
} |
336 |
public void removeUpdate(DocumentEvent e) { |
337 |
enableApply(e); |
338 |
} |
339 |
public void insertUpdate(DocumentEvent e) { |
340 |
enableApply(e); |
341 |
} |
342 |
|
343 |
public void enableApply(DocumentEvent e) { |
344 |
try {
|
345 |
e.getType(); |
346 |
Double max = Double.valueOf(txtMaximum.getText()); |
347 |
Double min = Double.valueOf(txtMinimum.getText()); |
348 |
} catch (Exception ex) { |
349 |
btnApply.setEnabled(false);
|
350 |
return;
|
351 |
} |
352 |
btnApply.setEnabled(true);
|
353 |
} |
354 |
}); |
355 |
|
356 |
this.txtMinimum.getDocument().addDocumentListener(new DocumentListener() { |
357 |
public void changedUpdate(DocumentEvent e) { |
358 |
enableApply(e); |
359 |
} |
360 |
public void removeUpdate(DocumentEvent e) { |
361 |
enableApply(e); |
362 |
} |
363 |
public void insertUpdate(DocumentEvent e) { |
364 |
enableApply(e); |
365 |
} |
366 |
|
367 |
public void enableApply(DocumentEvent e) { |
368 |
try{
|
369 |
Double max = Double.valueOf(txtMaximum.getText()); |
370 |
Double min = Double.valueOf(txtMinimum.getText()); |
371 |
}catch(Exception ex){ |
372 |
btnApply.setEnabled(false);
|
373 |
return;
|
374 |
} |
375 |
btnApply.setEnabled(true);
|
376 |
} |
377 |
}); |
378 |
|
379 |
editedFeatureMax=tableModel.getMaxValue(); |
380 |
editedFeatureMin=tableModel.getMinValue(); |
381 |
this.txtMaximum.setText(editedFeatureMax.toString());
|
382 |
this.txtMinimum.setText(editedFeatureMin.toString());
|
383 |
} |
384 |
|
385 |
private void doInvert(){ |
386 |
String max = txtMaximum.getText();
|
387 |
String min = txtMinimum.getText();
|
388 |
|
389 |
txtMaximum.setText(min); |
390 |
txtMinimum.setText(max); |
391 |
} |
392 |
|
393 |
private void doApply(){ |
394 |
reloadTableModel(); |
395 |
EditRouteCalibrationTableModel tableModel=(EditRouteCalibrationTableModel)tblLayerInfo.getModel(); |
396 |
Object[][] data=tableModel.getData(); |
397 |
Double newMax= Double.valueOf(txtMaximum.getText()); |
398 |
Double newMin= Double.valueOf(txtMinimum.getText()); |
399 |
|
400 |
Double oldMin=editedFeatureMin;
|
401 |
Double oldMax=editedFeatureMax;
|
402 |
|
403 |
if (data!=null&&data.length>1){ |
404 |
for (int i=0;data.length-1>=i;i++){ |
405 |
Double mCoordinate =(Double)data[i][EditRouteCalibrationTableModel.COLUMN_MCOORDINATE]; |
406 |
Double newMCoordinate=calculateNewM(oldMax, oldMin, newMax, newMin, mCoordinate);
|
407 |
data[i][EditRouteCalibrationTableModel.COLUMN_MCOORDINATE]=newMCoordinate; |
408 |
} |
409 |
} |
410 |
tableModel.setData(data); |
411 |
this.tblLayerInfo.setModel(tableModel);
|
412 |
this.tblLayerInfo.invalidate();
|
413 |
this.tblLayerInfo.repaint();
|
414 |
} |
415 |
|
416 |
|
417 |
private double calculateNewM(double oldMax, double oldMin, double newMax, double newMin, double mCoordinate){ |
418 |
double result;
|
419 |
if (oldMax==oldMin){
|
420 |
return Double.POSITIVE_INFINITY; |
421 |
} |
422 |
result=((newMax-newMin)*(mCoordinate-oldMin)/(oldMax-oldMin))+newMin; |
423 |
|
424 |
return result;
|
425 |
} |
426 |
|
427 |
|
428 |
private void cellUpdated(int row){ |
429 |
EditRouteCalibrationTableModel tableModel=(EditRouteCalibrationTableModel)tblLayerInfo.getModel(); |
430 |
Object[][] data=tableModel.getData(); |
431 |
Double newValue=tableModel.getValueUpdated();
|
432 |
|
433 |
data[row][EditRouteCalibrationTableModel.COLUMN_MCOORDINATE]=newValue; |
434 |
|
435 |
tableModel.setData(data); |
436 |
this.tblLayerInfo.setModel(tableModel);
|
437 |
this.tblLayerInfo.invalidate();
|
438 |
this.tblLayerInfo.repaint();
|
439 |
} |
440 |
|
441 |
private String initCmbRoute() { |
442 |
final List<String> routeNames=new ArrayList<String>(); |
443 |
try {
|
444 |
selectedFeatures.accept(new Visitor() {
|
445 |
|
446 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
447 |
if(obj instanceof Feature){ |
448 |
Feature feature=(Feature)obj; |
449 |
if (feature.get(fieldIdRoute.getName()) instanceof String){ |
450 |
String routeName=(String)feature.get(fieldIdRoute.getName()); |
451 |
if (!routeNames.contains(routeName)){
|
452 |
routeNames.add(routeName); |
453 |
} |
454 |
} |
455 |
} |
456 |
} |
457 |
}); |
458 |
} catch (BaseException e1) {
|
459 |
logger.error("Impossible to obtain route names to fill ComboBox",e1);
|
460 |
} |
461 |
|
462 |
String[] routeNamesArray=routeNames.toArray(new String[0]); |
463 |
ComboBoxModel<String> routeModel = |
464 |
new DefaultComboBoxModel<String>(routeNamesArray); |
465 |
cmbRoute.setModel(routeModel); |
466 |
|
467 |
cmbRoute.addItemListener(new ItemListener() { |
468 |
|
469 |
public void itemStateChanged(ItemEvent event) { |
470 |
if (event.getStateChange() == ItemEvent.SELECTED) { |
471 |
String item = (String) event.getItem(); |
472 |
routeChanged((String)item);
|
473 |
} |
474 |
} |
475 |
}); |
476 |
return (String)cmbRoute.getSelectedItem(); |
477 |
} |
478 |
|
479 |
|
480 |
private void routeChanged(String selectedRouteName) { |
481 |
|
482 |
final String routeName = selectedRouteName; |
483 |
|
484 |
DisposableIterator it = null;
|
485 |
editedFeature = null;
|
486 |
try {
|
487 |
it = selectedFeatures.fastIterator(); |
488 |
while (it.hasNext() && editedFeature == null) { |
489 |
Feature feature = (Feature) it.next(); |
490 |
if (feature.get(fieldIdRoute.getName()) instanceof String |
491 |
&& routeName.equals((String) feature.get(fieldIdRoute.getName()))) {
|
492 |
editedFeature = feature.getCopy(); |
493 |
} |
494 |
} |
495 |
} catch (DataException e) {
|
496 |
logger.error("Error getting table info", e);
|
497 |
} finally {
|
498 |
if (it != null) { |
499 |
it.dispose(); |
500 |
} |
501 |
} |
502 |
reloadTableModel(); |
503 |
reloadMaxMin((EditRouteCalibrationTableModel) tblLayerInfo.getModel()); |
504 |
|
505 |
graphics.removeGraphics(EDIT_ROUTECALIBRATION_GRAPHICS_ID); |
506 |
showEditingRoute(); |
507 |
mapContext.invalidate(); |
508 |
|
509 |
} |
510 |
|
511 |
/**
|
512 |
*
|
513 |
*/
|
514 |
private void showEditingRoute() { |
515 |
if (editedFeature != null) { |
516 |
Geometry geom = editedFeature.getDefaultGeometry().cloneGeometry(); |
517 |
ICoordTrans ct = layer.getCoordTrans(); |
518 |
geom.reProject(ct); |
519 |
graphics.addGraphic(EDIT_ROUTECALIBRATION_GRAPHICS_ID, geom, idLineSymbol); |
520 |
} |
521 |
} |
522 |
|
523 |
private void reloadTableModel(){ |
524 |
EditRouteCalibrationTableModel tableModel; |
525 |
tableModel =null;
|
526 |
try {
|
527 |
if (editedFeature!=null){ |
528 |
tableModel = new EditRouteCalibrationTableModel(editedFeature,fieldIdRoute);
|
529 |
tableModel.addTableModelListener(new TableModelListener() { |
530 |
|
531 |
public void tableChanged(TableModelEvent e) { |
532 |
cellUpdated(e.getFirstRow()); |
533 |
} |
534 |
}); |
535 |
} |
536 |
} catch (BaseException e) {
|
537 |
logger.error("Error getting table info",e);
|
538 |
} |
539 |
|
540 |
tblLayerInfo.setModel(tableModel); |
541 |
this.tblLayerInfo.invalidate();
|
542 |
} |
543 |
|
544 |
private void doClose() { |
545 |
graphics.removeGraphics(EDIT_ROUTECALIBRATION_GRAPHICS_ID); |
546 |
mapContext.invalidate(); |
547 |
this.setVisible(false); |
548 |
} |
549 |
|
550 |
public void close(){ |
551 |
this.doClose();
|
552 |
} |
553 |
|
554 |
private void validateData(){ |
555 |
hasWarnings=true;
|
556 |
EditRouteCalibrationTableModel tableModel=(EditRouteCalibrationTableModel)tblLayerInfo.getModel(); |
557 |
Object[][] data=tableModel.getData(); |
558 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
559 |
warnings=new ArrayList<String>(); |
560 |
|
561 |
if (data==null){ |
562 |
warnings.add(i18nManager.getTranslation("warn_Not_data_found_in_table"));
|
563 |
return;
|
564 |
} |
565 |
if (data.length<2){ |
566 |
warnings.add(i18nManager.getTranslation("warn_Not_enough_data_found_in_table"));
|
567 |
} |
568 |
for (int i=0;data.length-1>=i;i++){ |
569 |
Double mCoordinate=(Double)data[i][EditRouteCalibrationTableModel.COLUMN_MCOORDINATE]; |
570 |
if(mCoordinate.compareTo(new Double(0))<0){ |
571 |
warnings.add(MessageFormat.format(i18nManager.getTranslation("warn_Negative_value_at_row"), i+1)); |
572 |
} |
573 |
if(i>0 && i+1<data.length-1){ |
574 |
Double preMCoordinate=(Double)data[i-1][EditRouteCalibrationTableModel.COLUMN_MCOORDINATE]; |
575 |
Double postMCoordinate=(Double)data[i+1][EditRouteCalibrationTableModel.COLUMN_MCOORDINATE]; |
576 |
boolean orderedUpwardsPre=mCoordinate.compareTo(preMCoordinate)>=0; |
577 |
boolean orderedUpwardsPost=postMCoordinate.compareTo(mCoordinate)>=0; |
578 |
//if both,pre and post, are ordered upwards or downwards, it is correct
|
579 |
if(orderedUpwardsPre!=orderedUpwardsPost){
|
580 |
warnings.add(MessageFormat.format(i18nManager.getTranslation("warn_Out_of_sequence_value_at_row"), i+1)); |
581 |
} |
582 |
} |
583 |
|
584 |
for (int j=i+1;data.length-2>=j;j++){ |
585 |
Double auxMCoordinate=(Double)data[j][EditRouteCalibrationTableModel.COLUMN_MCOORDINATE]; |
586 |
if(mCoordinate.equals(auxMCoordinate)){
|
587 |
warnings.add(MessageFormat.format(i18nManager.getTranslation("warn_Repeated_value_at_row"), i+1,j+1)); |
588 |
} |
589 |
} |
590 |
} |
591 |
|
592 |
if(warnings.size()==0){ |
593 |
hasWarnings=false;
|
594 |
} |
595 |
} |
596 |
|
597 |
public boolean hasWarnings(){ |
598 |
return this.hasWarnings; |
599 |
} |
600 |
|
601 |
public List<String> getWarnings(){ |
602 |
return this.warnings; |
603 |
} |
604 |
|
605 |
public boolean isCanceled() { |
606 |
return this.canceled; |
607 |
} |
608 |
|
609 |
public void setVisibleAceptCancel(boolean visible) { |
610 |
this.btnAccept.setVisible(visible);
|
611 |
this.btnCancel.setVisible(visible);
|
612 |
} |
613 |
|
614 |
public boolean getVisibleAceptCancel() { |
615 |
return this.btnAccept.isVisible(); |
616 |
} |
617 |
|
618 |
public JComponent asJComponent() { |
619 |
return this; |
620 |
} |
621 |
|
622 |
public LrsAlgorithmParams getParams()
|
623 |
throws LrsGettingParametersException, LrsNeededParameterException {
|
624 |
LrsAlgorithmsManager manager = |
625 |
LrsAlgorithmsLocator.getLrsAlgorithmsManager(); |
626 |
LrsEditRouteCalibrationAlgorithmParams params = |
627 |
manager.createLrsEditRouteCalibrationAlgorithmParams(); |
628 |
// Recorrer todo del interfaz y rellenar los parametros
|
629 |
|
630 |
params.setIdRouteField(fieldIdRoute); |
631 |
params.setSelectedRouteName((String)cmbRoute.getSelectedItem());
|
632 |
|
633 |
EditRouteCalibrationTableModel tableModel=(EditRouteCalibrationTableModel)tblLayerInfo.getModel(); |
634 |
Object[][] data=tableModel.getData(); |
635 |
Geometry cloned=editedFeature.getDefaultGeometry().cloneGeometry(); |
636 |
int rowNumber=0; |
637 |
if (cloned.getGeometryType().isTypeOf(Geometry.TYPES.LINE)){
|
638 |
Line line=(Line) cloned; |
639 |
for (int i=0;i<line.getNumVertices();i++){ |
640 |
Double newMvalue=(Double)data[rowNumber][4]; |
641 |
line.setCoordinateAt(i, line.getDimension()-1, newMvalue);
|
642 |
rowNumber++; |
643 |
} |
644 |
} |
645 |
if (cloned.getGeometryType().isTypeOf(Geometry.TYPES.MULTILINE)){
|
646 |
MultiLine multiline=(MultiLine) cloned; |
647 |
for (int i=0;i<multiline.getPrimitivesNumber();i++){ |
648 |
Line line=(Line)multiline.getPrimitiveAt(i); |
649 |
for (int j=0;j<line.getNumVertices();j++){ |
650 |
Double newMvalue=(Double)data[rowNumber][4]; |
651 |
line.setCoordinateAt(j, line.getDimension()-1, newMvalue);
|
652 |
rowNumber++; |
653 |
} |
654 |
} |
655 |
} |
656 |
|
657 |
params.setModifiedGeometry(cloned); |
658 |
|
659 |
return params;
|
660 |
} |
661 |
|
662 |
public void setParams(LrsAlgorithmParams params) { |
663 |
if (params == null) { |
664 |
clear(); |
665 |
return;
|
666 |
} |
667 |
// Rellena los campos con los valores de params
|
668 |
LrsEditRouteCalibrationAlgorithmParams parameters = |
669 |
(LrsEditRouteCalibrationAlgorithmParams) params; |
670 |
|
671 |
if (parameters.getSelectedRouteName()!=null){ |
672 |
this.cmbRoute.setSelectedItem(parameters.getSelectedRouteName());
|
673 |
} |
674 |
if (parameters.getIdRouteField()!=null && |
675 |
parameters.getIdRouteField() instanceof FeatureAttributeDescriptor){
|
676 |
this.fieldIdRoute=parameters.getIdRouteField();
|
677 |
} |
678 |
if (parameters.getModifiedGeometry()!=null && |
679 |
parameters.getModifiedGeometry() instanceof Feature){
|
680 |
this.editedFeature=(Feature)parameters.getModifiedGeometry();
|
681 |
} |
682 |
} |
683 |
|
684 |
public void clear() { |
685 |
if (cmbRoute!=null){ |
686 |
this.cmbRoute.setSelectedItem(0); |
687 |
} |
688 |
} |
689 |
|
690 |
|
691 |
/**
|
692 |
* Returns true if selectedIdRoute is contained in layer
|
693 |
* @param layer
|
694 |
* @param selectedIdRoute
|
695 |
* @return
|
696 |
*/
|
697 |
private static boolean isCorrectIdRoute(FLayer layer, FeatureAttributeDescriptor selectedIdRoute){ |
698 |
// Validates is FLyrVect
|
699 |
FLyrVect lyrVect = null;
|
700 |
if (layer instanceof FLyrVect) { |
701 |
lyrVect = (FLyrVect) layer; |
702 |
} else
|
703 |
return false; |
704 |
try {
|
705 |
if (lyrVect != null) { |
706 |
FeatureStore featureStore = lyrVect.getFeatureStore(); |
707 |
try {
|
708 |
FeatureType type = featureStore.getDefaultFeatureType(); |
709 |
FeatureAttributeDescriptor[] attributeDescriptors =
|
710 |
type.getAttributeDescriptors(); |
711 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
712 |
FeatureAttributeDescriptor featureAttributeDescriptor = |
713 |
attributeDescriptors[i]; |
714 |
if(featureAttributeDescriptor.equals(selectedIdRoute)){
|
715 |
logger.debug("SelectedIdRoute founded in layer");
|
716 |
return true; |
717 |
} |
718 |
} |
719 |
} catch (DataException e1) {
|
720 |
logger.warn(new StringBuilder() |
721 |
.append( |
722 |
"Error getting default feature type from layer ")
|
723 |
.append(lyrVect.getName()).toString(), e1); |
724 |
} |
725 |
} |
726 |
} catch (Exception e) { |
727 |
logger.error("Can't get linear vector layers", e);
|
728 |
} |
729 |
return false; |
730 |
} |
731 |
|
732 |
private static FeatureSet getSelectedFeatures(FLayer layer) { |
733 |
FLyrVect lyrVect = null;
|
734 |
if (layer instanceof FLyrVect) { |
735 |
lyrVect = (FLyrVect) layer; |
736 |
} else
|
737 |
return null; |
738 |
FeatureStore featureStore=lyrVect.getFeatureStore(); |
739 |
if(featureStore!=null){ |
740 |
try {
|
741 |
FeatureSelection selectedFeatures= featureStore.getFeatureSelection(); |
742 |
if (selectedFeatures!=null && !selectedFeatures.isEmpty()){ |
743 |
return selectedFeatures;
|
744 |
}else{
|
745 |
//If there is not selected features, all features are used
|
746 |
return featureStore.getFeatureSet();
|
747 |
} |
748 |
} catch (DataException e) {
|
749 |
logger.warn("Can't get selected features", e);
|
750 |
} |
751 |
} |
752 |
return null; |
753 |
} |
754 |
|
755 |
} |