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