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 @ 89
History | View | Annotate | Download (13.4 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 = (EditingBehavior) editingCompoundBehavior
|
99 |
// .getBehavior(EditingCompoundBehavior.EDITING_INDEX);
|
100 |
EditingBehavior editingBehavior = getEditingBehavior(); |
101 |
editingCompoundBehavior.setDrawnBehavior( |
102 |
EditingCompoundBehavior.EDITING_INDEX, true);
|
103 |
editingBehavior.activateService(name); |
104 |
} |
105 |
} |
106 |
|
107 |
public void beginEdition(FLyrVect layer, Behavior[] additionalBehaviors) { |
108 |
|
109 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
110 |
.getEditingNotificationManager(); |
111 |
|
112 |
EditingNotification notification = editingNotificationManager |
113 |
.notifyObservers(this, EditingNotification.BEFORE_ENTER_EDITING_STORE,
|
114 |
null, layer);
|
115 |
|
116 |
if (notification.isCanceled()) {
|
117 |
logger.info("Edit layer '" + layer.getName()
|
118 |
+ "' canceled by somme observer.", new StartEditingException( |
119 |
"Edit layer '" + layer.getName() + "' canceled.", null)); |
120 |
|
121 |
} |
122 |
|
123 |
try {
|
124 |
addBehaviors(additionalBehaviors); |
125 |
} |
126 |
catch (CreateEditingBehaviorException e1) {
|
127 |
logger.error(e1.getMessage(), e1); |
128 |
} |
129 |
|
130 |
try {
|
131 |
layer.getFeatureStore().edit(); |
132 |
} |
133 |
catch (Exception e) { |
134 |
logger.error("Can't set featureStore in edit mode",
|
135 |
new VectorEditingException(e));
|
136 |
} |
137 |
|
138 |
// layer.getFeatureStore().addObserver(view);
|
139 |
layer.getFeatureStore().addObserver(mapControl); |
140 |
|
141 |
editingNotificationManager.notifyObservers(this,
|
142 |
EditingNotification.AFTER_ENTER_EDITING_STORE, null, layer);
|
143 |
} |
144 |
|
145 |
private void addBehaviors(Behavior[] additionalBehavior) |
146 |
throws CreateEditingBehaviorException {
|
147 |
|
148 |
EditingBehavior editingBehavior; |
149 |
EditingCompoundBehavior editingCompoundBehavior; |
150 |
if (!mapControl.hasTool("VectorEditing")) { |
151 |
editingBehavior = new DefaultEditingBehavior(this); |
152 |
editingCompoundBehavior = new EditingCompoundBehavior(editingBehavior);
|
153 |
setCompoundBehavior(editingCompoundBehavior); |
154 |
if (additionalBehavior != null) { |
155 |
Behavior[] behaviors = new Behavior[additionalBehavior.length + 1]; |
156 |
behaviors[0] = editingCompoundBehavior;
|
157 |
for (int i = 0; i < additionalBehavior.length; i++) { |
158 |
behaviors[i + 1] = additionalBehavior[i];
|
159 |
} |
160 |
mapControl.addBehavior("VectorEditing", behaviors);
|
161 |
} |
162 |
else {
|
163 |
mapControl.addBehavior("VectorEditing", editingCompoundBehavior);
|
164 |
} |
165 |
} |
166 |
else {
|
167 |
editingCompoundBehavior = getEditingCompoundBehavior(); |
168 |
editingBehavior = (EditingBehavior) editingCompoundBehavior |
169 |
.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
170 |
setCompoundBehavior(editingCompoundBehavior); |
171 |
editingBehavior.cleanBehavior(); |
172 |
} |
173 |
|
174 |
mapControl.setTool("VectorEditing");
|
175 |
showConsole(); |
176 |
} |
177 |
|
178 |
private void discardChanges(FLyrVect layer) throws EndEditingException { |
179 |
FeatureStore featureStore = layer.getFeatureStore(); |
180 |
try {
|
181 |
featureStore.cancelEditing(); |
182 |
EditingBehavior editingBehavior = getEditingBehavior(); |
183 |
editingBehavior.cleanBehavior(); |
184 |
hideConsole(); |
185 |
} |
186 |
catch (Exception e) { |
187 |
throw new EndEditingException(e); |
188 |
} |
189 |
|
190 |
featureStore.deleteObserver(mapControl); |
191 |
|
192 |
} |
193 |
|
194 |
private void doAction(FLyrVect layer, int option) { |
195 |
switch (option) {
|
196 |
case SAVE_CHANGES:
|
197 |
try {
|
198 |
saveChanges(layer); |
199 |
} |
200 |
catch (VectorEditingException e) {
|
201 |
logger.error("Changes can not be saved in " + layer.getName(), e);
|
202 |
} |
203 |
break;
|
204 |
case DISCARD_CHANGES:
|
205 |
try {
|
206 |
discardChanges(layer); |
207 |
} |
208 |
catch (VectorEditingException e) {
|
209 |
logger.error("Changes can not be discared in " + layer.getName(), e);
|
210 |
} |
211 |
break;
|
212 |
case EXPORT_LAYER:
|
213 |
try {
|
214 |
exportLayer(layer); |
215 |
} |
216 |
catch (VectorEditingException e) {
|
217 |
logger.error("Changes can not be imported", e);
|
218 |
} |
219 |
break;
|
220 |
case CANCEL:
|
221 |
break;
|
222 |
} |
223 |
|
224 |
} |
225 |
|
226 |
public void enableSelection(boolean enableSelection) { |
227 |
this.editingCompoundBehavior.setDrawnBehavior(
|
228 |
EditingCompoundBehavior.SELECTION_INDEX, enableSelection); |
229 |
} |
230 |
|
231 |
public void endEdition(FLyrVect layer) { |
232 |
if (layer.isEditing()) {
|
233 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
234 |
.getEditingNotificationManager(); |
235 |
|
236 |
EditingNotification notification = editingNotificationManager |
237 |
.notifyObservers(this, EditingNotification.BEFORE_EXIT_EDITING_STORE,
|
238 |
null, layer);
|
239 |
|
240 |
if (notification.isCanceled()) {
|
241 |
logger.info("Stop edit layer '" + layer.getName()
|
242 |
+ "' canceled by somme observer.", new EndEditingException( |
243 |
"Stop edit layer '" + layer.getName() + "' canceled.", null)); |
244 |
|
245 |
} |
246 |
mapControl.getCanceldraw().setCanceled(true);
|
247 |
int option;
|
248 |
EditingSwingManager swingManager = EditingSwingLocator.getSwingManager(); |
249 |
if (layer.isWritable()) {
|
250 |
option = swingManager.showPanelSaveOrDiscard(mapControl, |
251 |
layer.getName()); |
252 |
} |
253 |
else {
|
254 |
option = swingManager.showPanelExportOrDiscard(mapControl, |
255 |
layer.getName()); |
256 |
} |
257 |
|
258 |
doAction(layer, option); |
259 |
|
260 |
editingNotificationManager.notifyObservers(this,
|
261 |
EditingNotification.AFTER_EXIT_EDITING_STORE, null, layer);
|
262 |
|
263 |
} |
264 |
|
265 |
} |
266 |
|
267 |
private void exportLayer(FLyrVect layer) throws EndEditingException { |
268 |
// TODO Auto-generated method stub
|
269 |
|
270 |
} |
271 |
|
272 |
private JConsole getConsolePanel() {
|
273 |
if (console == null) { |
274 |
console = new JConsole(true); |
275 |
// Para distinguir cuando se est? escribiendo sobre la consola y
|
276 |
// cuando no.
|
277 |
console.setJTextName("VectorEditingConsole");
|
278 |
} |
279 |
return console;
|
280 |
} |
281 |
|
282 |
private Component getDockConsole() { |
283 |
if (dockConsole == null) { |
284 |
dockConsole = new JDockPanel(getConsolePanel());
|
285 |
} |
286 |
return dockConsole;
|
287 |
} |
288 |
|
289 |
private EditingBehavior getEditingBehavior() {
|
290 |
return (EditingBehavior) editingCompoundBehavior
|
291 |
.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
292 |
} |
293 |
|
294 |
private EditingCompoundBehavior getEditingCompoundBehavior() {
|
295 |
if (editingCompoundBehavior != null) { |
296 |
return editingCompoundBehavior;
|
297 |
} |
298 |
else {
|
299 |
EditingCompoundBehavior editingCompoundBehavior; |
300 |
|
301 |
CompoundBehavior compoundBehavior = (CompoundBehavior) mapControl |
302 |
.getMapTool("VectorEditing");
|
303 |
if (compoundBehavior instanceof EditingCompoundBehavior) { |
304 |
editingCompoundBehavior = (EditingCompoundBehavior) compoundBehavior; |
305 |
} |
306 |
else {
|
307 |
editingCompoundBehavior = (EditingCompoundBehavior) compoundBehavior |
308 |
.getBehavior(0);
|
309 |
} |
310 |
setCompoundBehavior(editingCompoundBehavior); |
311 |
return editingCompoundBehavior;
|
312 |
} |
313 |
} |
314 |
|
315 |
public MapControl getMapControl() {
|
316 |
return this.mapControl; |
317 |
} |
318 |
|
319 |
/**
|
320 |
* Hides editing console.
|
321 |
*/
|
322 |
private void hideConsole() { |
323 |
isShowConsole = false;
|
324 |
getDockConsole().setVisible(false);
|
325 |
} |
326 |
|
327 |
private void initConsolePanelListeners(JConsole jConsole) { |
328 |
jConsole.addResponseListener(new ResponseListener() {
|
329 |
|
330 |
public void acceptResponse(String response) { |
331 |
getEditingBehavior().textEntered(response); |
332 |
|
333 |
} |
334 |
}); |
335 |
} |
336 |
|
337 |
private void initMapControlListeners() { |
338 |
|
339 |
MapContext context = mapControl.getMapContext(); |
340 |
FLayers layers = context.getLayers(); |
341 |
layers.addLayerListener(layerListener); |
342 |
|
343 |
layers.addLayerCollectionListener(new LayerCollectionListener() {
|
344 |
|
345 |
public void layerAdded(LayerCollectionEvent e) { |
346 |
FLayers layers2 = e.getLayers(); |
347 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
348 |
FLayer layer = layers2.getLayer(i); |
349 |
if (layer instanceof FLyrVect) { |
350 |
((FLyrVect) layer).addLayerListener(layerListener); |
351 |
} |
352 |
} |
353 |
} |
354 |
|
355 |
public void layerAdding(LayerCollectionEvent e) |
356 |
throws CancelationException {}
|
357 |
|
358 |
public void layerMoved(LayerPositionEvent e) {} |
359 |
|
360 |
public void layerMoving(LayerPositionEvent e) throws CancelationException {} |
361 |
|
362 |
public void layerRemoved(LayerCollectionEvent e) { |
363 |
FLayers layers2 = e.getLayers(); |
364 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
365 |
FLayer layer = layers2.getLayer(i); |
366 |
if (layer instanceof FLyrVect) { |
367 |
((FLyrVect) layer).removeLayerListener(layerListener); |
368 |
} |
369 |
} |
370 |
} |
371 |
|
372 |
public void layerRemoving(LayerCollectionEvent e) |
373 |
throws CancelationException {}
|
374 |
|
375 |
public void visibilityChanged(LayerCollectionEvent e) |
376 |
throws CancelationException {}
|
377 |
}); |
378 |
} |
379 |
|
380 |
private void saveChanges(FLyrVect layer) throws EndEditingException { |
381 |
FeatureStore featureStore = layer.getFeatureStore(); |
382 |
try {
|
383 |
featureStore.finishEditing(); |
384 |
EditingBehavior editingBehavior = getEditingBehavior(); |
385 |
// EditingCompoundBehavior editingCompoundBehavior;
|
386 |
// editingCompoundBehavior = (EditingCompoundBehavior) mapControl
|
387 |
// .getMapTool("VectorEditing");
|
388 |
// editingBehavior = (EditingBehavior) editingCompoundBehavior
|
389 |
// .getBehavior(EditingCompoundBehavior.EDITING_INDEX);
|
390 |
|
391 |
editingBehavior.cleanBehavior(); |
392 |
hideConsole(); |
393 |
} |
394 |
catch (Exception e) { |
395 |
throw new EndEditingException(e); |
396 |
} |
397 |
|
398 |
featureStore.deleteObserver(mapControl); |
399 |
|
400 |
} |
401 |
|
402 |
private void setCaretPosition() { |
403 |
JEditTextArea jeta = getConsolePanel().getTxt(); |
404 |
jeta.requestFocusInWindow(); |
405 |
jeta.setCaretPosition(jeta.getText().length()); |
406 |
} |
407 |
|
408 |
public void setCompoundBehavior(EditingCompoundBehavior compoundBehavior) { |
409 |
this.editingCompoundBehavior = compoundBehavior;
|
410 |
} |
411 |
|
412 |
/**
|
413 |
* Shows editing console.
|
414 |
*/
|
415 |
private void showConsole() { |
416 |
if (isShowConsole) {
|
417 |
return;
|
418 |
} |
419 |
isShowConsole = true;
|
420 |
getMapControl().remove(getDockConsole()); |
421 |
getMapControl().setLayout(new BorderLayout()); |
422 |
getMapControl().add(getDockConsole(), BorderLayout.SOUTH);
|
423 |
getDockConsole().setVisible(true);
|
424 |
setCaretPosition(); |
425 |
} |
426 |
|
427 |
public void showConsoleMessage(String text) { |
428 |
getConsolePanel().addText(text, JConsole.MESSAGE); |
429 |
} |
430 |
} |