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