svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / EditionManager.java @ 40557
History | View | Annotate | Download (28.7 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.editing; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.ScrollPane; |
29 |
import java.util.ArrayList; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
|
33 |
import javax.swing.JOptionPane; |
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.SwingUtilities; |
36 |
|
37 |
import org.gvsig.andami.PluginServices; |
38 |
import org.gvsig.andami.messages.NotificationManager; |
39 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
40 |
import org.gvsig.app.ApplicationLocator; |
41 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
42 |
import org.gvsig.editing.layers.FactoryLayerEdited; |
43 |
import org.gvsig.editing.layers.ILayerEdited; |
44 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
45 |
import org.gvsig.fmap.dal.exception.DataException; |
46 |
import org.gvsig.fmap.dal.exception.DataRuntimeException; |
47 |
import org.gvsig.fmap.dal.exception.ReadException; |
48 |
import org.gvsig.fmap.dal.exception.WriteException; |
49 |
import org.gvsig.fmap.dal.feature.AbstractFeatureRule; |
50 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
51 |
import org.gvsig.fmap.dal.feature.Feature; |
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
53 |
import org.gvsig.fmap.dal.feature.FeatureRule; |
54 |
import org.gvsig.fmap.dal.feature.FeatureRules; |
55 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
56 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
57 |
import org.gvsig.fmap.dal.feature.FeatureType; |
58 |
import org.gvsig.fmap.dal.feature.rule.CheckMandatoryAttributesRule; |
59 |
import org.gvsig.fmap.geom.Geometry; |
60 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
61 |
import org.gvsig.fmap.geom.GeometryLocator; |
62 |
import org.gvsig.fmap.geom.GeometryManager; |
63 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
64 |
import org.gvsig.fmap.geom.primitive.Envelope; |
65 |
import org.gvsig.fmap.mapcontext.exceptions.CancelEditingLayerException; |
66 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
67 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
68 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
69 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener; |
70 |
import org.gvsig.fmap.mapcontext.layers.LayerEvent; |
71 |
import org.gvsig.fmap.mapcontext.layers.LayerListener; |
72 |
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent; |
73 |
import org.gvsig.fmap.mapcontext.layers.SpatialCache; |
74 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
75 |
import org.gvsig.fmap.mapcontrol.MapControl; |
76 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
77 |
import org.gvsig.fmap.mapcontrol.MapControlManager; |
78 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
79 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
80 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
81 |
import org.gvsig.tools.observer.ObservableHelper; |
82 |
import org.gvsig.tools.observer.Observer; |
83 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
84 |
import org.gvsig.tools.swing.api.dynobject.DynObjectSwingManager; |
85 |
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent; |
86 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
87 |
import org.slf4j.Logger; |
88 |
import org.slf4j.LoggerFactory; |
89 |
|
90 |
public class EditionManager implements LayerListener, LayerCollectionListener, IEditionManager { |
91 |
|
92 |
private static final Logger LOG = LoggerFactory |
93 |
.getLogger(EditionManager.class); |
94 |
|
95 |
private List<ILayerEdited> editedLayers = new ArrayList<ILayerEdited>(); |
96 |
private MapControl mapCtrl = null; |
97 |
private ILayerEdited ile = null; |
98 |
private ObservableHelper observableHelper = new ObservableHelper(); |
99 |
private GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
100 |
private MapControlManager mapControlManager = MapControlLocator.getMapControlManager();
|
101 |
|
102 |
/* (non-Javadoc)
|
103 |
* @see org.gvsig.editing.IEditionManager#getLayerEdited(org.gvsig.fmap.mapcontext.layers.FLayer)
|
104 |
*/
|
105 |
public ILayerEdited getLayerEdited(FLayer lyr) {
|
106 |
ILayerEdited aux = null;
|
107 |
for (int i = 0; i < editedLayers.size(); i++) { |
108 |
aux = editedLayers.get(i); |
109 |
if (aux.getLayer() == lyr)
|
110 |
return aux;
|
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
/* (non-Javadoc)
|
116 |
* @see org.gvsig.editing.IEditionManager#editLayer(org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect, org.gvsig.app.project.documents.view.gui.DefaultViewPanel)
|
117 |
*/
|
118 |
public void editLayer(FLyrVect lv, DefaultViewPanel vista) |
119 |
throws DataException {
|
120 |
|
121 |
notifyObservers(EditionNotifycation.BEFORE_ENTER_EDIT_LAYER, lv, null, null); |
122 |
|
123 |
|
124 |
CADExtension.initFocus(); |
125 |
vista.showConsole(); |
126 |
MapControl mapControl = vista.getMapControl(); |
127 |
this.setMapControl(mapControl);
|
128 |
|
129 |
lv.addLayerListener(this);
|
130 |
|
131 |
// This line also enables the spatial cache for snapping:
|
132 |
lv.getFeatureStore().edit(); |
133 |
lv.getFeatureStore().addObserver(vista); |
134 |
|
135 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
136 |
|
137 |
lv.getFeatureStore().addObserver(mapControl); |
138 |
StartEditing.startCommandsApplicable(vista, lv); |
139 |
|
140 |
// vista.repaintMap();
|
141 |
// ----------------------------
|
142 |
// this forces a true repaint:
|
143 |
lv.drawValueChanged(LayerEvent.createDrawValuesChangedEvent(lv, ""));
|
144 |
|
145 |
notifyObservers(EditionNotifycation.AFTER_ENTER_EDIT_LAYER, lv, null, null); |
146 |
} |
147 |
|
148 |
public void visibilityChanged(LayerEvent e) { |
149 |
} |
150 |
|
151 |
public void activationChanged(LayerEvent e) { |
152 |
if (e.getSource().isActive()) {
|
153 |
ile = getLayerEdited(e.getSource()); |
154 |
} |
155 |
if (ile == null || ile.getLayer().equals(e.getSource())) { |
156 |
|
157 |
if (ile != null && !ile.getLayer().isActive()) { |
158 |
VectorialLayerEdited lastVLE = (VectorialLayerEdited) ile; |
159 |
lastVLE.activationLost(e); |
160 |
} |
161 |
if (e.getSource() instanceof FLyrVect) { |
162 |
VectorialLayerEdited vle = null;
|
163 |
vle = (VectorialLayerEdited) getLayerEdited(e.getSource()); |
164 |
ile = vle; |
165 |
if (getMapControl() != null && vle != null |
166 |
&& vle.getLayer().isActive()) { |
167 |
getMapControl().setTool("cadtooladapter");
|
168 |
vle.activationGained(e); |
169 |
return;
|
170 |
} |
171 |
} |
172 |
if (getMapControl() != null) { |
173 |
getMapControl().setTool("zoomIn");
|
174 |
PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
|
175 |
} |
176 |
} |
177 |
} |
178 |
|
179 |
public void nameChanged(LayerEvent e) { |
180 |
} |
181 |
|
182 |
public void editionChanged(LayerEvent e) { |
183 |
ILayerEdited lyrEdit = getLayerEdited(e.getSource()); |
184 |
|
185 |
// Si no est? en la lista, comprobamos que est? en edici?n
|
186 |
// y lo a?adimos
|
187 |
if ((lyrEdit == null) && e.getSource().isEditing()) { |
188 |
lyrEdit = |
189 |
FactoryLayerEdited.createLayerEdited(e.getSource(), mapCtrl); |
190 |
editedLayers.add(lyrEdit); |
191 |
if (getMapControl() != null) { |
192 |
getMapControl().setTool("cadtooladapter");
|
193 |
CADExtension.setCADTool("_selection", true); |
194 |
} |
195 |
PluginServices.getMainFrame().setSelectedTool("_selection");
|
196 |
// idActiveLayer = editedLayers.size() - 1;
|
197 |
ile = getLayerEdited(e.getSource()); |
198 |
LOG.debug("NUEVA CAPA EN EDICION: {}", lyrEdit.getLayer().getName());
|
199 |
|
200 |
// Ponemos el resto de temas desactivados
|
201 |
if (mapCtrl != null) |
202 |
mapCtrl.getMapContext().getLayers().setActive(false);
|
203 |
// y activamos el nuevo.
|
204 |
e.getSource().setActive(true);
|
205 |
|
206 |
if (e.getSource() instanceof FLyrVect) { |
207 |
FLyrVect fLyrVect = (FLyrVect) e.getSource(); |
208 |
((VectorialLayerEdited) lyrEdit) |
209 |
.setEditionChangeManager(new EditionChangeManager(fLyrVect));
|
210 |
|
211 |
addCheckRules(fLyrVect); |
212 |
} |
213 |
} else {
|
214 |
for (int i = 0; i < editedLayers.size(); i++) { |
215 |
VectorialLayerEdited vle = |
216 |
(VectorialLayerEdited) editedLayers.get(i); |
217 |
if (vle.equals(lyrEdit)) {
|
218 |
editedLayers.remove(i); |
219 |
ile = null;
|
220 |
return;
|
221 |
} |
222 |
} |
223 |
} |
224 |
|
225 |
} |
226 |
|
227 |
/* (non-Javadoc)
|
228 |
* @see org.gvsig.editing.IEditionManager#getActiveLayerEdited()
|
229 |
*/
|
230 |
public ILayerEdited getActiveLayerEdited() {
|
231 |
return ile;
|
232 |
} |
233 |
|
234 |
/* (non-Javadoc)
|
235 |
* @see org.gvsig.editing.IEditionManager#getMapControl()
|
236 |
*/
|
237 |
public MapControl getMapControl() {
|
238 |
return mapCtrl;
|
239 |
} |
240 |
|
241 |
/* (non-Javadoc)
|
242 |
* @see org.gvsig.editing.IEditionManager#setMapControl(org.gvsig.fmap.mapcontrol.MapControl)
|
243 |
*/
|
244 |
public void setMapControl(MapControl mapCtrl) { |
245 |
if (mapCtrl != null) { |
246 |
this.mapCtrl = mapCtrl;
|
247 |
mapCtrl.getMapContext().getLayers().addLayerListener(this);
|
248 |
mapCtrl.getMapContext().getLayers() |
249 |
.addLayerCollectionListener(this);
|
250 |
} |
251 |
} |
252 |
|
253 |
public void layerAdded(LayerCollectionEvent e) { |
254 |
// Nothing to do
|
255 |
|
256 |
} |
257 |
|
258 |
public void layerMoved(LayerPositionEvent e) { |
259 |
// Nothing to do
|
260 |
} |
261 |
|
262 |
public void layerRemoved(LayerCollectionEvent e) { |
263 |
VectorialLayerEdited vle = |
264 |
(VectorialLayerEdited) getActiveLayerEdited(); |
265 |
if (vle != null && vle.getLayer().isActive()) { |
266 |
try {
|
267 |
vle.clearSelection(); |
268 |
} catch (DataException e1) {
|
269 |
NotificationManager.addError(e1); |
270 |
} |
271 |
editedLayers.remove(vle); |
272 |
getMapControl().setTool("zoomIn");
|
273 |
FLyrVect lv = (FLyrVect) vle.getLayer(); |
274 |
if (e.getAffectedLayer().equals(lv)) {
|
275 |
IWindow window = |
276 |
PluginServices.getMDIManager().getActiveWindow(); |
277 |
if (window instanceof DefaultViewPanel) { |
278 |
DefaultViewPanel view = (DefaultViewPanel) window; |
279 |
view.hideConsole(); |
280 |
view.validate(); |
281 |
view.repaint(); |
282 |
} |
283 |
} |
284 |
} |
285 |
PluginServices.getMainFrame().enableControls(); |
286 |
} |
287 |
|
288 |
public void layerAdding(LayerCollectionEvent e) throws CancelationException { |
289 |
// Nothing to do
|
290 |
} |
291 |
|
292 |
public void layerMoving(LayerPositionEvent e) throws CancelationException { |
293 |
// Nothing to do
|
294 |
} |
295 |
|
296 |
public void layerRemoving(LayerCollectionEvent e) |
297 |
throws CancelationException {
|
298 |
// Nothing to do
|
299 |
} |
300 |
|
301 |
public void activationChanged(LayerCollectionEvent e) |
302 |
throws CancelationException {
|
303 |
// Nothing to do
|
304 |
} |
305 |
|
306 |
public void visibilityChanged(LayerCollectionEvent e) |
307 |
throws CancelationException {
|
308 |
// Nothing to do
|
309 |
} |
310 |
|
311 |
public void drawValueChanged(LayerEvent e) { |
312 |
// Nothing to do
|
313 |
} |
314 |
|
315 |
public class DefaultCheckMandatoryAttributes extends AbstractFeatureRule |
316 |
implements CheckMandatoryAttributesRule {
|
317 |
|
318 |
protected DefaultCheckMandatoryAttributes() {
|
319 |
super("CheckMandatoryAttributtes", "CheckMandatoryAttributtes"); |
320 |
} |
321 |
|
322 |
public void validate(Feature feature, FeatureStore featureStore) |
323 |
throws DataException {
|
324 |
// Comprobar si hay campos obligatorios y presentar formulario
|
325 |
FeatureType featureType = feature.getType(); |
326 |
FeatureAttributeDescriptor[] attributeDescriptors =
|
327 |
featureType.getAttributeDescriptors(); |
328 |
boolean showForm = false; |
329 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
330 |
FeatureAttributeDescriptor attrDesc = attributeDescriptors[i]; |
331 |
if (((attrDesc.isPrimaryKey() && !attrDesc.isAutomatic())|| !attrDesc.allowNull())
|
332 |
&& feature.get(attrDesc.getName()) == null) {
|
333 |
showForm = true;
|
334 |
break;
|
335 |
} |
336 |
} |
337 |
if (showForm) {
|
338 |
askRequiredAttributtes((EditableFeature) feature, featureStore); |
339 |
} |
340 |
} |
341 |
|
342 |
} |
343 |
|
344 |
private void askRequiredAttributtes(final EditableFeature feature, |
345 |
final FeatureStore store)
|
346 |
throws FeatureRuleMandatoryAttributesException {
|
347 |
|
348 |
// Comprobar que estamos en el thread de swing y si no excepcion
|
349 |
if (!SwingUtilities.isEventDispatchThread()) { |
350 |
try {
|
351 |
SwingUtilities.invokeAndWait(new Runnable() { |
352 |
|
353 |
public void run() { |
354 |
askRequiredAttributtes(feature, store); |
355 |
} |
356 |
}); |
357 |
} catch (Exception e1) { |
358 |
throw new FeatureRuleMandatoryAttributesException(e1, |
359 |
store.getFullName()); |
360 |
} |
361 |
return;
|
362 |
} |
363 |
DynObjectSwingManager dynObjectSwinManager = |
364 |
ToolsSwingLocator.getDynObjectSwingManager(); |
365 |
final JDynObjectComponent dynObjectComponent;
|
366 |
try {
|
367 |
dynObjectComponent = |
368 |
dynObjectSwinManager.createJDynObjectComponent( |
369 |
feature.getAsDynObject(), true);
|
370 |
} catch (Exception e) { |
371 |
|
372 |
// TODO This has to be improved
|
373 |
// Instead of this warning, show dialog to
|
374 |
// fill feature type
|
375 |
showBadValuesMessage(e.getMessage()); |
376 |
return;
|
377 |
|
378 |
} |
379 |
final JPanel panel = new JPanel(); |
380 |
|
381 |
ButtonsPanel buttonsPanel = |
382 |
new ButtonsPanel(ButtonsPanel.BUTTONS_ACCEPTCANCEL);
|
383 |
buttonsPanel.addButtonPressedListener(new ButtonsPanelListener() {
|
384 |
|
385 |
public void actionButtonPressed(ButtonsPanelEvent e) { |
386 |
if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
|
387 |
dynObjectComponent.saveStatus(); |
388 |
panel.setVisible(false);
|
389 |
} |
390 |
if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
|
391 |
panel.setVisible(false);
|
392 |
throw new FeatureRuleMandatoryAttributesException(store |
393 |
.getFullName()); |
394 |
} |
395 |
} |
396 |
|
397 |
}); |
398 |
panel.setLayout(new BorderLayout()); |
399 |
ScrollPane scrollPanel = new ScrollPane(); |
400 |
scrollPanel.add(dynObjectComponent.asJComponent(), BorderLayout.CENTER);
|
401 |
panel.add(scrollPanel); |
402 |
panel.add(buttonsPanel, BorderLayout.SOUTH);
|
403 |
panel.setPreferredSize(new Dimension(800, 600)); |
404 |
WindowManager wm = ToolsSwingLocator.getWindowManager(); |
405 |
wm.showWindow(panel, "Fill mandatory fields", WindowManager.MODE.DIALOG);
|
406 |
|
407 |
} |
408 |
|
409 |
/**
|
410 |
* @param message
|
411 |
*/
|
412 |
private void showBadValuesMessage(String message) { |
413 |
|
414 |
ApplicationLocator.getManager().message( |
415 |
message, |
416 |
JOptionPane.WARNING_MESSAGE);
|
417 |
|
418 |
/*
|
419 |
* We cannot do this because window focus
|
420 |
* causes exception in drawing
|
421 |
*
|
422 |
IWindow iw = PluginServices.getMDIManager().getActiveWindow();
|
423 |
Component pare = null;
|
424 |
if (iw instanceof Component) {
|
425 |
pare = (Component) iw;
|
426 |
}
|
427 |
|
428 |
String _tit = Messages.getText("editing");
|
429 |
String _msg = Messages.getText("_Invalid_values_in_some_fields_must_be_edited_before_end_editing");
|
430 |
_msg = _msg + "\n\n[" + message + "]";
|
431 |
|
432 |
JOptionPane.showMessageDialog(
|
433 |
pare,
|
434 |
_msg, _tit,
|
435 |
JOptionPane.WARNING_MESSAGE);
|
436 |
*/
|
437 |
} |
438 |
|
439 |
private void addCheckRules(FLyrVect layer) { |
440 |
try {
|
441 |
FeatureType featureType = |
442 |
layer.getFeatureStore().getDefaultFeatureType(); |
443 |
FeatureRules featureRules = featureType.getRules(); |
444 |
for (Iterator it = featureRules.iterator(); it.hasNext();) { |
445 |
FeatureRule rule = (FeatureRule) it.next(); |
446 |
if (rule instanceof CheckMandatoryAttributesRule) { |
447 |
return;
|
448 |
} |
449 |
} |
450 |
featureRules.add(new DefaultCheckMandatoryAttributes());
|
451 |
} catch (DataException e) {
|
452 |
throw new RuntimeException(e); |
453 |
} |
454 |
} |
455 |
|
456 |
public class FeatureRuleMandatoryAttributesException extends |
457 |
DataRuntimeException { |
458 |
|
459 |
private static final long serialVersionUID = -7965533441336164612L; |
460 |
private final static String MESSAGE_FORMAT = |
461 |
"Can't apply rule in store %(store)s.";
|
462 |
private final static String MESSAGE_KEY = |
463 |
"_FeatureRuleMandatoryAttributesException";
|
464 |
|
465 |
public FeatureRuleMandatoryAttributesException(Throwable cause, |
466 |
String store) {
|
467 |
super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
468 |
this.setValue("store", store); |
469 |
} |
470 |
|
471 |
public FeatureRuleMandatoryAttributesException(String store) { |
472 |
super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
|
473 |
this.setValue("store", store); |
474 |
} |
475 |
} |
476 |
|
477 |
private VectorialLayerEdited getVLE() {
|
478 |
return (VectorialLayerEdited) getActiveLayerEdited();
|
479 |
} |
480 |
|
481 |
private void insertSpatialCache(Geometry geom) |
482 |
throws CreateEnvelopeException {
|
483 |
VectorialLayerEdited vle = getVLE(); |
484 |
SpatialCache spatialCache = ((FLyrVect) vle.getLayer()) |
485 |
.getSpatialCache(); |
486 |
Envelope r = geom.getEnvelope(); |
487 |
if (geom.getType() == Geometry.TYPES.POINT) {
|
488 |
r = geomManager.createEnvelope(r.getMinimum(0), r.getMinimum(1), |
489 |
r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D); |
490 |
} |
491 |
spatialCache.insert(r, geom); |
492 |
|
493 |
} |
494 |
|
495 |
private void updateSpatialCache(Geometry geom) { |
496 |
// TODO: no se actualizaban la cache espacial cuando se modifican las geometrias,
|
497 |
// habria que hacerlo.
|
498 |
LOG.info("TODO: need update spatial cache");
|
499 |
} |
500 |
|
501 |
private void removeSpatialCache(Geometry geom) { |
502 |
// TODO: no se actualizaban la cache espacial cuando se modifican las geometrias,
|
503 |
// habria que hacerlo.
|
504 |
LOG.info("TODO: need update spatial cache");
|
505 |
} |
506 |
|
507 |
private void draw(Geometry geometry, Feature feature) { |
508 |
if (geometry != null) { |
509 |
getMapControl().getMapControlDrawer() |
510 |
.draw(geometry, mapControlManager.getGeometrySelectionSymbol()); |
511 |
} |
512 |
} |
513 |
|
514 |
public Feature insertGeometry(Geometry geometry) {
|
515 |
VectorialLayerEdited vle = getVLE(); |
516 |
try {
|
517 |
FeatureStore featureStore = |
518 |
((FLyrVect) vle.getLayer()).getFeatureStore(); |
519 |
EditableFeature eFeature = featureStore.createNewFeature(true);
|
520 |
|
521 |
//Reproject the geometry
|
522 |
Geometry insertedGeometry = geometry; |
523 |
if (getVLE().getLayer().getCoordTrans() != null){ |
524 |
insertedGeometry = insertedGeometry.cloneGeometry(); |
525 |
insertedGeometry.reProject(getVLE().getLayer().getCoordTrans().getInverted()); |
526 |
} |
527 |
|
528 |
eFeature.setGeometry(featureStore.getDefaultFeatureType() |
529 |
.getDefaultGeometryAttributeName(), insertedGeometry); |
530 |
|
531 |
EditionNotifycation notification = notifyObservers(EditionNotifycation.BEFORE_INSERT_FEATURE, getVLE().getLayer(), featureStore, eFeature); |
532 |
if( notification.isProcessCanceled() ) {
|
533 |
return null; |
534 |
} |
535 |
|
536 |
featureStore.insert(eFeature); |
537 |
insertSpatialCache(insertedGeometry); |
538 |
draw(insertedGeometry, eFeature); |
539 |
|
540 |
notifyObservers(EditionNotifycation.AFTER_INSERT_FEATURE, vle.getLayer(), featureStore, eFeature); |
541 |
|
542 |
return eFeature;
|
543 |
} catch (Exception e) { |
544 |
NotificationManager.addError(e.getMessage(), e); |
545 |
return null; |
546 |
} |
547 |
} |
548 |
|
549 |
public Feature insertGeometry(Geometry geometry, Feature feature) {
|
550 |
VectorialLayerEdited vle = getVLE(); |
551 |
|
552 |
try {
|
553 |
FeatureStore featureStore = |
554 |
((FLyrVect) vle.getLayer()).getFeatureStore(); |
555 |
EditableFeature eFeature = |
556 |
featureStore.createNewFeature( |
557 |
featureStore.getDefaultFeatureType(), feature); |
558 |
eFeature.setGeometry(featureStore.getDefaultFeatureType() |
559 |
.getDefaultGeometryAttributeName(), geometry); |
560 |
|
561 |
EditionNotifycation notification = notifyObservers(EditionNotifycation.BEFORE_INSERT_FEATURE, getVLE().getLayer(), featureStore, feature); |
562 |
if( notification.isProcessCanceled() ) {
|
563 |
return null; |
564 |
} |
565 |
|
566 |
featureStore.insert(eFeature); |
567 |
insertSpatialCache(geometry); |
568 |
draw(geometry, eFeature); |
569 |
|
570 |
notifyObservers(EditionNotifycation.AFTER_INSERT_FEATURE, vle.getLayer(), featureStore, eFeature); |
571 |
|
572 |
return eFeature;
|
573 |
} catch (Exception e) { |
574 |
NotificationManager.addError(e.getMessage(), e); |
575 |
return null; |
576 |
} |
577 |
} |
578 |
|
579 |
|
580 |
public Feature insertFeature(Feature feature) {
|
581 |
VectorialLayerEdited vle = getVLE(); |
582 |
|
583 |
try {
|
584 |
|
585 |
FeatureStore featureStore = |
586 |
((FLyrVect) vle.getLayer()).getFeatureStore(); |
587 |
EditableFeature eFeature = |
588 |
featureStore.createNewFeature( |
589 |
featureStore.getDefaultFeatureType(), feature); |
590 |
Geometry geometry = feature.getDefaultGeometry(); |
591 |
|
592 |
EditionNotifycation notification = notifyObservers(EditionNotifycation.BEFORE_INSERT_FEATURE, getVLE().getLayer(), featureStore, feature); |
593 |
if( notification.isProcessCanceled() ) {
|
594 |
return null; |
595 |
} |
596 |
|
597 |
featureStore.insert(eFeature); |
598 |
insertSpatialCache(geometry); |
599 |
draw(geometry, eFeature); |
600 |
|
601 |
notifyObservers(EditionNotifycation.AFTER_INSERT_FEATURE, vle.getLayer(), featureStore, eFeature); |
602 |
|
603 |
return eFeature;
|
604 |
} catch (Exception e) { |
605 |
NotificationManager.addError(e.getMessage(), e); |
606 |
return null; |
607 |
} |
608 |
} |
609 |
|
610 |
public Feature insertAndSelectGeometry(String toolName, Geometry geometry) { |
611 |
Feature feature = null;
|
612 |
try {
|
613 |
FeatureStore featureStore = getVLE().getFeatureStore(); |
614 |
featureStore.beginComplexNotification(); |
615 |
featureStore.beginEditingGroup(toolName); |
616 |
FeatureSelection newSelection = |
617 |
featureStore.createFeatureSelection(); |
618 |
feature = insertGeometry(geometry); |
619 |
if( feature != null ) { |
620 |
newSelection.select(feature); |
621 |
featureStore.setSelection(newSelection); |
622 |
} |
623 |
featureStore.endEditingGroup(); |
624 |
featureStore.endComplexNotification(); |
625 |
|
626 |
} catch (DataException e) {
|
627 |
NotificationManager.showMessageError("insertAndSelectGeoemtry", e);
|
628 |
} |
629 |
return feature;
|
630 |
} |
631 |
|
632 |
|
633 |
public EditableFeature updateGeometry(FeatureStore store, Feature feature, Geometry geometry) {
|
634 |
try {
|
635 |
EditableFeature eFeature = feature.getEditable(); |
636 |
eFeature.setGeometry(feature.getType() |
637 |
.getDefaultGeometryAttributeName(), geometry); |
638 |
EditionNotifycation notification = notifyObservers(EditionNotifycation.BEFORE_UPDATE_FEATURE, getVLE().getLayer(), store, feature); |
639 |
if( notification.isProcessCanceled() ) {
|
640 |
return null; |
641 |
} |
642 |
store.update(eFeature); |
643 |
updateSpatialCache(geometry); |
644 |
notifyObservers(EditionNotifycation.AFTER_UPDATE_FEATURE, getVLE().getLayer(), store, eFeature); |
645 |
return eFeature;
|
646 |
} catch (Exception e) { |
647 |
NotificationManager.addError(e.getMessage(), e); |
648 |
return null; |
649 |
} |
650 |
|
651 |
} |
652 |
|
653 |
public EditableFeature updateFeature(FeatureStore store, Feature feature) {
|
654 |
try {
|
655 |
EditableFeature eFeature = feature.getEditable(); |
656 |
EditionNotifycation notification = notifyObservers(EditionNotifycation.BEFORE_UPDATE_FEATURE, getVLE().getLayer(), store, feature); |
657 |
if( notification.isProcessCanceled() ) {
|
658 |
return null; |
659 |
} |
660 |
store.update(eFeature); |
661 |
updateSpatialCache(feature.getDefaultGeometry()); |
662 |
notifyObservers(EditionNotifycation.AFTER_UPDATE_FEATURE, getVLE().getLayer(), store, eFeature); |
663 |
return eFeature;
|
664 |
} catch (Exception e) { |
665 |
NotificationManager.addError(e.getMessage(), e); |
666 |
return null; |
667 |
} |
668 |
|
669 |
} |
670 |
|
671 |
public void removeFeature(FeatureStore store, Feature feature) { |
672 |
try {
|
673 |
EditionNotifycation notification = notifyObservers(EditionNotifycation.BEFORE_REMOVE_FEATURE, getVLE().getLayer(), store, feature); |
674 |
if( notification.isProcessCanceled() ) {
|
675 |
return;
|
676 |
} |
677 |
store.delete(feature); |
678 |
removeSpatialCache(feature.getDefaultGeometry()); |
679 |
notifyObservers(EditionNotifycation.AFTER_REMOVE_FEATURE, getVLE().getLayer(), store, feature); |
680 |
} catch (Exception e) { |
681 |
NotificationManager.addError(e.getMessage(), e); |
682 |
} |
683 |
|
684 |
} |
685 |
|
686 |
public void addObserver(Observer o) { |
687 |
observableHelper.addObserver(o); |
688 |
} |
689 |
|
690 |
public void deleteObserver(Observer o) { |
691 |
observableHelper.deleteObserver(o); |
692 |
} |
693 |
|
694 |
public void deleteObservers() { |
695 |
observableHelper.deleteObservers(); |
696 |
} |
697 |
|
698 |
private void notifyObservers(EditionNotifycation notifycation) { |
699 |
observableHelper.notifyObservers(this, notifycation);
|
700 |
} |
701 |
|
702 |
private EditionNotifycation notifyObservers(int type, FLayer layer, |
703 |
FeatureStore store, Feature feature) { |
704 |
DefaultEditionNotifycation notifycation = new DefaultEditionNotifycation(
|
705 |
type, layer, store, feature); |
706 |
try {
|
707 |
observableHelper.notifyObservers(this, notifycation);
|
708 |
} catch (Exception ex) { |
709 |
LOG.info("Can't notify observers", ex);
|
710 |
} |
711 |
return notifycation;
|
712 |
} |
713 |
|
714 |
class DefaultEditionNotifycation implements EditionNotifycation { |
715 |
|
716 |
private int type; |
717 |
private FLayer layer;
|
718 |
private FeatureStore store;
|
719 |
private Feature feature;
|
720 |
private boolean processCanceled=false; |
721 |
|
722 |
DefaultEditionNotifycation(int type, FLayer layer, FeatureStore store, Feature feature) {
|
723 |
this.type = type;
|
724 |
this.layer = layer;
|
725 |
this.store = store;
|
726 |
this.feature = feature;
|
727 |
} |
728 |
|
729 |
public int getNotificationType() { |
730 |
return type;
|
731 |
} |
732 |
|
733 |
public FLyrVect getLayer() {
|
734 |
return (FLyrVect) layer;
|
735 |
} |
736 |
|
737 |
public FeatureStore getStore() {
|
738 |
return store;
|
739 |
} |
740 |
|
741 |
public Feature getFeature() {
|
742 |
return feature;
|
743 |
} |
744 |
|
745 |
public void cancelProcess() { |
746 |
processCanceled = true;
|
747 |
} |
748 |
|
749 |
public boolean isProcessCanceled() { |
750 |
return processCanceled;
|
751 |
} |
752 |
|
753 |
} |
754 |
|
755 |
|
756 |
public void stopEditLayer(DefaultViewPanel view, FLyrVect lv, int mode) |
757 |
throws CancelEditingLayerException, ReadException {
|
758 |
|
759 |
notifyObservers(EditionNotifycation.BEFORE_EXIT_EDIT_LAYER, lv, null, null); |
760 |
VectorialLayerEdited lyrEd = null;
|
761 |
FeatureStore featureStore = null;
|
762 |
|
763 |
switch (mode) {
|
764 |
|
765 |
case CANCEL_EDITING:
|
766 |
lyrEd = (VectorialLayerEdited) this.getActiveLayerEdited();
|
767 |
lv.getFeatureStore().deleteObserver(lyrEd); |
768 |
|
769 |
try {
|
770 |
featureStore = lv.getFeatureStore(); |
771 |
|
772 |
featureStore.cancelEditing(); |
773 |
} catch (ReadException e) {
|
774 |
throw new CancelEditingLayerException(lv.getName(), e); |
775 |
} catch (DataException e) {
|
776 |
throw new CancelEditingLayerException(lv.getName(), e); |
777 |
} |
778 |
lv.getFeatureStore().deleteObserver(view); |
779 |
lv.removeLayerListener(this);
|
780 |
|
781 |
if (view != null) { |
782 |
view.getMapControl().getMapContext().removeLayerDrawListener(lyrEd); |
783 |
} |
784 |
break;
|
785 |
case ACCEPT_EDITING:
|
786 |
lyrEd = (VectorialLayerEdited) this.getActiveLayerEdited();
|
787 |
lv.getFeatureStore().deleteObserver(lyrEd); |
788 |
featureStore = lv.getFeatureStore(); |
789 |
try {
|
790 |
featureStore.finishEditing(); |
791 |
} catch (WriteException e) {
|
792 |
throw new ReadException(lv.getName(), e); |
793 |
} catch (DataException e) {
|
794 |
throw new ReadException(lv.getName(), e); |
795 |
} |
796 |
lv.getFeatureStore().deleteObserver(view); |
797 |
lv.removeLayerListener(this);
|
798 |
if (view != null) { |
799 |
view.getMapControl().getMapContext().removeLayerDrawListener(lyrEd); |
800 |
} |
801 |
|
802 |
break;
|
803 |
case CONTINUE_EDITING:
|
804 |
break;
|
805 |
} |
806 |
|
807 |
notifyObservers(EditionNotifycation.AFTER_EXIT_EDIT_LAYER, lv, null, null); |
808 |
|
809 |
} |
810 |
} |