gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.swing / org.gvsig.vectorediting.swing.impl / src / main / java / org / gvsig / vectorediting / swing / impl / DefaultEditingContext.java @ 90
History | View | Annotate | Download (13 KB)
1 |
/*
|
---|---|
2 |
* Copyright 2014 DiSiD Technologies S.L.L. All rights reserved.
|
3 |
*
|
4 |
* Project : DiSiD org.gvsig.vectorediting.swing.impl
|
5 |
* SVN Id : $Id$
|
6 |
*/
|
7 |
package org.gvsig.vectorediting.swing.impl; |
8 |
|
9 |
import java.awt.BorderLayout; |
10 |
import java.awt.Component; |
11 |
|
12 |
import org.gvsig.editing.EditingNotification; |
13 |
import org.gvsig.editing.EditingNotificationManager; |
14 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
15 |
import org.gvsig.fmap.mapcontext.MapContext; |
16 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
17 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
18 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
19 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
20 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener; |
21 |
import org.gvsig.fmap.mapcontext.layers.LayerEvent; |
22 |
import org.gvsig.fmap.mapcontext.layers.LayerListener; |
23 |
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent; |
24 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
25 |
import org.gvsig.fmap.mapcontrol.MapControl; |
26 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
27 |
import org.gvsig.fmap.mapcontrol.tools.CompoundBehavior; |
28 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
29 |
import org.gvsig.utils.console.JConsole; |
30 |
import org.gvsig.utils.console.JDockPanel; |
31 |
import org.gvsig.utils.console.ResponseListener; |
32 |
import org.gvsig.utils.console.jedit.JEditTextArea; |
33 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
34 |
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException; |
35 |
import org.gvsig.vectorediting.lib.api.exceptions.EndEditingException; |
36 |
import org.gvsig.vectorediting.lib.api.exceptions.StartEditingException; |
37 |
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException; |
38 |
import org.gvsig.vectorediting.swing.api.EditingBehavior; |
39 |
import org.gvsig.vectorediting.swing.api.EditingContext; |
40 |
import org.gvsig.vectorediting.swing.api.EditingSwingLocator; |
41 |
import org.gvsig.vectorediting.swing.api.EditingSwingManager; |
42 |
import org.slf4j.Logger; |
43 |
import org.slf4j.LoggerFactory; |
44 |
|
45 |
public class DefaultEditingContext implements EditingContext { |
46 |
|
47 |
private static final Logger logger = LoggerFactory |
48 |
.getLogger(EditingManager.class); |
49 |
|
50 |
private MapControl mapControl;
|
51 |
|
52 |
private EditingCompoundBehavior editingCompoundBehavior;
|
53 |
|
54 |
private JConsole console;
|
55 |
|
56 |
private JDockPanel dockConsole = null; |
57 |
|
58 |
private boolean isShowConsole = false; |
59 |
|
60 |
private LayerListener layerListener = new LayerListener() { |
61 |
|
62 |
public void activationChanged(LayerEvent e) { |
63 |
FLayer layer = e.getSource(); |
64 |
if (layer instanceof FLyrVect) { |
65 |
|
66 |
if (layer.isActive() && layer.isEditing()) {
|
67 |
showConsole(); |
68 |
getMapControl().setTool("VectorEditing");
|
69 |
} |
70 |
else if (!layer.isEditing() && layer.isActive()) { |
71 |
hideConsole(); |
72 |
getMapControl().setTool("zoomIn");
|
73 |
} |
74 |
getEditingBehavior().changeCurrentLayer((FLyrVect) layer); |
75 |
} |
76 |
|
77 |
} |
78 |
|
79 |
public void drawValueChanged(LayerEvent e) {} |
80 |
|
81 |
public void editionChanged(LayerEvent e) {} |
82 |
|
83 |
public void nameChanged(LayerEvent e) {} |
84 |
|
85 |
public void visibilityChanged(LayerEvent e) {} |
86 |
}; |
87 |
|
88 |
public DefaultEditingContext(MapControl mapControl) {
|
89 |
this.mapControl = mapControl;
|
90 |
initMapControlListeners(); |
91 |
initConsolePanelListeners(getConsolePanel()); |
92 |
} |
93 |
|
94 |
public void activateService(String name) { |
95 |
if (mapControl != null && mapControl.hasTool("VectorEditing")) { |
96 |
CompoundBehavior editingCompoundBehavior = getEditingCompoundBehavior(); |
97 |
mapControl.setTool("VectorEditing");
|
98 |
EditingBehavior editingBehavior = getEditingBehavior(); |
99 |
editingCompoundBehavior.setDrawnBehavior( |
100 |
EditingCompoundBehavior.EDITING_INDEX, true);
|
101 |
editingBehavior.activateService(name); |
102 |
} |
103 |
} |
104 |
|
105 |
public void beginEdition(FLyrVect layer, Behavior[] additionalBehaviors) { |
106 |
|
107 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
108 |
.getEditingNotificationManager(); |
109 |
|
110 |
EditingNotification notification = editingNotificationManager |
111 |
.notifyObservers(this, EditingNotification.BEFORE_ENTER_EDITING_STORE,
|
112 |
null, layer);
|
113 |
|
114 |
if (notification.isCanceled()) {
|
115 |
logger.info("Edit layer '" + layer.getName()
|
116 |
+ "' canceled by somme observer.", new StartEditingException( |
117 |
"Edit layer '" + layer.getName() + "' canceled.", null)); |
118 |
|
119 |
} |
120 |
|
121 |
try {
|
122 |
addBehaviors(additionalBehaviors); |
123 |
} |
124 |
catch (CreateEditingBehaviorException e1) {
|
125 |
logger.error(e1.getMessage(), e1); |
126 |
} |
127 |
|
128 |
try {
|
129 |
layer.getFeatureStore().edit(); |
130 |
} |
131 |
catch (Exception e) { |
132 |
logger.error("Can't set featureStore in edit mode",
|
133 |
new VectorEditingException(e));
|
134 |
} |
135 |
|
136 |
// layer.getFeatureStore().addObserver(view);
|
137 |
layer.getFeatureStore().addObserver(mapControl); |
138 |
|
139 |
editingNotificationManager.notifyObservers(this,
|
140 |
EditingNotification.AFTER_ENTER_EDITING_STORE, null, layer);
|
141 |
} |
142 |
|
143 |
private void addBehaviors(Behavior[] additionalBehavior) |
144 |
throws CreateEditingBehaviorException {
|
145 |
|
146 |
EditingBehavior editingBehavior; |
147 |
EditingCompoundBehavior editingCompoundBehavior; |
148 |
if (!mapControl.hasTool("VectorEditing")) { |
149 |
editingBehavior = new DefaultEditingBehavior(this); |
150 |
editingCompoundBehavior = new EditingCompoundBehavior(editingBehavior);
|
151 |
setCompoundBehavior(editingCompoundBehavior); |
152 |
if (additionalBehavior != null) { |
153 |
Behavior[] behaviors = new Behavior[additionalBehavior.length + 1]; |
154 |
behaviors[0] = editingCompoundBehavior;
|
155 |
for (int i = 0; i < additionalBehavior.length; i++) { |
156 |
behaviors[i + 1] = additionalBehavior[i];
|
157 |
} |
158 |
mapControl.addBehavior("VectorEditing", behaviors);
|
159 |
} |
160 |
else {
|
161 |
mapControl.addBehavior("VectorEditing", editingCompoundBehavior);
|
162 |
} |
163 |
} |
164 |
else {
|
165 |
editingCompoundBehavior = getEditingCompoundBehavior(); |
166 |
editingBehavior = (EditingBehavior) editingCompoundBehavior |
167 |
.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
168 |
setCompoundBehavior(editingCompoundBehavior); |
169 |
editingBehavior.cleanBehavior(); |
170 |
} |
171 |
|
172 |
mapControl.setTool("VectorEditing");
|
173 |
showConsole(); |
174 |
} |
175 |
|
176 |
private void discardChanges(FLyrVect layer) throws EndEditingException { |
177 |
FeatureStore featureStore = layer.getFeatureStore(); |
178 |
try {
|
179 |
featureStore.cancelEditing(); |
180 |
EditingBehavior editingBehavior = getEditingBehavior(); |
181 |
editingBehavior.cleanBehavior(); |
182 |
hideConsole(); |
183 |
} |
184 |
catch (Exception e) { |
185 |
throw new EndEditingException(e); |
186 |
} |
187 |
|
188 |
featureStore.deleteObserver(mapControl); |
189 |
|
190 |
} |
191 |
|
192 |
private void doAction(FLyrVect layer, int option) { |
193 |
switch (option) {
|
194 |
case SAVE_CHANGES:
|
195 |
try {
|
196 |
saveChanges(layer); |
197 |
} |
198 |
catch (VectorEditingException e) {
|
199 |
logger.error("Changes can not be saved in " + layer.getName(), e);
|
200 |
} |
201 |
break;
|
202 |
case DISCARD_CHANGES:
|
203 |
try {
|
204 |
discardChanges(layer); |
205 |
} |
206 |
catch (VectorEditingException e) {
|
207 |
logger.error("Changes can not be discared in " + layer.getName(), e);
|
208 |
} |
209 |
break;
|
210 |
case EXPORT_LAYER:
|
211 |
try {
|
212 |
exportLayer(layer); |
213 |
} |
214 |
catch (VectorEditingException e) {
|
215 |
logger.error("Changes can not be imported", e);
|
216 |
} |
217 |
break;
|
218 |
case CANCEL:
|
219 |
break;
|
220 |
} |
221 |
|
222 |
} |
223 |
|
224 |
public void enableSelection(boolean enableSelection) { |
225 |
this.editingCompoundBehavior.setDrawnBehavior(
|
226 |
EditingCompoundBehavior.SELECTION_INDEX, enableSelection); |
227 |
} |
228 |
|
229 |
public void endEdition(FLyrVect layer) { |
230 |
if (layer.isEditing()) {
|
231 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
232 |
.getEditingNotificationManager(); |
233 |
|
234 |
EditingNotification notification = editingNotificationManager |
235 |
.notifyObservers(this, EditingNotification.BEFORE_EXIT_EDITING_STORE,
|
236 |
null, layer);
|
237 |
|
238 |
if (notification.isCanceled()) {
|
239 |
logger.info("Stop edit layer '" + layer.getName()
|
240 |
+ "' canceled by somme observer.", new EndEditingException( |
241 |
"Stop edit layer '" + layer.getName() + "' canceled.", null)); |
242 |
|
243 |
} |
244 |
mapControl.getCanceldraw().setCanceled(true);
|
245 |
int option;
|
246 |
EditingSwingManager swingManager = EditingSwingLocator.getSwingManager(); |
247 |
if (layer.isWritable()) {
|
248 |
option = swingManager.showPanelSaveOrDiscard(mapControl, |
249 |
layer.getName()); |
250 |
} |
251 |
else {
|
252 |
option = swingManager.showPanelExportOrDiscard(mapControl, |
253 |
layer.getName()); |
254 |
} |
255 |
|
256 |
doAction(layer, option); |
257 |
|
258 |
editingNotificationManager.notifyObservers(this,
|
259 |
EditingNotification.AFTER_EXIT_EDITING_STORE, null, layer);
|
260 |
|
261 |
} |
262 |
|
263 |
} |
264 |
|
265 |
private void exportLayer(FLyrVect layer) throws EndEditingException { |
266 |
// TODO Auto-generated method stub
|
267 |
|
268 |
} |
269 |
|
270 |
private JConsole getConsolePanel() {
|
271 |
if (console == null) { |
272 |
console = new JConsole(true); |
273 |
// Para distinguir cuando se est? escribiendo sobre la consola y
|
274 |
// cuando no.
|
275 |
console.setJTextName("VectorEditingConsole");
|
276 |
} |
277 |
return console;
|
278 |
} |
279 |
|
280 |
private Component getDockConsole() { |
281 |
if (dockConsole == null) { |
282 |
dockConsole = new JDockPanel(getConsolePanel());
|
283 |
} |
284 |
return dockConsole;
|
285 |
} |
286 |
|
287 |
private EditingBehavior getEditingBehavior() {
|
288 |
return (EditingBehavior) editingCompoundBehavior
|
289 |
.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
290 |
} |
291 |
|
292 |
private EditingCompoundBehavior getEditingCompoundBehavior() {
|
293 |
if (editingCompoundBehavior != null) { |
294 |
return editingCompoundBehavior;
|
295 |
} |
296 |
else {
|
297 |
EditingCompoundBehavior editingCompoundBehavior; |
298 |
|
299 |
CompoundBehavior compoundBehavior = (CompoundBehavior) mapControl |
300 |
.getMapTool("VectorEditing");
|
301 |
if (compoundBehavior instanceof EditingCompoundBehavior) { |
302 |
editingCompoundBehavior = (EditingCompoundBehavior) compoundBehavior; |
303 |
} |
304 |
else {
|
305 |
editingCompoundBehavior = (EditingCompoundBehavior) compoundBehavior |
306 |
.getBehavior(0);
|
307 |
} |
308 |
setCompoundBehavior(editingCompoundBehavior); |
309 |
return editingCompoundBehavior;
|
310 |
} |
311 |
} |
312 |
|
313 |
public MapControl getMapControl() {
|
314 |
return this.mapControl; |
315 |
} |
316 |
|
317 |
/**
|
318 |
* Hides editing console.
|
319 |
*/
|
320 |
private void hideConsole() { |
321 |
isShowConsole = false;
|
322 |
getDockConsole().setVisible(false);
|
323 |
} |
324 |
|
325 |
private void initConsolePanelListeners(JConsole jConsole) { |
326 |
jConsole.addResponseListener(new ResponseListener() {
|
327 |
|
328 |
public void acceptResponse(String response) { |
329 |
getEditingBehavior().textEntered(response); |
330 |
|
331 |
} |
332 |
}); |
333 |
} |
334 |
|
335 |
private void initMapControlListeners() { |
336 |
|
337 |
MapContext context = mapControl.getMapContext(); |
338 |
FLayers layers = context.getLayers(); |
339 |
layers.addLayerListener(layerListener); |
340 |
|
341 |
layers.addLayerCollectionListener(new LayerCollectionListener() {
|
342 |
|
343 |
public void layerAdded(LayerCollectionEvent e) { |
344 |
FLayers layers2 = e.getLayers(); |
345 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
346 |
FLayer layer = layers2.getLayer(i); |
347 |
if (layer instanceof FLyrVect) { |
348 |
((FLyrVect) layer).addLayerListener(layerListener); |
349 |
} |
350 |
} |
351 |
} |
352 |
|
353 |
public void layerAdding(LayerCollectionEvent e) |
354 |
throws CancelationException {}
|
355 |
|
356 |
public void layerMoved(LayerPositionEvent e) {} |
357 |
|
358 |
public void layerMoving(LayerPositionEvent e) throws CancelationException {} |
359 |
|
360 |
public void layerRemoved(LayerCollectionEvent e) { |
361 |
FLayers layers2 = e.getLayers(); |
362 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
363 |
FLayer layer = layers2.getLayer(i); |
364 |
if (layer instanceof FLyrVect) { |
365 |
((FLyrVect) layer).removeLayerListener(layerListener); |
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
public void layerRemoving(LayerCollectionEvent e) |
371 |
throws CancelationException {}
|
372 |
|
373 |
public void visibilityChanged(LayerCollectionEvent e) |
374 |
throws CancelationException {}
|
375 |
}); |
376 |
} |
377 |
|
378 |
private void saveChanges(FLyrVect layer) throws EndEditingException { |
379 |
FeatureStore featureStore = layer.getFeatureStore(); |
380 |
try {
|
381 |
featureStore.finishEditing(); |
382 |
EditingBehavior editingBehavior = getEditingBehavior(); |
383 |
editingBehavior.cleanBehavior(); |
384 |
hideConsole(); |
385 |
} |
386 |
catch (Exception e) { |
387 |
throw new EndEditingException(e); |
388 |
} |
389 |
|
390 |
featureStore.deleteObserver(mapControl); |
391 |
|
392 |
} |
393 |
|
394 |
private void setCaretPosition() { |
395 |
JEditTextArea jeta = getConsolePanel().getTxt(); |
396 |
jeta.requestFocusInWindow(); |
397 |
jeta.setCaretPosition(jeta.getText().length()); |
398 |
} |
399 |
|
400 |
public void setCompoundBehavior(EditingCompoundBehavior compoundBehavior) { |
401 |
this.editingCompoundBehavior = compoundBehavior;
|
402 |
} |
403 |
|
404 |
/**
|
405 |
* Shows editing console.
|
406 |
*/
|
407 |
private void showConsole() { |
408 |
if (isShowConsole) {
|
409 |
return;
|
410 |
} |
411 |
isShowConsole = true;
|
412 |
getMapControl().remove(getDockConsole()); |
413 |
getMapControl().setLayout(new BorderLayout()); |
414 |
getMapControl().add(getDockConsole(), BorderLayout.SOUTH);
|
415 |
getDockConsole().setVisible(true);
|
416 |
setCaretPosition(); |
417 |
} |
418 |
|
419 |
public void showConsoleMessage(String text) { |
420 |
getConsolePanel().addText(text, JConsole.MESSAGE); |
421 |
} |
422 |
} |